Opened 2 years ago

Closed 2 years ago

#28301 closed Bug (duplicate)

Object history is not logged for many to many fields

Reported by: Karolis Ryselis Owned by: nobody
Component: contrib.admin Version: 1.11
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

Consider this models.py:

from django.db import models


class Model1(models.Model):
    title = models.CharField(max_length=20)

    def __str__(self):
        return self.title


class Model2(models.Model):
    title = models.CharField(max_length=20)
    models1 = models.ManyToManyField(to="Model1")

    def __str__(self):
        return self.title

and this admin.py

from django.contrib import admin

from .models import Model1, Model2

admin.site.register(Model1)
admin.site.register(Model2)

My test scenario:

  1. Add two Model1 items via admin
  2. Add one Model2 item via admin, choose one instance of Model1 for many-to-many field
  3. Edit the Model2 item via admin choosing both Model1 instances for many-to-many field
  4. Open history for Model2 instance
  5. "No fields changed." is in history even though we have changed one of the fields.

I guess that this could be caused by lazy evaluation of initial queryset.
Initial data is loaded from model instance using model_to_dict which calls f.value_from_object for each field. ManyToManyField implemention returns a queryset. It think it is only evaluated when object history messages are constructed, i.e., after object is saved to database, and queryset returns already saved values instead of initial values. Because of this, initial and saved data are always the same and this field never appears in changed_data.
I was able to reproduce this on 1.11.2.

Change History (1)

comment:1 Changed 2 years ago by Tim Graham

Resolution: duplicate
Status: newclosed

Duplicate of #27998

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