How can I use a variable from the instance of my data in the upload path for a FileField in my model?

118 views
Skip to first unread message

Alexander Joseph

unread,
May 3, 2018, 4:50:02 PM5/3/18
to Django users
Not even sure if you can do it this way. I'm thinking theres another way like overriding the save or the form_valid function in the view... but I'm trying to use a variable from my instance in my upload path for my file field. If I should do something like override the save method in the view how do I save that in the "upload_to" in my model? 

Here are my model fields

design_ui = models.CharField(max_length=255, default='')
design_document
= models.FileField(upload_to='uploads/%Y/%m/%d/(Id like to put the instance design_ui here)', blank=True)



and my view

class GaasWaferDesignCreateView(LoginRequiredMixin, CreateView,):
    fields = ("design_ui", "emitting", "contact_location", "optical_power", "design_date", "designer", "design_document", "designer_ui", "in_trash", "inactive_date", "notes")
    model = GaasWaferDesign
    template_name = 'engineering/gaas_wafer_designs/gaas_wafer_design_form.html'

    def form_valid(self, form):
        object = form.save(commit=False)
        object.created_by = self.request.user
        object.save()
        return super(GaasWaferDesignCreateView, self).form_valid(form)

i.e. if the design_ui of the specific record is GWD0001 and the date is 5/3/18 the upload path would be "2018/05/03/GWD0001/"


Thanks again

Alexander Joseph

unread,
May 3, 2018, 6:20:27 PM5/3/18
to Django users
Actually I figured this out... kind of

You have to make a method for assigning the upload path in the model.. something like

def upload_path(instance, filename):
    return os.path.join('uploads/%Y/%m/%d/' + instance.design_ui, filename)

and instance the method in your FileField...

design_document = models.FileField(upload_to=upload_path, blank=True)

but for some reason my %Y/%m/%d/  no longer works. If I take out the  %Y/%m/%d/  it works but if I leave it in it throws an exception. Does anyone know why?

James Farris

unread,
May 3, 2018, 8:59:17 PM5/3/18
to django...@googlegroups.com
I did something very similar in my models.py. I think for the date you have to get the year, month with datetime.

def get_upload_path(self, filename): return 'pictures/{0}/{1}'.format(self.user.username, filename) pet_image = models.ImageField(upload_to=get_upload_path, blank=True)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8173dbcc-8870-44d1-9796-e22b465144e1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexander Joseph

unread,
May 3, 2018, 9:38:11 PM5/3/18
to Django users
Thanks! Heres my method now, it works great...

def upload_path(instance, filename):
    return os.path.join('uploads/' + datetime.datetime.now().strftime('%Y/%m/%d/') + instance.design_ui, filename)


Reply all
Reply to author
Forward
0 new messages