Django:InlineAdmin 和 ManyToManyField 以及“through”
我有一个简单的 Gallery
模型,它通过具有 ordering
的表通过多对多关系与 Image
模型相关code>-attribute:
# models.py
class Image(models.Model):
....
class Gallery(models.Model):
images = models.ManyToManyField(Image, through='ImageGallery')
....
class ImageGallery(models.Model)
image = models.ForeignKey(Image)
gallery = models.ForeignKey(Gallery)
ordering = models.PositiveIntegerField(_('ordering'), default=0)
# admin.py
class ImageGalleryAdmin(admin.ModelAdmin):
model = ImageGallery
class GalleryAdmin(admin.ModelAdmin):
inlines = (ImageGalleryAdmin,)
我正在通过内联管理编辑“through”表。
我想做的是能够直接在内联管理中上传/编辑图像,所以我想知道是否有人知道现有的片段,它允许我编辑“通过”的字段-表与引用模型(图像)的字段一起,不需要编辑外键选择......
I'm having a simple Gallery
model, that is related to an Image
model via a many-to-many relationship through a table that has an ordering
-attribute:
# models.py
class Image(models.Model):
....
class Gallery(models.Model):
images = models.ManyToManyField(Image, through='ImageGallery')
....
class ImageGallery(models.Model)
image = models.ForeignKey(Image)
gallery = models.ForeignKey(Gallery)
ordering = models.PositiveIntegerField(_('ordering'), default=0)
# admin.py
class ImageGalleryAdmin(admin.ModelAdmin):
model = ImageGallery
class GalleryAdmin(admin.ModelAdmin):
inlines = (ImageGalleryAdmin,)
I'm editing the 'through' table via an inline admin.
What I'd like to do is to be able to upload/edit the images directly in the inline admin, so I'd like to know if anybody knows an exisiting snippet, that allows me to edit the field of the 'through'-table together with the fields of the referenced model (the image), not needing to edit the foreign key select....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎这个问题已经在这里得到解答:
Django admin - 内联内联(或同时编辑三个模型)
您需要为引用链接对象的内联创建自定义表单和模板。
It seems this question has already been answered here:
Django admin - inline inlines (or, three model editing at once)
You need to create a custom form and template for the inline which references the linked object.
我可能没有理解你的问题。你不能只使用:
I might not have understood your question. Can't you just use: