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.
* 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>
* 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>
* cc: me@… (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/24156#comment:3>
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>
* 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>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/24156#comment:6>
* 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>
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>