Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#14529 closed (fixed)

Save messages for proxy models show a crazy verbose_name

Reported by: rlaager@… Owned by: nobody
Component: contrib.admin Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by ramiro)

Steps to reproduce:

  1. In a ModelAdmin, provide a custom queryset method that adds .only().
  2. Save a model in the admin.

Expected results:

A

The model name "whatever" was saved successfully.

message.

Actual results:

A

The model_name_deferred__all_the_fields_you_did_not_load "whatever" was saved successfully.

message.

Attachments (2)

14529-save-messages-for-proxy-models.diff (597 bytes) - added by rlaager@… 5 years ago.
A trivial patch to fix this issue
14529.1.diff (1.5 KB) - added by ramiro 4 years ago.
Another similar patch but with reduced impact.

Download all attachments as: .zip

Change History (10)

Changed 5 years ago by rlaager@…

A trivial patch to fix this issue

comment:1 Changed 5 years ago by Alex

  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset

comment:2 Changed 4 years ago by rasca

  • milestone set to 1.3
  • Patch needs improvement set
  • Triage Stage changed from Unreviewed to Accepted
  • Version changed from 1.2 to SVN

Okay, I can confirm this happens in 1.2 and in trunk.
I've applied the patch but it solves this partially. Also, I'm not sure that's the best approach.

When you don't define a __unicode__() it shows "whatever"_Deferred_"fields" object in the changelist's list always.

comment:3 Changed 4 years ago by julien

Here's a simple test case:

Model:

from django.db import models

class Book(models.Model):
    name = models.CharField(max_length=100)
    author = models.CharField(max_length=100, blank=True)

Admin:

from django.contrib import admin

from .models import Book

class BookAdmin(admin.ModelAdmin):
    list_display = ('name',)
    
    def queryset(self, request):
        return super(BookAdmin, self).queryset(request).only('name')

admin.site.register(Book, BookAdmin)

Narrowing down the issue with print statements in django.contrib.admin.options:

    def change_view(self, request, object_id, extra_context=None):
        ...
                print "Before:" + force_unicode(opts.verbose_name)
                return self.response_change(request, new_object)
        ...

    def response_change(self, request, obj):
        """
        Determines the HttpResponse for the change_view stage.
        """
        opts = obj._meta
        print "After:" + force_unicode(opts.verbose_name)
        ...

This results in:

Before:book
After:book_ deferred_author

So apparently the issue occurs when the meta options are reloaded in response_change() with opts = obj._meta.

Note that the issue only arises with the change view. With the add view, the correct verbose names are displayed.

comment:4 Changed 4 years ago by ramiro

  • Description modified (diff)

(reformatted description)

Changed 4 years ago by ramiro

Another similar patch but with reduced impact.

comment:5 Changed 4 years ago by ramiro

  • Patch needs improvement unset

The new patch is similar to the one uploaded by rlaager but it only uses the model verbose name of the proxied model when the model associated with the ModelAdmin is an synthetic proxy model originated from the use of .only() or .defer().

It still needs tests.

comment:6 Changed 4 years ago by ramiro

  • Resolution set to fixed
  • Status changed from new to closed

In [15596]:

Fixed #14529 -- Fixed representation of model names in admin messages after model object changes when the ModelAdmin queryset() uses defer() or only(). Thanks rlaager for report and initial patch, to rasca an julien for help in tracking the problem.

comment:7 Changed 4 years ago by ramiro

In [15597]:

[1.2.X] Fixed #14529 -- Fixed representation of model names in admin messages after model object changes when the ModelAdmin queryset() uses defer() or only(). Thanks rlaager for report and initial patch, to rasca an julien for help in tracking the problem.

Backport of [15596] from trunk.

comment:8 Changed 4 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

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