开发者

Django - Multiple Image Upload

开发者 https://www.devze.com 2023-03-03 04:37 出处:网络
I need to upload and attach an arbitrary number of images to my model. The site is for an estate agent, so they want to attach images to the property pages of their site. I can only think of the follo

I need to upload and attach an arbitrary number of images to my model. The site is for an estate agent, so they want to attach images to the property pages of their site. I can only think of the following solutions:

  1. Create another model for the images and associate them using a (edit:)ForeignKey field. There are many reasons why I don't want to do this, mainly because it confuses the process of adding a new property (they'd have to go two different admin screens) and since there could be thousands of properties, finding them in the ManyToMany drop down would be a real pain.

  2. Create a rather complicated custom widget which launches a popup with a image model. The problem here is that I need to ensure that the images are linked to the property once it has been saved, so I need to figure out a way of doing that (since when you add a new property, there is nothing to create a link to.) I know I could do this with signals, but I'm unsure as to how I'd do this with multiple images since there could be more than one value to associate.

D开发者_JS百科oes anyone have any suggestions as to how I can do this? Fundamentally I just need a way of uploading a dynamic number of images and somehow associating them with my model.

Thanks!


It doesn't complicate the admin screen at all thanks to the addition of inline model admin objects. You would have the property admin page with a list of images below it, with the inline giving you the ability to associate new images with each property.

This is pretty much the classic example of a foreign key scenario. I'd question the use of a many to many relationship in that a photo of a property shouldn't be being linked against other properties (though I suppose in the case of blocks of flats...) You need to store the association between property and image and that is done via a foreign key relationship if you want your database to be in any way well designed. Otherwise you'll be having to kludge many file names into a single field which is very messy and almost unqueryable in any sane way.

Alternatively if the admin doesn't give you enough in the way of flexibility it wouldn't be too hard to construct a ModelForm that allowed associating any number of images to upload.

0

精彩评论

暂无评论...
验证码 换一张
取 消