Django:ForeignKey limit_choices_to等于当前对象的客户端

时间:2022-03-01 02:09:59

Given these two models, I want my url FK (in ClientKeyword) to only show the urls whose client equals the current ClientKeyword's client.

鉴于这两个模型,我希望我的URL FK(在ClientKeyword中)只显示其客户端等于当前ClientKeyword客户端的URL。

What should I put in limit_choices_to?

我应该在limit_choices_to中加入什么?

Right now it looks like this:

现在它看起来像这样:

class ClientUrl(models.Model):
    client = models.ForeignKey(UpstreamClientModel)
    url = models.URLField(unique=True, null=False)
    active = models.BooleanField(default=True)

def __unicode__(self):
    return self.url

class Meta:
    verbose_name = 'url'


KEYWORD_TYPES = (
    ('MN', 'Money'),
    ('BR', 'Brand'),
    ('LT', 'Long Tail'),
)

class ClientKeyword(models.Model):
    client = models.ForeignKey(UpstreamClientModel)
    kw_type = models.CharField("keyword type", max_length=2, 
                choices=KEYWORD_TYPES, default='LT')
    keyword = models.CharField(max_length=150, unique=True)
    # TODO: keyword parent ManyToManyField("self")
    url = models.ForeignKey(ClientUrl, limit_choices_to={'client': client})

I could do the migrations and django didn't complaint, but when I try to add a new keyword form the admin interface I get the following traceback:

我可以进行迁移并且django没有抱怨,但是当我尝试从管理界面添加新关键字时,我得到以下回溯:

File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
  366.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  89.         response = view_func(request, *args, **kwargs)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
  196.             return view(request, *args, **kwargs)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  25.             return bound_func(*args, **kwargs)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  21.                 return func(self, *args2, **kwargs2)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/transaction.py" in inner
  209.                 return func(*args, **kwargs)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view
  932.         ModelForm = self.get_form(request)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/contrib/admin/options.py" in get_form
  459.         return modelform_factory(self.model, **defaults)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/forms/models.py" in modelform_factory
  407.     return form_metaclass(class_name, (form,), form_class_attrs)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/forms/models.py" in __new__
  206.                                       opts.exclude, opts.widgets, formfield_callback)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/forms/models.py" in fields_for_model
  164.             formfield = formfield_callback(f, **kwargs)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/contrib/admin/options.py" in formfield_for_dbfield
  107.                 formfield = self.formfield_for_foreignkey(db_field, request, **kwargs)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/contrib/admin/options.py" in formfield_for_foreignkey
  168.         return db_field.formfield(**kwargs)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/fields/related.py" in formfield
  1005.             'queryset': self.rel.to._default_manager.using(db).complex_filter(self.rel.limit_choices_to),
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/query.py" in complex_filter
  660.             return self._filter_or_exclude(None, **filter_obj)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
  642.             clone.query.add_q(Q(*args, **kwargs))
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/sql/query.py" in add_q
  1250.                             can_reuse=used_aliases, force_having=force_having)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/sql/query.py" in add_filter
  1185.                 connector)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/sql/where.py" in add
  69.             value = obj.prepare(lookup_type, value)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/sql/where.py" in prepare
  320.             return self.field.get_prep_lookup(lookup_type, value)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/fields/related.py" in get_prep_lookup
  137.             return self._pk_trace(value, 'get_prep_lookup', lookup_type)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/fields/related.py" in _pk_trace
  210.         v = getattr(field, prep_func)(lookup_type, v, **kwargs)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_lookup
  310.             return self.get_prep_value(value)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
  537.         return int(value)

Exception Type: TypeError at /admin/clients/clientkeyword/add/
Exception Value: int() argument must be a string or a number, not 'ForeignKey'

1 个解决方案

#1


6  

Even better: the more "official" way to do this is to override formfield_for_foreignkey() in your model's admin class.

更好的是:更“官方”的方法是覆盖模型管理类中的formfield_for_foreignkey()。

class ClientKeywordAdmin(admin.ModelAdmin):
    def get_form(self, request, obj=None, **kwargs):
        # store the model instance for later
        request.obj = obj
        return super(ClientKeywordAdmin, self).get_form(request, obj, 
                                                        **kwargs)

    def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
        if db_field.name == 'url':
            # set the query set to whatever you like
            kwargs['queryset'] = request._obj.client.clienturls.all()
        return super(ClientKeywordAdmin, 
                     self).formfield_for_foreignkey(request, obj, **kwargs)

#1


6  

Even better: the more "official" way to do this is to override formfield_for_foreignkey() in your model's admin class.

更好的是:更“官方”的方法是覆盖模型管理类中的formfield_for_foreignkey()。

class ClientKeywordAdmin(admin.ModelAdmin):
    def get_form(self, request, obj=None, **kwargs):
        # store the model instance for later
        request.obj = obj
        return super(ClientKeywordAdmin, self).get_form(request, obj, 
                                                        **kwargs)

    def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
        if db_field.name == 'url':
            # set the query set to whatever you like
            kwargs['queryset'] = request._obj.client.clienturls.all()
        return super(ClientKeywordAdmin, 
                     self).formfield_for_foreignkey(request, obj, **kwargs)