Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:从下拉列表中选择包含字段为_active=True的对象_Python_Django_Django Class Based Views - Fatal编程技术网

Python Django:从下拉列表中选择包含字段为_active=True的对象

Python Django:从下拉列表中选择包含字段为_active=True的对象,python,django,django-class-based-views,Python,Django,Django Class Based Views,我是一个使用django的新手,我有一个无法解决的问题。 在我的ServiceCreateView中,我需要从下拉列表中选择包含字段为_active=true的对象车, 我怎样才能做到这一点 class Car(models.Model): brand = models.charfield(...) is_active= models.BooleanField(default=True) class Service(models.Model): car = models

我是一个使用django的新手,我有一个无法解决的问题。 在我的ServiceCreateView中,我需要从下拉列表中选择包含字段为_active=true的对象车, 我怎样才能做到这一点

class Car(models.Model):
    brand = models.charfield(...)
    is_active= models.BooleanField(default=True)

class Service(models.Model):
    car = models.ForeingKey('Car'....)
    name = models.Charfield()

class ServiceCreateView(CreateView):
    model = Service
    form = ServiceForm
    ...
如果我将车型中的字段is_active更改为false,则不应在下拉列表中显示。
有人能告诉我正确的方向吗?

您可以通过以下方式限制选择:

班级服务(models.Model): car=models.ForeingKey( “汽车”, 在_delete=models.CASCADE上, 将_选项_限制为={'is_active':True} ) name=models.Charfield()
这不是在数据库级别强制执行的。因此,这意味着如果您通过视图构造一个
服务
对象,并选择一个
汽车
,此时
处于活动状态=真
,那么如果以后
汽车
设置为
处于活动状态=假
,那么
服务
记录仍将存在并指向非活动的
汽车
。因此,这只会对Django/Python层产生影响,Django/Python层只会显示
is\u active=True
Car
,而且
ModelForm
将关联选择处于非活动状态的
Car

我就是这样解决的

from django import forms
from .models import Service, Car


    class ServiceForm(forms.ModelForm):
    
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.fields['car'].queryset = Car.objects.filter(is_active=True)
    
        class Meta:
            model = Service
            fields = ('car', 'name')

谢谢这是解决我问题的另一种方法…:)@ApoloMachine:如果它应该(几乎)用于所有表单,我会在
服务
模型中设置它,因为这样就不需要更改所有
模型表单
s,而
模型管理员也会使用它(当然,您可以为
ModelAdmin
定义一个自定义
ModelForm
,但这需要更多的工作),如果它是一次性的,那么在
ModelForm
中设置它更有意义:)。
from django import forms
from .models import Service, Car


    class ServiceForm(forms.ModelForm):
    
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.fields['car'].queryset = Car.objects.filter(is_active=True)
    
        class Meta:
            model = Service
            fields = ('car', 'name')