Opened 8 years ago
Closed 8 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:
- Add two
Model1items via admin - Add one
Model2item via admin, choose one instance ofModel1for many-to-many field - Edit the
Model2item via admin choosing bothModel1instances for many-to-many field - Open history for
Model2instance - "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.
Duplicate of #27998