Django generic relation field reports that all() is getting unexpected keyword argument when no args

Posted by Joshua on Stack Overflow See other posts from Stack Overflow or by Joshua
Published on 2010-04-22T06:39:09Z Indexed on 2010/04/22 6:43 UTC
Read the original article Hit count: 309

Filed under:
|
|

I have a model which can be attached to to other models.

class Attachable(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_pk = models.TextField()
    content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_pk")

    class Meta:
        abstract = True

class Flag(Attachable):
    user = models.ForeignKey(User)
    flag = models.SlugField()
    timestamp = models.DateTimeField()

I'm creating a generic relationship to this model in another model.

flags = generic.GenericRelation(Flag)

I try to get objects from this generic relation like so:

self.flags.all()

This results in the following exception:

>>> obj.flags.all()        
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/manager.py", line 105, in all
    return self.get_query_set()                                                              
  File "/usr/local/lib/python2.6/dist-packages/django/contrib/contenttypes/generic.py", line 252, in get_query_set
    return superclass.get_query_set(self).filter(**query)                                                         
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 498, in filter                    
    return self._filter_or_exclude(False, *args, **kwargs)                                                        
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 516, in _filter_or_exclude        
    clone.query.add_q(Q(*args, **kwargs))                                                                         
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py", line 1675, in add_q                
    can_reuse=used_aliases)                                                                                       
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py", line 1569, in add_filter           
    negate=negate, process_extras=process_extras)                                                                 
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py", line 1737, in setup_joins          
    "Choices are: %s" % (name, ", ".join(names)))                                                                 
FieldError: Cannot resolve keyword 'object_id' into field. Choices are: content_type, flag, id, nestablecomment, object_pk, timestamp, user
>>> obj.flags.all(object_pk=obj.pk)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: all() got an unexpected keyword argument 'object_pk'

What have I done wrong?

© Stack Overflow or respective owner

Related posts about django

Related posts about generic