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:
- Give your model a ForeignKey to ContentType. The usual name for this
field is “content_type”.
- 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”.
- 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