S
S
Shyngys Sailaubai2018-03-26 16:16:06
Django
Shyngys Sailaubai, 2018-03-26 16:16:06

How to pass parameter to Django model?

Good day.
Available model

class Image(models.Model)
  name = models.ImageField(upload_to='относительный каталог для файлов/', blank=True, verbose_name='подпись')

I would like to scatter the images in different directories (mandatory). How to pass this very relative path to the directory to this model? Naming a directory by time is not an option. To generate from a name of a file too not a variant. It should be exactly the string that I will pass when saving the model.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2018-03-26
@chyngys_94

https://docs.djangoproject.com/en/2.0/ref/models/f...

def content_file_name(instance, filename):
    return '/'.join(['content', instance.user.username, filename])

class Content(models.Model):
    name = models.CharField(max_length=200)
    user = models.ForeignKey(User)
    file = models.FileField(upload_to=content_file_name)

R
Roman Kitaev, 2018-03-26
@deliro

sacred knowledge

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question