Opened 4 years ago

Closed 4 years ago

#31812 closed Bug (fixed)

The `model` attribute of image fields doesn't point to concrete model.

Reported by: Matthias Kestenholz Owned by: Matthias Kestenholz
Component: Database layer (models, ORM) Version: dev
Severity: Release blocker Keywords:
Cc: Sultan Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In Django 3.1 and before, one could use the model attribute of image fields to find the concrete model the image field belongs to.

This isn't possible in 3.2 anymore, and I bisected the change to the fix of #31701.

I found this while investigating a CI failure of django-imagefield https://travis-ci.org/github/matthiask/django-imagefield/jobs/710794644

I'm not sure whether this is a bug or whether it is an intentional change. If it is the later, is there an alternative to find the concrete model an image field belongs to? I'm classifying this as a bug because the change made model and field introspection harder than it was before. Also, since behavior changed #31701 may possibly not be classified as a cleanup/optimization anymore...

Change History (6)

comment:1 by Mariusz Felisiak, 4 years ago

Cc: Sultan added
Severity: NormalRelease blocker
Summary: Possible unintentional consequence of #31701: The `model` attribute of image fields has changed in 3.2 (master) to reference the abstract modelThe `model` attribute of image fields doesn't point to concrete model.
Triage Stage: UnreviewedAccepted

Thanks for the report! I think we should restore contribute_to_class() but with attname:

diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py
index b064202ee3..e10a5bb6d9 100644
--- a/django/db/models/fields/files.py
+++ b/django/db/models/fields/files.py
@@ -299,6 +299,10 @@ class FileField(Field):
             file.save(file.name, file.file, save=False)
         return file
 
+    def contribute_to_class(self, cls, name, **kwargs):
+        super().contribute_to_class(cls, name, **kwargs)
+        setattr(cls, self.attname, self.descriptor_class(self))
+
     def generate_filename(self, instance, filename):
         """
         Apply (if callable) or prepend (if a string) upload_to to the filename,

Regression in a93425a37f4defdb31d4ca96bb3bf6da21f0b5ce.

Reproduced at f65454801bfa13fc043fee0aca8f49af41380683.

comment:2 by Matthias Kestenholz, 4 years ago

Yes, the proposed fix works for me.

comment:3 by Mariusz Felisiak, 4 years ago

Great, would you like to prepare a patch (with regression tests)?

comment:4 by Matthias Kestenholz, 4 years ago

Sure! Here's a pull request:

PR

comment:5 by Mariusz Felisiak, 4 years ago

Has patch: set
Owner: changed from nobody to Matthias Kestenholz
Status: newassigned
Triage Stage: AcceptedReady for checkin

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In c1f8d87b:

Fixed #31812 -- Fixed FileField.model for fields defined in abstract models.

Regression in a93425a37f4defdb31d4ca96bb3bf6da21f0b5ce.

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