I’m trying to create a One-to-many relationship in Django. In my example, I have a news post, which may have several images associated with it. However, different news posts will never share images.
As far as I can see, there are two ways to do this: Through a ManyToManyField, which creates a multi-select tool in the admin panel, which shows all the images ever uploaded, or through a ForeignKeyField in the PostImage class, which results in there not being any option to add new images when creating a new news post on the admin panel.
Since the users of the admin panel will not be at all technically-inclined, I would like two things, if possible:
- Add several images on the “Create new news post” page, without having any images from other news posts as a choice
- Less importantly, replace the multi-select box with checkboxes, or anything less confusing than a multi-select box.
How can I achieve this?
The second part is the easier one:
You want the horizontal javascript filter
If you go with a ManyToManyField, you could filter those choices by using a Custom Manager.
If you use a ForeignKey, you would want to use one of the Admin Inlines. If you really never re-use images, use the inlines.