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)




On Thursday, May 3, 2018 at 6:59:17 PM UTC-6, James Farris wrote:
>
> 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)
>
> On Thu, May 3, 2018 at 3:20 PM Alexander Joseph <alexander...@gmail.com 
> <javascript:>> wrote:
>
>> 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?
>>
>>
>>
>>
>> On Thursday, May 3, 2018 at 2:50:02 PM UTC-6, Alexander Joseph wrote:
>>>
>>> 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...@googlegroups.com <javascript:>.
>> To post to this group, send email to django...@googlegroups.com 
>> <javascript:>.
>> 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
>>  
>> <https://groups.google.com/d/msgid/django-users/8173dbcc-8870-44d1-9796-e22b465144e1%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
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+unsubscr...@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/f11cca2a-d80a-445c-bd2d-446da0a23438%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to