我正在做一个通过夹层生成的Django项目。我已经能够创建我的模型,但是我希望有一个表单,管理员可以从列表中选择一个值来分配多个或多个关系中的值。例如,我有一个模式模型:
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)员工可以拥有多个架构,架构可以由多个员工拥有。我的数据库中有一个活动的雇员,但是当我试图创建一个Schema时,它会显示为Employee Object。相反,我希望表单显示Employee.name。我该怎么做?我的管理文件包含以下内容:
class SchemasAdmin(admin.ModelAdmin):
list_display = ['name', 'status', 'database', 'description']
ordering = ['status', 'database', 'name']
actions = []
exclude = ('created_at', 'updated_at', 'deleted_at')

发布于 2018-03-27 18:35:20
首先,您是在使用python 2还是3?对于第三种方法,应该使用__str__方法而不是__unicode__。我之所以写这篇文章,是因为雇员的__unicode__方法似乎有问题,尽管它被定义为:
def __unicode__(self):
return "{}".format(self.employee)th Employee类没有employee属性(除非类继承自(AutoCreatedUpdatedMixin, SoftDeleteMixin)的混合器中有这样的属性),但我不认为是这样的。
无论如何,问题在于您尚未在__str__类上定义属性__unicode__ (如果使用python 3)或__unicode__(用于python 2)方法--只需定义如下:
return self.name您应该在django管理选择字段中看到雇员的姓名。
https://stackoverflow.com/questions/49519391
复制相似问题