Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
74 views
in Technique[技术] by (71.8m points)

python - Rename content_type and object_id in ContentTypeFramework

I want to create specific relation in my Django app. For doing this, I've chosen ContentTypeFramework and GenericForeignKey. In the django documentation there are not so much info about it. Here is it:

There are three parts to setting up a GenericForeignKey:

  1. Give your model a ForeignKey to ContentType. The usual name for this field is “content_type”.
  2. Give your model a field that can store primary key values from the models you’ll be relating to. For most models, this means a PositiveIntegerField. The usual name for this field is “object_id”.
  3. Give your model a GenericForeignKey, and pass it the names of the two fields described above. If these fields are named “content_type” and “object_id”, you can omit this – those are the default field names GenericForeignKey will look for.

And it works great if I left fields with default names:

content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.UUIDField(null=True, blank=True)
item = GenericForeignKey('content_type', 'object_id')

But if I change their names, for something more specific (just custom field names) e.g:

item_ct = models.ForeignKey(ContentType, on_delete=models.CASCADE)
item_id = models.UUIDField(null=True, blank=True)
item = GenericForeignKey('item_ct', 'item_id')

My Application tests fails with traceback:

  ..................
  File "/usr/local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1021, in add_annotation
    annotation = annotation.resolve_expression(self, allow_joins=True, reuse=None,
  File "/usr/local/lib/python3.8/site-packages/django/db/models/expressions.py", line 625, in resolve_expression
    c.source_expressions[pos] = arg.resolve_expression(query, allow_joins, reuse, summarize, for_save)
  File "/usr/local/lib/python3.8/site-packages/django/db/models/aggregates.py", line 47, in resolve_expression
    c = super().resolve_expression(query, allow_joins, reuse, summarize)
  File "/usr/local/lib/python3.8/site-packages/django/db/models/expressions.py", line 625, in resolve_expression
    c.source_expressions[pos] = arg.resolve_expression(query, allow_joins, reuse, summarize, for_save)
  File "/usr/local/lib/python3.8/site-packages/django/db/models/expressions.py", line 532, in resolve_expression
    return query.resolve_ref(self.name, allow_joins, reuse, summarize, simple_col)
  File "/usr/local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1685, in resolve_ref
    join_info = self.setup_joins(field_list, self.get_meta(), self.get_initial_alias(), can_reuse=reuse)
  File "/usr/local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1567, in setup_joins
    path, final_field, targets, rest = self.names_to_path(
  File "/usr/local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1496, in names_to_path
    pathinfos = field.get_path_info(filtered_relation)
  File "/usr/local/lib/python3.8/site-packages/django/contrib/contenttypes/fields.py", line 399, in get_path_info
    object_id_field = opts.get_field(self.object_id_field_name)
  File "/usr/local/lib/python3.8/site-packages/django/db/models/options.py", line 583, in get_field
    raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: Record has no field named 'object_id'

So, has anyone faced with the same problems, maybe anyone knows how to fix them?

question from:https://stackoverflow.com/questions/65832421/rename-content-type-and-object-id-in-contenttypeframework

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...