Replies: 3 comments
-
|
I'm pretty sure this isn't an issue that's specific to django-polymorphic https://docs.djangoproject.com/en/4.2/ref/models/fields/#django.db.models.ForeignKey.related_query_name Does this fix your issue? Could this issue be closed? |
Beta Was this translation helpful? Give feedback.
-
|
I honestly don't remember what I was trying to do at the time. Does this kind of self-reference work now? |
Beta Was this translation helpful? Give feedback.
-
|
@AstraLuma This is not really a problem with django-polymorphic. In Django multi-table model inheritance a pointer is stored as a OneToOne relation from children to parents. By default django uses the lowercased name of the child model for this relation from the parent to the child. When you add a ManyToMany to the child Django also uses the lowercase name of the child model as the reverse relation on the parent model. The error you are seeing is Django's system check system identifying the name clash. To fix it, you simply need to add a related_name to the spam field: class Parent(PolymorphicModel):
field = models.CharField(max_length=20)
class Child(Parent):
spam = models.ManyToManyField(Parent, related_name="spams")
# You would then be able to access related Child rows from the Parent like so:
Parent.objects.first().spams.all() |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Beta Was this translation helpful? Give feedback.
All reactions