Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: ForeignKey limit_choices_to to equal current object's client

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

What should I put in 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:

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'
like image 833
la_f0ka Avatar asked Nov 27 '12 17:11

la_f0ka


1 Answers

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

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)
like image 165
Seth Avatar answered Oct 22 '22 10:10

Seth