Dave
Dave

Reputation: 3956

How to enable Django management interface for foreign keys?

https://docs.djangoproject.com/en/1.4/ref/models/relations/ gives model definition and code examples of using one-to-many and many-to-many relationships. But neither the reference nor the tutorial (https://docs.djangoproject.com/en/1.4/intro/tutorial02/) give any administration examples for foreign keys.

Using the following models from the relations examples:

class Reporter(models.Model):
    name = models.CharField()

class Article(models.Model):
    title = models.CharField()
    reporter = models.ForeignKey(Reporter)

class Topping(models.Model):
    flavor = models.CharField()

class Pizza(models.Model):
    type = models.CharField()
    toppings = models.ManyToManyField(Topping)

(Each model includes unicode() def, omitted for brevity.) And after registering each in admin.py:

admin.site.register(Reporter)
admin.site.register(Article)
admin.site.register(Topping)
admin.site.register(Pizza)

the admin interface allows the creation of instances of each of the four types. When managing articles it shows a picklist to select the reporter. But when managing reporters there is no list of articles.

Similarly managing pizza shows a selection list of toppings, but managing toppings does not show the list of pizza types using that topping.

How does one modify admin.py to enable automatic management of the reporter.article_set and topping.pizza_set relations?

------------ EDIT ------------

In an attempt to simplify the question, I oversimplified. The actual pizza models and admin interface I'm using are:

class Topping(models.Model):
    flavor = models.CharField(max_length=32)
    quantity = models.IntegerField()

def __unicode__(self):
    return self.flavor

class Pizza(models.Model):
    type = models.CharField(max_length=32)
    toppings = models.ManyToManyField(Topping)
    price = models.DecimalField(max_digits=5, decimal_places=2)

def __unicode__(self):
    return self.type

and

class ToppingInline(admin.TabularInline):
    model = Topping
    extra = 3

class PizzaAdmin(admin.ModelAdmin):
    filedsets = [(None,  {'fields':['type', 'price']})]
    inlines = [ToppingInline]

admin.site.register(Topping)
admin.site.register(Pizza, PizzaAdmin)

and the article / reporter model I'm using is similar - a many-to-many field because one reporter can have multiple articles and one article can have several authors. The Poll example in the tutorial shows only a one-to-many mapping from question to choices, and a straightforward extension of the tutorial's PollAdmin to PizzaAdmin fails using Django 1.4 on OpenShift Origin 3.0.

So is there some guidance or tutorial on how to write an admin.py that works with ManyToManyField? The Poll example doesn't work here because it eliminates the ability to administer choices separately from questions. To be comparable one should be able to create a catalog of choices (by administering Choices) and then from the Questions interface be able to select which choices apply to the question but not be able to modify the content of the choice.

Upvotes: 1

Views: 111

Answers (1)

Leistungsabfall
Leistungsabfall

Reputation: 6488

InlineModelAdmin is what you want. You can either use StackedInline or TabularInline.

Step 1: Create a new TabularInline class or StackedInline class for Articles:

class ArticleInline(admin.TabularInline):
    model = Article

Step 2: Create a ModelAdmin class for Reporter with the inline created above:

class ReporterAdmin(admin.ModelAdmin):
    inlines = [
        ArticleInline,
    ]

Step 3: Register the ModelAdmin with the ModelClass:

admin.site.register(Reporter, ReporterAdmin)

Btw: You should really upgrade from 1.4 to a newer Django release

Upvotes: 3

Related Questions