Login

Tag "upload_to"

Snippet List

Automatic Folder FileField

Your MEDIA_ROOT directories are a mess? FileField save on "upload_to" directories with old/strange/temporary names decided "on the fly" and never fixed down? SmartFolderFileField is the solution! "upload_to" directory depends only on: app, model and field names. No mess, no ambiguities Obviously, in case you need a real callable for a dynamic directory name: please use it! and leave apart FixedFolderFileField Sample: from django.db import models from utilities_app.models import SmartFolderFileField class SampleModel(models.Model): sample_char_field = models.CharField(max_length=50) sample_file_field = SmartFolderFileField()

  • upload_to
  • FileField
Read More

slug filename

A one-liner that I use all the time: Set `upload_to` to something based on the slug and the filename's extension. Just add this function to the top of your models and use it like this: image = models.FileField(upload_to=slug_filename('people')) and the `upload_to` path will end up like this for eg `myfile.jpg`: people/this-is-the-slug.jpg Enjoy!

  • upload_to
Read More

Use the primary key in FileField and ImageField filenames

Sometimes it is desirable to use values like the primary key when naming `FileField` and `ImageField` files, but such values are only available after saving the model instance. This abstract class implements a two-phase save in order to make this case easy. See the example in the docstring. Another solution would be to write a `save()` that requires `upload_to` to be a callable that checks for `instance.pk`, then calls it again after saving. However, this would require more work from the developer for simple cases.

  • imagefield
  • filefield
  • rename
  • upload_to
Read More

3 snippets posted so far.