我正在运行Django 1.1,无法让我的ManytoManyField工作时使用“limit_choices_to”选项。Django:“limit_choices_to”在ManyToManyField上不起作用
我有两个型号:
class MemberPhoto(ImageModel):
title = models.CharField(_('title'), max_length=255, blank=True, null=True)
caption = models.CharField(_('caption'), max_length=255, blank=True, null=True)
date_added = models.DateTimeField(_('date added'), default=datetime.now, editable=False)
member = models.ForeignKey(User)
def __unicode__(self):
return u'%s (%s)' % (self.member.username, self.id)
和
class lock(models.Model):
user = models.ForeignKey(User, related_name="owner")
to_user = models.ForeignKey(User, related_name="to_user")
unlocked_photos = models.ManyToManyField(MemberPhoto, blank=True, null=True, limit_choices_to = {'member':'user'})
objects = locking_manager()
在第二个模型
,我要确保在Django的管理,唯一的 “unlocked_photos”( “MemberPhoto” 对象)介绍在多选字段中,那些拥有与“锁”对象的“用户”(也是用户对象)相同的“成员”值(用户对象)的用户。
我以为我跟着Django文档,但它不起作用。我得到以下错误:
TemplateSyntaxError
Caught an exception while rendering: invalid input syntax for integer: "user"
我试图改变 “limit_choices_to” 喜欢的东西:
limit_choices_to = { '成员':用户} ---不工作
limit_choices_to = {'member__username':'kyle'} ---这样做可以,但是没用,我只是手动指定用户名
我该如何让用户从当前的“锁定”对象中过滤掉MemberPhoto“成员“的财产呢?
感谢任何人的帮助。
凯尔
你可以使用管理员模式的form_for_foreignkey方法也都做到了。 ;) –
您可能需要将该赋值放在try块中以满足过滤器值尚不存在的创建用例。 –