-1

For example, there are two applications cars and houses, both of them currently have a models.ManyToManyField to a model with a models.ImageField(upload_to='img/[APP_NAME].

Instead of copying and pasting that snippet of ImageField code, is it better to split the image upload into a a separate application and import it? What should it look like?

davidtgq
  • 3,780
  • 10
  • 43
  • 80
  • Considering the [DRY](https://docs.djangoproject.com/en/stable/misc/design-philosophies/#don-t-repeat-yourself-dry) approach, making a new app for images makes sense. – xyres Nov 24 '15 at 14:32
  • So far the only way that comes to mind is to have a separate model in the `images` app, one model for each app that requires images. Unless I want to put the images from all the apps into one directory. – davidtgq Nov 24 '15 at 14:34

1 Answers1

1

There's no clear answer on your question. It highly depends on what you want to achieve.

With making a separate app, that only knows about images, you will have the following problems:

  • You want different upload_to for your cars and houses. You will need to set it dynamically. Example.
  • Your image model (the one with ImageField) will know about both cars and houses, via related_name property. I don't find it to be any good. To get rid of the backreferences, you can specify ManyToMany(..., related_name="+"). Note: "+" means no related name given and it shouldn't be auto-generated. But the reasonability of removing it is also questionable.
  • One more app, that does... well, nothing. One more dependency for each of your apps.

On your place I would just leave both model images, one in cars, another in houses. That's considering the amount of boilerplate code you will have to write and amount of problems you would create for yourself out of nowhere.

Community
  • 1
  • 1
Art
  • 2,235
  • 18
  • 34
  • That's pretty much my reasoning to keep it the way it is. Since this is considered acceptable, I'll leave it that way – davidtgq Nov 24 '15 at 17:55