Herman Schaaf
Herman Schaaf

Reputation: 48475

One-to-many relationships in Django

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:

How can I achieve this?

Upvotes: 1

Views: 925

Answers (1)

Paul McMillan
Paul McMillan

Reputation: 20117

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.

Upvotes: 3

Related Questions