【问题标题】:Django TabularInline fields giving error on updating inline fieldsDjango TabularInline 字段在更新内联字段时出错
【发布时间】:2018-05-16 13:14:44
【问题描述】:

我有一个类似的模型

class Section(models.Model):
    section_title = models.CharField(max_length=200)
    section_tiles = models.ManyToManyField(Tile, blank=True)
    on_menu = models.BooleanField(default=False)
    section_is_active = models.BooleanField(default=True)

    class Meta:
        verbose_name = "Section"
        verbose_name_plural = "Sections"

    def __unicode__(self):
        return self.section_title



class Page(models.Model):
    page_title = models.CharField(max_length=200)
    page_sections = models.ManyToManyField(Section, blank=True)
    on_menu = models.BooleanField(default=False)
    page_is_active = models.BooleanField(default=True)

    class Meta:
        verbose_name = "Page"
        verbose_name_plural = "Pages"

    def __unicode__(self):
        return self.page_title

在管理部分我有如下代码:

class SectionTabularInline(admin.TabularInline):
    model = Page.page_sections.through

    class PageAdmin(admin.ModelAdmin):
        inlines = [SectionTabularInline,]

        list_display =[
        'page_title',
        'on_menu',
        ]

        list_filter =[
        'on_menu',
        ]

        search_fields = [
        'page_title',
        ]

        ordering = ['page_title']

    admin.site.register(Page, PageAdmin)

当我在页面的内联部分进行更改时,它会给出错误“请更正以下错误”。没有任何附加信息。如果我从页面中删除部分并保存它,那么我可以从头开始重新分配它们。有没有办法在不先删除它们的情况下更改它们?

【问题讨论】:

  • 您找到修复/解决方法了吗?

标签: python django django-admin


【解决方案1】:

发件人:Save M2M "Through" Inlines in Django Admin

我发现了我的问题:如果您想要多对多关系,您需要保留原始模型的主键 (id)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-18
    • 2016-08-01
    • 1970-01-01
    相关资源
    最近更新 更多