小编Kyl*_*can的帖子

Django:"limit_choices_to"在ManyToManyField上不起作用

我正在运行Django 1.1并且无法获取我的ManytoManyField的"limit_choices_to"选项.

我有两个型号:

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)
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)

在第二个模型中,我想确保在Django的管理员中,在多个选择字段中呈现的唯一"unlocked_photos"("MemberPhoto"对象)是具有"成员"值(用户对象)的那些与"锁定"对象的"用户"(也是一个用户对象).

我以为我已经遵循了Django文档,但它不起作用.我收到以下错误:

TemplateSyntaxError

Caught an exception while rendering: invalid input syntax for integer: "user"
Run Code Online (Sandbox Code Playgroud)

我已经尝试将"limit_choices_to"改为:

limit_choices_to = {'member':user} ---不起作用

limit_choices_to = {'member__username':'kyle'} ---这可行,但它没用,我只是手动指定用户名

我怎样才能从当前的"锁定"对象中获取用户并按此过滤MemberPhoto"member"属性? …

django admin limit-choices-to manytomanyfield

5
推荐指数
2
解决办法
6072
查看次数

标签 统计

admin ×1

django ×1

limit-choices-to ×1

manytomanyfield ×1