Django模型的最佳实践是什么?

时间:2021-12-26 20:07:11

Is there a best practice for ForeignKey unique=True values not to be displayed in the forms selection choice?

在选择表单时,是否有不显示真值的方法?

WarehouseBin = models.ForeignKey(WarehouseBin, unique=True)

It is annoying for the users to select options that they are not allowed in model. Do I have to redefine in the view form values after it was initiated .

用户选择模型中不允许的选项是很烦人的。我是否必须在视图表单初始化后重新定义它的值。

 form = ***Form(instance=post)
    form.fields['WarehouseBin'].queryset =***.objects.exclude(***)

Is there any other way?

还有别的办法吗?

1 个解决方案

#1


1  

The best practice to control the content of the foreign key fields is to override your form __init__() method. That way your form logic is nicely separated from your view, and you can reuse the same form in your view and admin if you want to.

控制外键字段内容的最佳实践是重写您的form __init__()方法。这样,您的表单逻辑就与您的视图很好地分离了,如果您愿意,您可以在您的视图和管理中重用相同的表单。

from django.forms import ModelForm
from myapp.models import Article

# Create the form class.
class ArticleForm(ModelForm):
    class Meta:
        model = Article
        fields = ['pub_date', 'headline', 'content', 'reporter']

    def __init__(self, *args, **kwargs):
        super(ArticleForm, self).__init__(*args, **kwargs)
        # fetch only active reporters
        self.fields['reporter'].queryset = Repoter.objects.filter(active=True)

For more details see: https://docs.djangoproject.com/en/dev/ref/forms/fields/#fields-which-handle-relationships

有关更多细节,请参见:https://docs.djangoproject.com/en/dev/ref/forms/fields/fields/ #fields- hand -hand -relationships

BTW, since you are interested in best practices, here's another tip. You shouldn't use camel case for your class fields, but lowercase with words separated with underscore:

顺便说一下,既然你对最佳实践感兴趣,这是另一个建议。您不应该在类字段中使用驼色大小写,而是用小写字母和下划线分隔:

warehouse_bin = models.ForeignKey(WarehouseBin, unique=True)

See PEP8 for more details.

参见PEP8了解更多细节。

#1


1  

The best practice to control the content of the foreign key fields is to override your form __init__() method. That way your form logic is nicely separated from your view, and you can reuse the same form in your view and admin if you want to.

控制外键字段内容的最佳实践是重写您的form __init__()方法。这样,您的表单逻辑就与您的视图很好地分离了,如果您愿意,您可以在您的视图和管理中重用相同的表单。

from django.forms import ModelForm
from myapp.models import Article

# Create the form class.
class ArticleForm(ModelForm):
    class Meta:
        model = Article
        fields = ['pub_date', 'headline', 'content', 'reporter']

    def __init__(self, *args, **kwargs):
        super(ArticleForm, self).__init__(*args, **kwargs)
        # fetch only active reporters
        self.fields['reporter'].queryset = Repoter.objects.filter(active=True)

For more details see: https://docs.djangoproject.com/en/dev/ref/forms/fields/#fields-which-handle-relationships

有关更多细节,请参见:https://docs.djangoproject.com/en/dev/ref/forms/fields/fields/ #fields- hand -hand -relationships

BTW, since you are interested in best practices, here's another tip. You shouldn't use camel case for your class fields, but lowercase with words separated with underscore:

顺便说一下,既然你对最佳实践感兴趣,这是另一个建议。您不应该在类字段中使用驼色大小写,而是用小写字母和下划线分隔:

warehouse_bin = models.ForeignKey(WarehouseBin, unique=True)

See PEP8 for more details.

参见PEP8了解更多细节。