Django嵌套表单-始终显示对象而不是模型详细信息
我正在做一个通过夹层生成的Django项目。我已经能够创建我的模型,但是我希望有一个表单,管理员可以从列表中选择,以分配多对多或一对多关系中的值。例如,我有一个模式模型:Django嵌套表单-始终显示对象而不是模型详细信息,django,mezzanine,Django,Mezzanine,我正在做一个通过夹层生成的Django项目。我已经能够创建我的模型,但是我希望有一个表单,管理员可以从列表中选择,以分配多对多或一对多关系中的值。例如,我有一个模式模型: class Schema(AutoCreatedUpdatedMixin, SoftDeleteMixin): """List of all Schemas in a given database""" name = models.CharField(max_length=128, null=False)
class Schema(AutoCreatedUpdatedMixin, SoftDeleteMixin):
"""List of all Schemas in a given database"""
name = models.CharField(max_length=128, null=False)
status = models.BooleanField(max_length=128, null=False, default=True, verbose_name="Is Active")
description = models.CharField(max_length=65535, null=True, blank=True, default=None)
database = models.ForeignKey(Database, on_delete=models.CASCADE)
pull_requests = models.ManyToManyField(Link)
questions = models.ManyToManyField(Question, blank=True)
comments = models.ManyToManyField(Comment, blank=True)
technical_owners = models.ManyToManyField(Employee, related_name='technical_owners_schemas', blank=True)
business_owners = models.ManyToManyField(Employee, related_name='business_owners_schemas', blank=True)
watchers = models.ManyToManyField(Employee, related_name='watchers_schemas', blank=True)
def __unicode__(self):
return "{}".format(self.name)
我有一个员工模型
class Employee(AutoCreatedUpdatedMixin, SoftDeleteMixin):
"""List of people with any involvement in tables or fields: business or technical owners, developers, etc"""
name = models.CharField(max_length=256, blank=False, null=False, default=None, unique=True)
email = models.EmailField(blank=True, null=True, unique=True)
def __unicode__(self):
return "{}".format(self.employee)
一个员工可以拥有多个架构,而一个架构可以由多个员工拥有。我的数据库中有一个活动的employee,但是当我尝试创建模式时,employee显示为employee对象。相反,我希望表单显示Employee.name。我该怎么做?我的管理文件包含以下内容:
class SchemasAdmin(admin.ModelAdmin):
list_display = ['name', 'status', 'database', 'description']
ordering = ['status', 'database', 'name']
actions = []
exclude = ('created_at', 'updated_at', 'deleted_at')
首先,您使用的是Python2还是Python3?对于3,应该使用_ustr__uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。我写这篇文章是因为Employee的_uUnicode__;方法似乎有问题,尽管它被定义为:
def __unicode__(self):
return "{}".format(self.employee)
th Employee类没有Employee属性,除非该类从AutoCreatedUpdatedMixin、SoftDeleteMixin继承的mixin中有这样一个属性,但我认为情况并非如此
在任何情况下,问题在于,如果在Employee类上使用python 3或python 2的unicode方法,您还没有定义propery\uuuuu str\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
return self.name
您应该在django admin select字段中看到员工的姓名