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

Regression in a93425a37f.
This commit is contained in:
Matthias Kestenholz 2020-07-23 01:38:31 +02:00 committed by Mariusz Felisiak
parent 41065cfed5
commit c1f8d87bb0
2 changed files with 24 additions and 1 deletions

View File

@ -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,

View File

@ -8,8 +8,9 @@ from pathlib import Path
from django.core.files import File, temp
from django.core.files.base import ContentFile
from django.core.files.uploadedfile import TemporaryUploadedFile
from django.db import IntegrityError
from django.db import IntegrityError, models
from django.test import TestCase, override_settings
from django.test.utils import isolate_apps
from .models import Document
@ -147,3 +148,21 @@ class FileFieldTests(TestCase):
self.assertEqual(document.myfile.field, loaded_myfile.field)
finally:
document.myfile.delete()
@isolate_apps('model_fields')
def test_abstract_filefield_model(self):
"""
FileField.model returns the concrete model for fields defined in an
abstract model.
"""
class AbstractMyDocument(models.Model):
myfile = models.FileField(upload_to='unused')
class Meta:
abstract = True
class MyDocument(AbstractMyDocument):
pass
document = MyDocument(myfile='test_file.py')
self.assertEqual(document.myfile.field.model, MyDocument)