我对Django比较陌生,目前正在利用内置的管理应用程序。每当我尝试添加/保存嵌入式子组以及对父级进行简单编辑时,都会收到以下错误:
请更正以下错误
如果我不包括内联...添加,编辑和删除都可以正常工作。我不太确定如何解决此问题。我已经根据Google搜索提供的内容尝试了一些方法,但仍未解决。
这是我的模特的片段:
class Group(models.Model): # Parent
groupid = models.CharField('Group ID',db_column='groupId', primary_key=True, auto_created=True, default=uuid.uuid4, max_length=36) # Field name made lowercase.
groupabbr = models.CharField('Group Abbr',db_column='groupAbbr', max_length=30) # Field name made lowercase.
groupname = models.CharField('Group Name',db_column='groupName', max_length=100) # Field name made lowercase.
groupemail = models.EmailField('Group Email',db_column='groupEmail', max_length=200, blank=True, null=True) # Field name made lowercase.
# description = models.TextField(db_column='description', max_length=255, blank=True, null=True)
description = models.CharField(db_column='description', max_length=255, blank=True, null=True)
fkpripocpersonid = models.ForeignKey(Person,db_column='fkPriPocPersonId', related_name='priPocForGroups', on_delete=models.SET_NULL, max_length=36, blank=True, null=True, verbose_name='Primary POC') # Field name made lowercase.
fksecpocpersonid = models.ForeignKey(Person,db_column='fkSecPocPersonId', related_name='secPocForGroups', on_delete=models.SET_NULL, max_length=36, blank=True, null=True, verbose_name='Secondary POC') # Field name made lowercase.
primaryfieldtitle = models.CharField('Primary Field Title',db_column='primaryFieldTitle', max_length=100, blank=True, null=True) # Field name made lowercase.
primaryfieldcontent = models.TextField('Primary Field Content',db_column='primaryFieldContent', blank=True, null=True) # Field name made lowercase.
secondaryfieldtitle = models.CharField('Secondary Field Title',db_column='secondaryFieldTitle', max_length=100, blank=True, null=True) # Field name made lowercase.
secondaryfieldcontent = models.TextField('Secondary Field Content',db_column='secondaryFieldContent', blank=True, null=True) # Field name made lowercase.
createddate = models.DateTimeField('Date Created',db_column='createdDate', default=datetime.now()) # Field name made lowercase.
createdby = models.ForeignKey(Person, db_column='createdBy', related_name='createdGroups', on_delete=models.SET_NULL, max_length=36, null=True, verbose_name='Created By') # Field name made lowercase.
archiveddate = models.DateTimeField('Date Archived',db_column='archivedDate',
default=datetime.now(), blank=True, null=True) # Field name made lowercase.
archivedby = models.ForeignKey(Person, db_column='archivedBy', related_name='archivedGroups', on_delete=models.SET_NULL, max_length=36, blank=True, null=True, verbose_name='Archived By') # Field name made lowercase.
fkparentgroupid = models.ForeignKey('self', db_column='fkParentGroupId', related_name='Parents',
on_delete=models.CASCADE, null=True, verbose_name='Parent Group',
max_length=36, blank=True)
def __str__(self): # this object's representation used throughout Django
return self.groupabbr + ' - ' + self.groupname
class Meta:
managed = False
db_table = 'tbl_group'
ordering = ('groupname',)
这是我的Admin.py的摘录:
class GroupInline(admin.TabularInline):
model = Group
classes = ['collapse']
exclude = ['groupemail','primaryfieldtitle', 'primaryfieldcontent',
'archiveddate', 'archivedby', 'createddate', 'createdby', 'secondaryfieldtitle',
'secondaryfieldcontent',]
"""list_display = ('groupid', 'groupabbr','groupname', 'description', 'fkpripocpersonid', 'fksecpocpersonid',
'createddate', 'createdby') """
list_editable = ('groupid', 'groupabbr','groupname', 'description', 'fkpripocpersonid', 'fksecpocpersonid',
'createddate', 'createdby')
extra = 0
class GroupExistingInline(admin.TabularInline):
model = Group
classes = ['collapse']
exclude = ['groupemail','primaryfieldtitle', 'primaryfieldcontent',
'archiveddate', 'archivedby', 'createddate', 'createdby', 'secondaryfieldtitle',
'secondaryfieldcontent', 'groupid',]
readonly_fields = ('groupabbr','groupname', 'description', 'fkpripocpersonid', 'fksecpocpersonid',
'createddate', 'createdby')
extra = 0
def has_add_permission(self, request):
return False
class GroupAdmin(admin.ModelAdmin, ExportCsvMixin):
list_display = ('groupabbr','groupname', 'fkparentgroupid', 'fkpripocpersonid', 'fksecpocpersonid')
list_filter = ('groupname',)
actions = ["export_as_csv"]
inlines = [GroupExistingInline, GroupInline, ]
fieldsets = [
(None, {'fields': ('fkparentgroupid', ('groupname', 'groupabbr'),
'description', 'groupemail', ('fkpripocpersonid', 'fksecpocpersonid',), ('createddate',
'createdby'),)}),
('Primary Content', {
'classes': ('collapse',), # collapse, wide, extrapretty
'fields': ['primaryfieldtitle', 'primaryfieldcontent', ],
}),
('Secondary Content', {
'classes': ('collapse',), # collapse, wide, extrapretty
'fields': ['secondaryfieldtitle', 'secondaryfieldcontent', ],
}),
]
def __init__(self, model, admin_site):
self.form.admin_site = admin_site
super(GroupAdmin, self).__init__(model, admin_site)
def get_actions(self, request):
actions = super().get_actions(request)
if 'delete_selected' in actions:
del actions['delete_selected']
return actions
admin.site.register(Group, GroupAdmin)