Thursday, May 3, 2018

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

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

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/d436a98f-19ec-4c15-a504-688cb49923a9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment