主/详细页面的 Django 管理应用程序
考虑 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()
有没有一种方法可以使用 admin 在同一页面上编辑项目及其详细信息,其表单如下所示
title: < >
pub_date: < >
details:
+-----------------+----------------------+-------------------------+
| name | value | diplay order |
+-----------------+----------------------+-------------------------+
|< >|< >|< >|
|< >|< >|< >|
|< >|< >|< >|
|< >|< >|< >|
|< >|< >|< >|
+-----------------+----------------------+-------------------------+
: > 将是数据输入的输入类型的占位符。
所以,我的问题是:我可以使用 admin 从父级的角度编辑外键
关系吗?如果没有办法用 Django 的 admin 来编辑数据,那么尝试扩展/自定义 admin 来做到这一点是个好主意吗?有关如何执行此操作的任何指示?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这实际上是 django 擅长处理关系的唯一方向——反之则更困难(直接编辑子级的相关父级)。
要获得您想要的格式,请查看 ModelAdmin 内联:
http://docs.djangoproject.com/ en/dev/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin
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