开发者

Django admin application for master/detail page

开发者 https://www.devze.com 2023-02-14 09:37 出处:网络
Consider this simplified model in Django: class Item(models.Model): title = models.CharField(max_length=200)

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       |
+-----------------+----------------------+-------------------------+
|<               &g开发者_Python百科t;|<                    >|<                       >|
|<               >|<                    >|<                       >|
|<               >|<                    >|<                       >|
|<               >|<                    >|<                       >|
|<               >|<                    >|<                       >|
+-----------------+----------------------+-------------------------+

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!


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,
    ]
0

精彩评论

暂无评论...
验证码 换一张
取 消