Opened 8 years ago

Closed 8 years ago

#26766 closed Bug (worksforme)

Delayed effect of calling set on ManyToManyField

Reported by: jeberger Owned by: nobody
Component: Database layer (models, ORM) Version: 1.9
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When calling set on a ManyToManyField, the effect is not visible in the current object, but is only visible after the object has been re-fetched from the DB:

my_model = MyModel.objects.get (id = 1)

my_model.some_m2m_field.set (some_list)
my_model.save()    # Not really necessary according to the docs

# These two lines should print the same thing, but the second one
# prints an empty list
print some_list
print my_model.some_m2m_field.all()

my_model = MyModel.objects.get (id = 1)
# Now the list is correct
print my_model.some_m2m_field.all()

Change History (1)

comment:1 by Tim Graham, 8 years ago

Resolution: worksforme
Status: newclosed

The behavior you describe seems to contradict the tested behavior.

Can you provide a failing test for Django's test suite that demonstrates the issue? Possibly you're using prefetch_related() similar to what's described in #26706.

Note: See TracTickets for help on using tickets.
Back to Top