我只想在 OneToOneField 中列出可用的项目而不是所有项目,它不像在 ChoiceField 中过滤值,因为我们需要仅找出可以使用的值,这是基于它是否已被使用的原则.
我有一个模型定义如下:
class Foo(models.Model):
somefield = models.CharField(max_length=12)
class Bar(models.Model):
somefield = models.CharField(max_length=12)
foo = models.OneToOneField(Foo)
现在我使用 ModelForm 创建基于 Bar 模型的表单:
class BarForm(ModelForm):
class Meta:
model = Bar
现在的问题在于它使用 HTML 的选择小部件在 ChoiceField 中显示数据库中所有可用 Foo 对象列表的形式,因为该字段是 OneToOneField django 将强制 Bar 对象与 Foo 对象的单一关联,但是由于它在列表中显示了所有可用和不可用的项目,因此很难找出表单中可接受的值,用户被迫使用命中/试用方法来找出正确的选项。
如何更改此行为并仅列出字段中可以使用的那些项目?
最佳答案
虽然这是一个老话题,但我在寻找相同答案时遇到了它。
特别针对 OP:
调整您的 BarForm,使其看起来像:
class BarForm(ModelForm):
class Meta:
model = Bar
def __init__(self, *args, **kwargs):
super(BarForm, self).__init__(*args, **kwargs)
#only provide Foos that are not already linked to a Bar, plus the Foo that was already chosen for this Bar
self.fields['foo'].queryset = Foo.objects.filter(Q(bar__isnull=True)|Q(bar=self.instance))
这应该可以解决问题。您覆盖 init 函数,以便您可以编辑表单中的 foo 字段,为其提供一个更具体的查询集,该查询集包含可用的 Foo 和(相当重要的)已被选中的 Foo。
针对我自己的情况
我最初的问题是:如何只显示 OneToOne 关系中的可用用户?
我的 models.py
中的 Actor 模型如下所示:
class Actor(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name = 'peactor')
# lots of other fields and some methods here
在我的 admin.py
中,我有以下类:
class ActorAdmin(admin.ModelAdmin):
# some defines for list_display, actions etc here
form = ActorForm
我之前没有使用特殊表单(只是依赖 Django 默认为 ModelAdmin 提供的基本 ModelForm),但我需要它来解决以下问题。
所以,最后,在我的 forms.py
中我有:
class ActorForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ActorForm, self).__init__(*args, **kwargs)
#only provide users that are not already linked to an actor, plus the user that was already chosen for this Actor
self.fields['user'].queryset = User.objects.filter(Q(peactor__isnull=True)|Q(peactor=self.instance))
所以在这里我制作了一个 ActorForm 并覆盖了 __init__
方法。
self.fields['user'].queryset =
设置用户表单域使用的查询集。这个表单域是一个 ModelChoiceField 默认情况下用于模型上的 OneToOneField(或 ForeignKey)。
Q(peactor__isnull=True)|Q(peactor=self.instance)
Q 代表 Q-objects有助于“复杂”查询,例如 或
语句。
所以这个查询说:没有设置 peactor 或 peactor 与已经为这个 actor 选择的相同
peactor
是 related_name
对于 Actor 。
这样一来,您不仅可以获得可用的用户,还可以获得不可用的用户,因为它已经链接到您当前正在编辑的对象。
我希望这对有同样问题的人有所帮助。 :-)
关于django - 仅列出 OneToOneField Django 中的可用值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33078717/