Django:InlineAdmin 和 ManyToManyField 以及“through”

发布于 2024-10-05 15:51:38 字数 761 浏览 2 评论 0原文

我有一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆蝶 2024-10-12 15:51:38

似乎这个问题已经在这里得到解答:

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.

北风几吹夏 2024-10-12 15:51:38

我可能没有理解你的问题。你不能只使用:

类 ImageAdmin (admin.ModelAdmin)

内联 = (ImageGalleryAdmin,)

admin.site.register(Image, ImageAdmin)

I might not have understood your question. Can't you just use:

class ImageAdmin (admin.ModelAdmin)

inlines = (ImageGalleryAdmin,)

admin.site.register(Image, ImageAdmin)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文