Hi,
If you look at the documentation for the upload_to parameter, you can see
that you can do exactly what you want:
https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.FileField.upload_to
The upload_to method (in your case "get_user_document_directory") can take
2 parameters:
ArgumentDescription
instance
An instance of the model where the FileField is defined. More specifically,
this is the particular instance where the current file is being attached.
In most cases, this object will not have been saved to the database yet, so
if it uses the default AutoField, *it might not yet have a value for its
primary key field*.
filename The filename that was originally given to the file. This may or
may not be taken into account when determining the final destination path.
So you don't need to add "self", you get that automatically. All you need
to do is change the method to:
def get_user_document_directory(instance, filename):
return "media/{0}/{1}/documents/{2}".format(instance.bundle.user,
instance.bundle.name, filename)
You have to remember to include the filename as well (it's not only the
directory but the entire path).
Regards,
Andréas
2018-01-08 21:34 GMT+01:00 <[email protected]>:
> Hey Friends,
>
> I'm having trouble figuring out how to upload a document model to a
> dynamically generated location. The following models.py file contains two
> models and a helper function
>
>
> views.py
>
>
> # I would like the string inputs to be self.bundle.user, and
> self.bundle.name where self is the document model. I know this doesn't
> work which is why I am here asking this question.
> def get_user_document_directory():
> document_directory = 'media/{0}/{1}/documents/'.format( ____, _____ )
> return document_directory
>
> class Bundle(models.Model):
> user = models.ForeignKey(User)
> name = models.CharField(max_length=100)
>
> class Document(models.Model):
> bundle = models.ForeignKey(Bundle)
> name = models.CharField(max_length=100)
> description = models.CharField(max_length=100)
> document = models.FileField(upload_to=get_user_document_directory(self
> ) ) # I know passing self doesn't work but just trying to get the point
> across of what I am trying to do.
> uploaded_at = models.DateTimeField(auto_now_add=True)
>
>
>
> Any comments about how to remedy this situation would be greatly
> appreciated.
>
> Thanks,
> K
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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/71125202-9b89-4572-a086-ab9fed2b4d01%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/71125202-9b89-4572-a086-ab9fed2b4d01%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 [email protected].
To post to this group, send email to [email protected].
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/CAK4qSCcS22DVXy%2B3R-jw1v1-JrXYP7kbXWpOmKV5OQKq1z%3DhVg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.