Reputation: 19
I've been reading the django documentation and I'm trying to create a set of choices. Depending on which choice a user chooses, a file will save in a different folder. Though I can't seem to find a way for this to work.
I've currently got this as my model:
from django.db import models
class Document(models.Model):
Name = models.CharField(max_length=25, blank=True)
Week_1 = 'Week1'
Week_2 = 'Week2'
Week_3 = 'Week3'
Week_4 = 'Week4'
Weekly_Choices = (
(Week_1, 'Week_1'),
(Week_2, 'Week_2'),
(Week_2, 'Week_3'),
(Week_2, 'Week_4')
)
Week = models.CharField(max_length=10, choices=Weekly_Choices, default=Week_1, blank=False)
docfile = models.FileField()
if Week.choices == Week_1:
docfile.upload_to = 'documents/'+ Week_1 + '/' + 'Mentee'
Though, I don't know why this doesn't work - sorry I'm still a bit new to Django and Python.
I've looked into it more and I know there is a Model.get_FOO_display() function but that's not what im looking for. In addition I also looked at django-choices, though the 'get_choice' function outputs a dictionary type. I was hoping there might be an easier way which im missing?
Any help would be useful - thanks :D
Upvotes: 0
Views: 74
Reputation: 59
You can try :
def choices_location(instance, filename):
if instance.week == 'Week_1':
return os.path.join('documents', 'Week_1','Mentee', filename)
elif instance.week == Week_2:
return os.path.join('documents', 'Week_2','Mentee', filename)
docfile = models.FileField(upload_to=choices_location)
This will work for you !
Upvotes: 2
Reputation: 308
it won't work unless you give a upload_to field in your modal field docfile
try this
docfile = models.FileField(upload_to=location)
def location(instance, filename):
if instance.Week == Week_1:
return 'documents/'+ Week_1 + '/' + 'Mentee' + filename
hope this helps you out
Upvotes: 0