#32279: Is this a bug with the ManyToMany self referencing field?
-----------------------------------------+------------------------
Reporter: Conrad | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
I asked this on [stack
overflow](https://stackoverflow.com/questions/65338714/why-doesnt-this-
script-add-my-structure-correctly-to-django), but got no answers, so I
think it might be a bug?
I encountered a pretty weird problem with a ManyToManyField in Django
today. When automatically reading input data from a dict the ManyToMany
field gets populated falsely. My model is for this example pretty simple
the UnitType has a name and 4 ManyToMany fields, one of them beeing
`possible_children = models.ManyToManyField('self',
related_name='UnitTypeChildren', blank=True)`. When I run my management
command which I set up for this minimal example I run (subtypes is
truncutaded because there are a lot of values and everything with this
works):
{{{
building_subtypes = {
'garage/ parking': {'subtypes': [...]},
'room': {'subtypes': [...], 'child': ['room']},
'apartment': {'subtypes': [...]},
'floor': {'subtypes': [...], 'child': ['room', 'apartment', 'garage/
parking']},
'building': {'subtypes': [...], 'child': ['room', 'floor',
'apartment', 'garage/ parking']},
'address': {'subtypes': [...], 'child': ['building', 'apartment',
'garage/ parking']},
'business': {'subtypes': [...]},
'other': {'subtypes': [...]}
}
for typ in building_subtypes:
ut, new = UnitType.objects.get_or_create(name=typ)
for subtype in building_subtypes[typ]['subtypes']:
UnitSubType.objects.get_or_create(name=subtype, unit_type=ut)
if 'child' in building_subtypes[typ]:
for child in building_subtypes[typ]['child']:
child_ut, new = UnitType.objects.get_or_create(name=child)
print(subtype.name + ' ' + child_ut.name) # Prints
correctly
ut.possible_children.add(child_ut)
# Check if it worked
for ut in UnitType.objects.all():
childstring = ""
for child in ut.possible_children.all():
childstring += str(child) + ', '
print(ut.name + ': ' + childstring) # Prints alternate structure
}}}
When accessing Django admin after this script the structure is not at all
what I intended or expected but seemingly random. I tried reading the data
in from a different dict, reordering the dict to avoid dependency issues,
adding `null=True` to the field, because of the values which are populated
but shouldn't be. Furthermore I tried saving the object after each add and
after all adds but the results are the same. Right now I'm a bit confused.
It's not that I couldn't do it another way, it's more that I am asking
myself why this occurs?
The output is as follows:
{{{
room room
floor room
floor apartment
floor garage/ parking
building room
building floor
building apartment
building garage/ parking
address building
address apartment
address garage/ parking
garage/ parking: floor, building, address,
room: room, floor, building,
apartment: floor, building, address,
floor: garage/ parking, room, apartment, building,
building: garage/ parking, room, apartment, floor, address,
address: garage/ parking, apartment, building,
business:
other:
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32279>
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/048.cf07a7e616b554327f6cb78c7e23f7ae%40djangoproject.com.