Django:“limit_choices_to”不适用于ManyToManyField [英] Django: "limit_choices_to" doesn't work on ManyToManyField

查看:476
本文介绍了Django:“limit_choices_to”不适用于ManyToManyField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Django 1.1,无法获取我的ManytoManyField的limit_choices_to选项。

I am running Django 1.1 and cannot get the "limit_choices_to" option for my ManytoManyField to work.

我有两个模型:

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的admin中,在多个选择字段中显示的唯一unlocked_photos(MemberPhoto对象)是具有与lock对象的user(也是User对象)相同的member值(User对象) 。

in the second model, i want to make sure in Django's admin that the only "unlocked_photos" ("MemberPhoto" objects) presented in the multiple select field are those who have a "member" value (a User object) the same as the "lock" object's "user" (also a User object).

我以为我已经遵循了Django文档,但它并不奏效。我收到以下错误:

I thought I had followed the Django docs on this, but it doesn't work. I get the following error:

TemplateSyntaxError

Caught an exception while rendering: invalid input syntax for integer: "user"

我尝试将limit_choices_to更改为:

I've tried changing the "limit_choices_to" to things like:

limit_choices_to = {'member':user} ---不工作

limit_choices_to = {'member': user} --- Doesnt work

limit_choices_to = {'member__username':'kyle' } ---这个工作,但它是无用的,我只是手动指定用户名

limit_choices_to = {'member__username':'kyle'} --- this DOES work but it's useless, i'm just manually specifying a username

我如何可以从当前的锁定对象和过滤器获取用户会员照片会员属性由

How can I instead get the user from the current "lock" object and filter the MemberPhoto "member" property by that?

感谢任何人可以帮助。

凯尔

推荐答案

我发现一个答案,正是在这个链接上达到了我想要的: Django MTMField:limit_choices_to = other_ForeignKeyField_on_same _model?,我在这里发布我的工作代码,让任何人都有同样的问题。看起来,limit_choices_to可能根本无法实现我想要的,而且管理员使用的表单是可以自定义的:

I found an answer that achieves exactly what I wanted at this link: Django MTMField: limit_choices_to = other_ForeignKeyField_on_same_model?, and I'm posting my working code here for anybody having the same problem. It seems from looking around that "limit_choices_to" may simply not be able to achieve what I wanted, and that customizing the form used by the admin is the way to go:

from django.contrib import admin
from django import forms
from gayhop.apps.locking.models import lock
from gayhop.apps.photos.models import MemberPhoto

class LockAdminForm(forms.ModelForm):
  class Meta:
    model = lock

  def __init__(self, *args, **kwargs):
    super(LockAdminForm, self).__init__(*args, **kwargs)
    self.fields['unlocked_photos'].queryset = MemberPhoto.objects.filter(member=self.instance.user)


class LockAdmin(admin.ModelAdmin):
  form = LockAdminForm
  filter_horizontal = ('unlocked_photos',)

django.contrib.admin.site.register(lock, LockAdmin)

所有你需要改变的是: p>

All you have to change is:


  1. 您的模型名称(在上述例子中这是锁)

  2. 模型中的ManyToManyField字段的名称(在上面的例子中是unlocked_photos)

  3. 相关模型(在上面的例子中是MemberPhoto)

  4. 要过滤相关对象的字段的名称(在上面的例子中是member)

  5. 要用于过滤相关对象的字段的值(它将以self.instance开头),然后是该字段的名称,在上面的示例中为user)

  6. 最后确保您的自定义管理表单和管理模型的类名称都匹配。

  1. the name of your model (in the above example it's "lock")
  2. the name of the ManyToManyField field in your model (in the above example it's "unlocked_photos")
  3. the name of the related model (in the above example it's "MemberPhoto")
  4. the name of the field you want to filter related objects by (in the above example it's "member")
  5. the value for the field you want to use to filter related objects by (it will start with "self.instance." and then be the name of the field, in the above example it's "user")
  6. And finally make sure your class names for the custom admin form and admin model all match up.

希望这有助于某人!

这篇关于Django:“limit_choices_to”不适用于ManyToManyField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆