Reputation: 637
I have a problem. I'm trying to change the directory on which a file gets uploaded, using the upload_to attribute of a FileField, without success.
The main issue is that I have defined a parent class, with a file attribute, and I want to change the directory on the child class.
My models are defined in this way :
class DocumentBase(models.Model):
file = models.FileField(upload_to=get_filename)
class Document(DocumentBase):
file_type = models.CharField(max_size=150)
I tried to overwrite the FileField in the child class, without success (I'm aware now that this is not possible.)
I also tried the answers on this other question (that is very similar to my problem), without success.
Could somebody help me with this? Thanks!
Upvotes: 0
Views: 824
Reputation: 637
As Willem said this can be solved monkey patching the upload_to
attribute. But it didn't work for this case.
Digging into the FileField class definition, this class has another attribute: generate_filename
. This attribute is filled when upload_to
is callable.
This attribute is used to generate the file filename.
So, the solution that worked was :
class DocumentBase(models.Model):
file = models.FileField(upload_to=get_filename)
class Document(DocumentBase):
file_type = models.CharField(max_size=150)
Document._meta.get_field('file').generate_filename = other_get_filename
This behavior changes on Django 1.10.
Upvotes: 1