选择基于对象字段的limit_choices_to的查询 [英] Choose queryset for limit_choices_to based on object fields
问题描述
我尝试过将这个外来字段的选择限制在那些看起来像对象自身的其他对象中。
class Model1(models.Model):
entry = models.ForeignKey(Model2,limit_choices_to ='get_limit_choices_to')
number = IntegerField()
def get_limit_choices_to(self):
return Model2.objects.filter(expenditure_type = self.expenditure_type)
class Model2(models.Model ):
number = IntegerField()
但是我收到错误
_filter_or_exclude()参数**必须是映射,而不是str
我不知道limit_choices_to是否是正确的方法。也许我应该在一个或者意见中选择查询集。
错误说, limit_choices_to ='get_limit_choices_to'
参考方法的错误方法,但是如何正确地引用方法?我不能使用
limit_choices_to = lambda:{'model1_set':self}
或
limit_choices_to = lambda:{'数字':number}
我正在使用Django 1.7。
您的函数应该在模型之前。并且应该返回一个字典
def get_limit_choices_to():
return {'entry':Model2.objects.get number = 1).id}
class Model1(models.Model):
entry = models.ForeignKey(Model2,limit_choices_to = get_limit_choices_to)
number = IntegerField()
class Model2(models.Model):
number = IntegerField()
I am trying to limit the choices of a foreign field to those other objects who look like the object self.
I've tried this:
class Model1(models.Model):
entry = models.ForeignKey(Model2, limit_choices_to='get_limit_choices_to')
number = IntegerField()
def get_limit_choices_to(self):
return Model2.objects.filter(expenditure_type=self.expenditure_type)
class Model2(models.Model):
number = IntegerField()
but I get the error
_filter_or_exclude() argument after ** must be a mapping, not str
I don't know if limit_choices_to is the right way to do this. Maybe I should choose the queryset in a or the views.
The error says that limit_choices_to='get_limit_choices_to'
is a wrong way to refer to the method, but how can I refer to the method correctly? I can't use
limit_choices_to=lambda: {'model1_set': self}
nor
limit_choices_to=lambda: {'number': number}
I am using Django 1.7.
Your function should be before the model. And should return an dictionary
def get_limit_choices_to():
return {'entry': Model2.objects.get(number=1).id}
class Model1(models.Model):
entry = models.ForeignKey(Model2, limit_choices_to=get_limit_choices_to)
number = IntegerField()
class Model2(models.Model):
number = IntegerField()
这篇关于选择基于对象字段的limit_choices_to的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!