Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin application for master/detail page

Consider this simplified model in Django:

class Item(models.Model):
    title = models.CharField(max_length=200)
    pub_date = models.DateTimeField()

class ItemDetail(models.Model):
    item = models.ForeignKey(Item)
    name = models.CharField(max_length=200)
    value = models.CharField(max_length=200)
    display_order = models.IntegerField()

Is there a way to use admin to edit an Item with its details on the same page with a form that looks something like:

title:    <       >
pub_date: <       >
details:
+-----------------+----------------------+-------------------------+
|       name      |        value         |      diplay order       |
+-----------------+----------------------+-------------------------+
|<               >|<                    >|<                       >|
|<               >|<                    >|<                       >|
|<               >|<                    >|<                       >|
|<               >|<                    >|<                       >|
|<               >|<                    >|<                       >|
+-----------------+----------------------+-------------------------+

Where < > would be placeholder for input types for data entry.

So, my question: can I use admin to edit a foreign key relationship from parent's perspective? If there isn't a way to edit data with Django's admin this way, would it be a good idea to try to extend/customize admin to do this? Any directions on how to do this?

Thanks!

like image 228
Pablo Santa Cruz Avatar asked Mar 01 '11 23:03

Pablo Santa Cruz


1 Answers

That's actually the only direction django is good at dealing with relationships for -- the other way around is harder (directly editing the related parent from the child).

To get the format you want, look into ModelAdmin inlines:
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin

class ItemDetailInline(admin.TabularInline):
    model = ItemDetail

class ItemAdmin(admin.ModelAdmin):
    inlines = [
        ItemDetailInline,
    ]
like image 181
Yuji 'Tomita' Tomita Avatar answered Sep 18 '22 09:09

Yuji 'Tomita' Tomita