Код: Выделить всё
class Value(models.Model):
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, blank=True, null=True)
val_id = models.PositiveIntegerField(blank=True, null=True)
data_obj = GenericForeignKey('content_type', 'val_id')
< /code>
Value
Код: Выделить всё
class Int(models.Model):
data = models.IntegerField(blank=True, null=True)
< /code>
So, after creating an instance of Int< /code>: < /p>
myint = Int.objects.create(data=1)
< /code>
I can create a Value< /code> Это указывает на это: < /p>
myval = Value.objects.create(data_obj=myint)
< /code>
There are other similar models that a Value
Код: Выделить всё
Value.objects.filter(data_obj__data=1)
< /code>
But this results in an error:
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 790, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 808, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1243, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1269, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1149, in build_filter
lookups, parts, reffed_expression = self.solve_lookup_type(arg)
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1035, in solve_lookup_type
_, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1316, in names_to_path
"adding a GenericRelation." % name
FieldError: Field 'data_obj' does not generate an automatic reverse relation and therefore cannot be used for reverse querying. If it is a GenericForeignKey, consider adding a GenericRelation.
< /code>
The problem is that even though I've updated each data model to have a GenericRelation field back to the Value< /code> Модель: < /p>
class Int(models.Model):
data = models.IntegerField(blank=True, null=True)
val_obj = GenericRelation(Value, object_id_field="val_id")
class UInt(models.Model):
data = models.PositiveIntegerField(blank=True, null=True)
val_obj = GenericRelation(Value, object_id_field="val_id")
class String(models.Model):
data = models.CharField(max_length=2048)
val_obj = GenericRelation(Value, object_id_field="val_id")
# etc.
< /code>
I still can't get reverse querying to work and get the same error from above. What am I doing wrong here that adding GenericRelation still does not allow for reverse querying or seem to change anything at all? I would definitely like the reverse querying to work because the project has a filtering script that would be much easier to make work on Values
< /code>
And according to the Django documentation, I believe I would then reverse query like this:
Value.objects.filter(val__data=1)
< /code>
Yet, even though I definitely have data models whose data field contains the int 1, this and any other reverse querying returns an empty list. Is there something else I'm doing wrong/missing?
Подробнее здесь: https://stackoverflow.com/questions/365 ... ericforeig