#24156: inherited manytomany.all() empty when 2 or more inherited model from
abstract one
-------------------------------------+-------------------------------------
Reporter: Pawamoy | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: inherit asbtract | Triage Stage: Accepted
manytomanys |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by coldmind):
* version: 1.7 => master
Comment:
I faced with this issue too.
My example:
{{{
class ModelA(models.Model):
field1 = models.TextField(blank=True)
class AbstrMdl(models.Model):
class Meta:
abstract = True
field1 = models.ManyToManyField('ModelA', related_name='+')
class ModelB(AbstrMdl):
field2 = models.TextField(blank=True)
class ModelC(AbstrMdl):
field2 = models.TextField(blank=True)
---
In [1]: from myapp.models import ModelA, ModelB
In [2]: a = ModelA.objects.create(field1='bla bla')
In [3]: b = ModelB.objects.create()
In [4]: b.field1.all()
Out[4]: []
In [5]: b.field1.add(a)
In [6]: b.field1.all()
Out[6]: []
}}}
Data is saving to database, but queryset is empty because it is filtered
by something like `+__in=`.
Investigation showed me that problem is near
`django.db.models.fields.related.RelatedField#related_query_name`.
For example this diff fixed my issue, but some tests are failing, so I
will investigate deeper.
{{{
diff --git a/django/db/models/fields/related.py
b/django/db/models/fields/related.py
index 4569037..345a83c 100644
--- a/django/db/models/fields/related.py
+++ b/django/db/models/fields/related.py
@@ -371,7 +371,10 @@ class RelatedField(Field):
Define the name that can be used to identify this related object
in a
table-spanning query.
"""
- return self.remote_field.related_query_name or
self.remote_field.related_name or self.opts.model_name
+ if self.remote_field.is_hidden():
+ return self.remote_field.related_query_name or
self.opts.model_name
+ else:
+ return self.remote_field.related_query_name or
self.remote_field.related_name or self.opts.model_name
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24156#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.d12341d24e2e5e0e9970e6994f8460ab%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.