一尘不染

如何将计算字段添加到Django模型

python

我有一个简单的Employee模型,其中包括firstnamelastnamemiddlename领域。

在管理方面,可能在其他地方,我想显示为:

lastname, firstname middlename

对我而言,执行此操作的逻辑位置是在模型中通过创建一个这样的计算字段:

from django.db import models
from django.contrib import admin

class Employee(models.Model):
    lastname = models.CharField("Last", max_length=64)
    firstname = models.CharField("First", max_length=64)
    middlename = models.CharField("Middle", max_length=64)
    clocknumber = models.CharField(max_length=16)
    name = ''.join(
        [lastname.value_to_string(),
        ',',
         firstname.value_to_string(),
        ' ',
         middlename.value_to_string()])

    class Meta:
        ordering = ['lastname','firstname', 'middlename']

class EmployeeAdmin(admin.ModelAdmin):
    list_display = ('clocknumber','name')
    fieldsets = [("Name", {"fields":(("lastname", "firstname", "middlename"), "clocknumber")}),
        ]

admin.site.register(Employee, EmployeeAdmin)

最终,我认为我需要以字符串形式获取名称字段的值。我得到的错误是value_to_string() takes exactly 2 arguments (1 given)。字符串所需的值self, obj。我不知道这obj是什么意思。

一定有一种简单的方法可以做到这一点,我敢肯定我不是第一个想要这样做的人。

编辑:下面是我的代码修改为丹尼尔的答案。我得到的错误是:

django.core.exceptions.ImproperlyConfigured:
    EmployeeAdmin.list_display[1], 'name' is not a callable or an
    attribute of 'EmployeeAdmin' of found in the model 'Employee'.
from django.db import models
from django.contrib import admin

class Employee(models.Model):
    lastname = models.CharField("Last", max_length=64)
    firstname = models.CharField("First", max_length=64)
    middlename = models.CharField("Middle", max_length=64)
    clocknumber = models.CharField(max_length=16)

    @property
    def name(self):
        return ''.join(
            [self.lastname,' ,', self.firstname, ' ', self.middlename])

    class Meta:
        ordering = ['lastname','firstname', 'middlename']

class EmployeeAdmin(admin.ModelAdmin):
    list_display = ('clocknumber','name')
    fieldsets = [("Name", {"fields":(("lastname", "firstname", "middlename"), "clocknumber")}),
]

admin.site.register(Employee, EmployeeAdmin)

阅读 137

收藏
2020-12-20

共1个答案

一尘不染

好的… Daniel Roseman的答案似乎应该起作用。与往常一样, 在发布问题之后 ,您会找到所需的内容。

Django
1.5文档中,
我发现了可以立即使用的示例。感谢大家的帮助。

这是起作用的代码:

from django.db import models
from django.contrib import admin

class Employee(models.Model):
    lastname = models.CharField("Last", max_length=64)
    firstname = models.CharField("First", max_length=64)
    middlename = models.CharField("Middle", max_length=64)
    clocknumber = models.CharField(max_length=16)

    def _get_full_name(self):
        "Returns the person's full name."
        return '%s, %s %s' % (self.lastname, self.firstname, self.middlename)
    full_name = property(_get_full_name)


    class Meta:
        ordering = ['lastname','firstname', 'middlename']

class EmployeeAdmin(admin.ModelAdmin):
    list_display = ('clocknumber','full_name')
    fieldsets = [("Name", {"fields":(("lastname", "firstname", "middlename"), "clocknumber")}),
]

admin.site.register(Employee, EmployeeAdmin)
2020-12-20