[Django] #24156: inherited manytomany.all() empty when 2 or more inherited model from asbtract one

19 views
Skip to first unread message

Django

unread,
Jan 15, 2015, 12:08:34 PM1/15/15
to django-...@googlegroups.com
#24156: inherited manytomany.all() empty when 2 or more inherited model from
asbtract one
-------------------------------------+-------------------------------------
Reporter: Pawamoy | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7
(models, ORM) | Keywords: inherit asbtract
Severity: Normal | manytomanys
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
If you set a ManyToMany field in an abstract model, and then you use this
model as inheritance for 2 or more sub-models (non-abstracts), you will
not be able to use submodel_instance.manytomany_field.all() anymore
(actually it will work but return an empty list).

Here is an example of code producing this "bug":

{{{
from django.db import models


class Something(models.Model):
name = models.CharField('something', max_length=255)

def __unicode__(self):
return self.name


class AbstractModel(models.Model):
manythings = models.ManyToManyField(
Something, verbose_name='manythings', related_name='+',
blank=True, null=True)

class Meta:
abstract = True


class BugModel1(AbstractModel):
custom1 = models.CharField('custom_field', max_length=255)


class BugModel2(AbstractModel):
custom2 = models.CharField('custom_field', max_length=255)
}}}

When you add a Something instance to the manythings field of a BugModelN
instance, it is correctly added (I was able to confirm this by trying to
delete the object through admin interface: it lists the foreign key
related object that will be deleted on cascade), but when you want to get
it through the all() method of the manytomany field, it returns an empty
list.

I am not sure if it is a bug since I was not able to get any raised error
or else through debug. Maybe a voluntary behavior or a forgotten use case?

(Django 1.7.3)

--
Ticket URL: <https://code.djangoproject.com/ticket/24156>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 3, 2015, 11:41:15 AM3/3/15
to django-...@googlegroups.com
#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: 1.7
(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 timgraham):

* needs_docs: => 0
* needs_better_patch: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


Comment:

Seems like a bug.

--
Ticket URL: <https://code.djangoproject.com/ticket/24156#comment:1>

Django

unread,
May 5, 2015, 1:42:25 PM5/5/15
to django-...@googlegroups.com
#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

unread,
May 5, 2015, 1:42:32 PM5/5/15
to django-...@googlegroups.com
#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):

* cc: me@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/24156#comment:3>

Django

unread,
May 9, 2015, 7:04:36 AM5/9/15
to django-...@googlegroups.com
#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
-------------------------------------+-------------------------------------

Comment (by coldmind):

In fact, list is not empty.
In my example it get empty list because `ModelB` results are empty.
That means that if I will inherit 2 or more models from abstract one,
`ModelA` and `ModelB` will have the result of `ModelC` queryset.

--
Ticket URL: <https://code.djangoproject.com/ticket/24156#comment:4>

Django

unread,
May 9, 2015, 7:05:35 AM5/9/15
to django-...@googlegroups.com
#24156: inherited manytomany.all() empty when 2 or more inherited model from
abstract one
-------------------------------------+-------------------------------------
Reporter: Pawamoy | Owner: coldmind
Type: Bug | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: inherit asbtract | Triage Stage: Accepted
manytomanys |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by coldmind):

* status: new => assigned
* owner: nobody => coldmind
* has_patch: 0 => 1


Comment:

https://github.com/django/django/pull/4631

--
Ticket URL: <https://code.djangoproject.com/ticket/24156#comment:5>

Django

unread,
May 12, 2015, 3:40:58 PM5/12/15
to django-...@googlegroups.com
#24156: inherited manytomany.all() empty when 2 or more inherited model from
abstract one
-------------------------------------+-------------------------------------

Reporter: Pawamoy | Owner: coldmind
Type: Bug | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: inherit asbtract | Triage Stage: Ready for
manytomanys | checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/24156#comment:6>

Django

unread,
May 12, 2015, 7:16:28 PM5/12/15
to django-...@googlegroups.com
#24156: inherited manytomany.all() empty when 2 or more inherited model from
abstract one
-------------------------------------+-------------------------------------
Reporter: Pawamoy | Owner: coldmind
Type: Bug | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: inherit asbtract | Triage Stage: Ready for
manytomanys | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"f7b297815819153b53dc1125d3f42869fb1b7ebc" f7b29781]:
{{{
#!CommitTicketReference repository=""
revision="f7b297815819153b53dc1125d3f42869fb1b7ebc"
Fixed #24156 -- Fixed inherited related name of ManyToManyField.

Fixed situation when parent abstract model declares related_name='+',
and child models had an invalid queryset.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24156#comment:7>

Django

unread,
Oct 13, 2015, 7:18:30 AM10/13/15
to django-...@googlegroups.com
#24156: inherited manytomany.all() empty when 2 or more inherited model from
abstract one
-------------------------------------+-------------------------------------

Reporter: Pawamoy | Owner: coldmind
Type: Bug | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: inherit asbtract | Triage Stage: Ready for
manytomanys | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"eb85e6672aeb00c67a3666785965edeceed976af" eb85e667]:
{{{
#!CommitTicketReference repository=""
revision="eb85e6672aeb00c67a3666785965edeceed976af"
[1.8.x] Fixed #24156 -- Fixed inherited related name of ManyToManyField.

Fixed situation when parent abstract model declares related_name='+'

and child models had an invalid queryset.

Backport of f7b297815819153b53dc1125d3f42869fb1b7ebc from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24156#comment:8>

Reply all
Reply to author
Forward
0 new messages