Django - 限制对实例的选择 [英] Django - Limit choices to something that depends on the instance
问题描述
因此,我的模型看起来像这样:
$ b $
$ b $更多字段...
获胜者= models.ManyToManyField(
'Foo',
related_name ='winner'
)
class Foo(models.Model):
name = models .CharField(max_length = 30)
#更多字段...
category = models.ForeignKey(
类别,
related_name ='category'
)
(获胜者
的原因是 ManyToManyField
是一个单一的foo可能属于几个类别,而在单个类别中,由于前者可能会有多个获胜者。)
我想强加一个foo可以在类别中获得的自然约束,只有它属于该类别。最合理的方式似乎是使用 limit_choices_to
参数,但在我看来,根据模型的当前实例不可能限制选择。
我可以以给定的形式强加这个约束,但是我想避免这种情况有两个原因:
- 将在管理员中选择获胜者,我想避免自定义管理表单
<限制自然地生活在模型层面。这是一个特定的关系,应该始终保持在两个相关模型之间。
有没有办法在Django中强加这个约束?
没有办法在模型级别(即使用limit_choices_to)对M2M字段进行约束。但是,您可以这样做:
class MyForm(forms.ModelForm):
class Meta:
model = models.Category
def __init __(self,* args,** kwargs):
super(MyForm,self).__ init __(* args,** kwargs)
如果kwargs中的实例:
my_category = kwargs ['instance']
self.fields ['winner']。queryset = my_category.category.all()
嗯,你注意到了吗?
my_category.category.all()
可能你想要的是:
class Foo(models.Model):
name = models.CharField(max_length = 30)
#更多字段。 ..
category = models.ForeignKey(
类别,
related_name ='参与者
)
I have some foos which are organized into categories. For each category, I want to be able to select a winner foo.
Hence I have models which look like this:
class Category(models.Model):
name = models.CharField(max_length=30)
# More fields...
winner = models.ManyToManyField(
'Foo',
related_name='winner'
)
class Foo(models.Model):
name = models.CharField(max_length=30)
# More fields...
category = models.ForeignKey(
Category,
related_name='category'
)
(The reason why winner
is a ManyToManyField
is that a single foo may belong to several categories, while in a single category there may be more than one winner due to ex-aequo.)
I want to impose the natural constraint that a foo can win in a category only if it belongs to that category. The most reasonable way to do so seems to use the limit_choices_to
parameter, but it seems to me that it is not possible to limit the choices based on the current instance of the model.
I may be able to impose this constraint in a given form, but I would like to avoid this for two reasons:
- the constraint naturally lives at the model level. It is a particular relation that should always hold between two related models
- the choice of the winner will be made in the admin, and I would like to avoid having to customize the admin forms
Is there any way to impose this constraint in Django?
There is no way to put constraint on M2M field on the models level (ie with limit_choices_to). However, you can do this in the form:
class MyForm(forms.ModelForm):
class Meta:
model = models.Category
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
if 'instance' in kwargs:
my_category = kwargs['instance']
self.fields['winner'].queryset = my_category.category.all()
Well, did you notice that?
my_category.category.all()
Probably what you want is:
class Foo(models.Model):
name = models.CharField(max_length=30)
# More fields...
category = models.ForeignKey(
Category,
related_name='participants'
)
这篇关于Django - 限制对实例的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!