1

So I have 2 models, like this:

class Movie(models.Model):
    # some fields here

class MovieGenre(models.Model):
    movie = models.ForeignKey(Movie)
    genre = models.CharField(max_length=256, choices=MOVIE_GRENRES)

This is my model admin:

class MovieAdmin(admin.ModelAdmin):
    fields = ['title', 'description', 'publish_date', 'file_1080p', 'thumbnail']

What I want to do is to let people add Movie models in the admin page, selecting the genres from the list (there are 6 genres) as a checkbox or something similar. So I don't create 2 different pages to add both movies and genres.

Thanks!

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Pacha
  • 1,438
  • 3
  • 23
  • 46

1 Answers1

1

This is a perfect use case for Model Admin Inlines:

The admin interface has the ability to edit models on the same page as a parent model.

These are called inlines.

Example for TabularInline:

from django.contrib import admin

class MovieGenreInline(admin.TabularInline):
    model = MovieGenre

class MovieAdmin(admin.ModelAdmin):
    inlines = [
        MovieGenreInline,
    ]
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • It is a choice field. Is it possible to use a checkbox for all genres instead of adding them from a dropdown? – Pacha Jul 18 '14 at 13:41
  • @Pacha it really sounds like you need `ManyToManyField` here, not a simple `ForeignKey`. See this topic for a similar use case: http://stackoverflow.com/questions/2726476/django-multiple-choice-field-checkbox-select-multiple – alecxe Jul 18 '14 at 13:47