从 Django 1.11 迁移到 2.1:“fieldsets[2][1]”中有重复的字段

Posted

技术标签:

【中文标题】从 Django 1.11 迁移到 2.1:“fieldsets[2][1]”中有重复的字段【英文标题】:Migrating from Django 1.11 to 2.1: There are duplicate field(s) in 'fieldsets[2][1]' 【发布时间】:2019-03-31 21:29:55 【问题描述】:

我目前正在将我的 Django 1.11 应用程序迁移到 Django 2.1。我有一个自定义用户模型:

from authtools.models import AbstractEmailUser, UserManager
from django.db import models
from django.utils.translation import ugettext_lazy as _
from model_utils.fields import AutoLastModifiedField
from model_utils.models import SoftDeletableModel

from core.behaviors import UniversallyUniqueIdentifiable


class User(
    UniversallyUniqueIdentifiable,
    SoftDeletableModel,
    AbstractEmailUser
):
    """
    User should generally not be deleted, but rather is_removed should just
    be set to true. The delete() method is overwritten in the
    SoftDeletableModel.
    Also add a uuid field to avoid displaying the sequential primary key.
    """

    name = models.CharField(_('name'), max_length=255, blank=True)
    modified = AutoLastModifiedField(_('modified'))

    objects = UserManager()

还有一个自定义管理员:

from authtools.admin import (BASE_FIELDS, SIMPLE_PERMISSION_FIELDS,
                             NamedUserAdmin)
from django.contrib import admin

from .models import User


def verified(obj):
    email = obj.emailaddress_set.filter(primary=True)
    if email.exists():
        return email[0].verified
    return False


verified.boolean = True


@admin.register(User)
class SoftDeletableNamedUserAdmin(NamedUserAdmin):
    """
    Overwrite the fields of the NamedUserAdmin to add is_removed.
    """

    date_hierarchy = "date_joined"
    list_display = (
        'email',
        'name',
        verified,
        'is_active',
        'is_removed',
    )
    search_fields = ["email", "name"]
    readonly_fields = ("date_joined", "modified")
    fieldsets = (
        BASE_FIELDS,
        SIMPLE_PERMISSION_FIELDS,
        ("Contact information", 
            "fields": (
                ("email", "name"),
            )
        ),
        ("Account information", 
            "fields": (
                "is_removed",
            ),
        ),
        ("Dates", 
            "fields": (
                ("date_joined", "modified",),
            ),
        )
    )
    list_filter = ('is_active', 'is_removed',)

现在的问题是,当我启动服务器时,出现以下错误:

django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
<class 'accounts.admin.SoftDeletableNamedUserAdmin'>: (admin.E012) There are duplicate field(s) in 'fieldsets[2][1]'.
<class 'accounts.admin.SoftDeletableNamedUserAdmin'>: (admin.E012) There are duplicate field(s) in 'fieldsets[3][1]'.
<class 'accounts.admin.SoftDeletableNamedUserAdmin'>: (admin.E012) There are duplicate field(s) in 'fieldsets[4][1]'.

我查看了the documentation 以查看这些元组的定义方式是否发生了变化,但我没有发现差异。这里出了什么问题?

【问题讨论】:

【参考方案1】:

打印/记录fieldsets 并删除所有重复项。例如,看起来email 可能被显式包含在BASE_FIELDS 中。

看起来检查不像以前版本的 Django 那样严格。这已由 Django 2.1 中的 ticket 29322 修复。

【讨论】:

以上是关于从 Django 1.11 迁移到 2.1:“fieldsets[2][1]”中有重复的字段的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Django 迁移中创建 GIN 索引

无法将 Django 从 1.7 迁移到 1.8

Django 1.11 模型迁移操作不适用

Two Scoops of Django 1.11 章节2.1简译

Django ORM那些相关操作

升级到 django 1.11 多对多列未找到