1

I am building a recipe/cookbook application (something like cookpad or allrecipes) for a personal project. I have a model for image like so:

class Image(Model):
    type = (("R", "Recipe"), ("P", "Profile"), 
            ("E", "Embedded"), ("B", "Base"))
    name = models.CharField(max_length=55)
    type = models.CharField(choices=type, max_length=1, default="B")
    image = VersatileImageField(
        upload_to=FileNameGenerator(prefix="images"),
        placeholder_image=OnDiscPlaceholderImage(
            path=join(settings.MEDIA_ROOT, "images", "default.jpg")
        ),
        null=True,
        blank=True,
    )

I want to use one table only to store all image. But store it to different place for each different type.

Example:

  • Base image would be at images/xxx.jpg
  • Profile's image at profiles/xxxx.jpg
  • Recipe's image at recipes/xxx.jpg
  • etc..

The only way I know how, is to make it an abstract model and extend it. But it would create many table (ProfileImage, RecipeImage, etc..)

If I use proxy I cannot change/override the behaviour of image field.

Is there a a way to extend models but save it to the same table in database?

flemadap
  • 639
  • 8
  • 18
  • check this https://stackoverflow.com/questions/5135556/dynamic-file-path-in-django – Selim Yılmaz Nov 15 '18 at 06:39
  • 1
    You don't need separate models here. You just need a dynamic upload_to parameter - which you already seem to have, with FileNameGenerator. Why can't you extend that to use different directories as well as filenames? – Daniel Roseman Nov 15 '18 at 08:36

0 Answers0