如何在Django中让updated_by字段显示用户的全名?

dwbf0jvd  于 2023-05-08  发布在  Go
关注(0)|答案(1)|浏览(101)

我最近开始使用Django。请帮我弄清楚。我有一个数据库,存储有关网络设备的各种信息。在站点上,此信息显示在一个表中。我希望updated_by字段显示创建此表或上次修改此表的用户的全名。所有用户都在Django中注册,有以下字段:名姓我希望这两个字段一起显示在表中的updated_by字段中。例如,在网站上的表格的末尾写着:由员工更新:伊凡伊凡
我试着这样做:

from django.contrib.auth.models import User

updated_by = models.ForeignKey(User, verbose_name='Updated by an employee', on_delete=models.SET_DEFAULT, default='Employee deleted')

但我得到一个错误

OperationalError at /admin/mainapp/device/1/change/
no such column: device.updated_by_id

我的代码models.py:

from django.db import models
from django.contrib.auth.models import User

    class Device(models.Model):
        device_vendor = models.CharField(max_length=128, blank=True, verbose_name='Vendor')
        device_model = models.CharField(max_length=128, blank=True, verbose_name='Model')
        device_hostname = models.CharField(max_length=128, blank=True, verbose_name='Hostname')
        device_serial_number = models.CharField(max_length=128, blank=True, verbose_name='Serial number')
        device_description = models.TextField(blank=True, verbose_name='Description')
        project = models.CharField(max_length=128, blank=True, verbose_name='Project')
        updated_by = models.ForeignKey(User.get_full_name(), verbose_name='Updated by', on_delete=models.SET_DEFAULT, default='None')
        # updated_by = models.CharField(max_length=128, verbose_name='Updated by')
        updated_at = models.DateTimeField(auto_now=True, verbose_name='Updated at')
        rack_id = models.ForeignKey(Rack, on_delete=models.CASCADE, verbose_name='Rack')
vhmi4jdf

vhmi4jdf1#

你可以获取用户的详细信息,它被用作另一个表的外键。假设型号名称为Foo

from django.db.models.functions import Concat
from django.db.models import Value

data = Foo.objects.filter().annotate(full_name=Concat("updated_by__first_name", Value(" "), "updated_by__last_name"))

这将返回用户的全名。

更新

你可以覆盖你的保存方法来获取该用户的全名,并将其存储在另一个字段中:

from django.db import models
from django.contrib.auth.models import User

    class Device(models.Model):
        device_vendor = models.CharField(max_length=128, blank=True, verbose_name='Vendor')
        device_model = models.CharField(max_length=128, blank=True, verbose_name='Model')
        device_hostname = models.CharField(max_length=128, blank=True, verbose_name='Hostname')
        device_serial_number = models.CharField(max_length=128, blank=True, verbose_name='Serial number')
        device_description = models.TextField(blank=True, verbose_name='Description')
        project = models.CharField(max_length=128, blank=True, verbose_name='Project')
        updated_by = models.ForeignKey(User, verbose_name='Updated by', on_delete=models.SET_DEFAULT, default='None')
        updated_at = models.DateTimeField(auto_now=True, verbose_name='Updated at')
        rack_id = models.ForeignKey(Rack, on_delete=models.CASCADE, verbose_name='Rack')
        user_full_name = models.CharField(max_length=128, null=True,blank=True) # add this and do migrate

        def save(self, *args, **kwargs):
            self.user_full_name = self.updated_by.first_name + ' ' + self.updated_by.last_name
            super(Device, self).save(*args, **kwargs)

相关问题