Django 通用关系字段报告当没有传递参数时 all() 收到意外的关键字参数

发布于 2024-08-30 00:07:51 字数 2686 浏览 3 评论 0原文

我有一个可以附加到其他模型的模型。

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()

我正在另一个模型中创建与此模型的通用关系。

flags = generic.GenericRelation(Flag)

我尝试从这个通用关系中获取对象,如下所示:

self.flags.all()

这会导致以下异常:

>>> 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'

我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉墨 2024-09-06 00:07:51

创建GenericRelation时需要定义object_id_fieldcontent_type_field

flags = generic.GenericRelation(Flag, object_id_field="object_pk", content_type_field="content_type")

You need to define object_id_field and content_type_field when creating GenericRelation:

flags = generic.GenericRelation(Flag, object_id_field="object_pk", content_type_field="content_type")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文