我在 Django 中有以下模型,其结构如下:
class Office_Accounts(models.Model):
accountid = models.EmailField(max_length=200, unique=True)
validtill = models.DateField(default=datetime.now)
limit = models.CharField(max_length=2)
class Device(models.Model):
device_type = models.ForeignKey(DeviceType,to_field='device_type')
serial_number = models.CharField(max_length=200,unique=True)
in_use_by = models.ForeignKey(User,to_field='username')
brand = models.CharField(max_length=200,default="-", null=False)
model = models.CharField(max_length=200,default="-", null=False)
type_number = models.CharField(max_length=200,blank=True,null=True, default = None)
mac_address = models.CharField(max_length=200,blank=True,null=True, default = None)
invoice = models.FileField(upload_to='Device_Invoice', null=True, blank = True)
msofficeaccount = models.ForeignKey(Office_Accounts, to_field="accountid")
class Meta:
verbose_name_plural = "Devices"
def full_name(self):
return self.device_type + self.serial_number + self.brand
我将在 admin.py 中显示这两个模型。现在,我想在 Office_Accounts 模型的管理页面中显示字段“msofficeaccount”(存在于设备模型中)中的每个 accountid 的计数。例如,如果 xyz@abc.com 出现在 msofficeaccount 字段的 10 行中,那么在 Office_Accounts 管理页面中计数应显示为 10。谁能指导我如何解决这个问题来解决它?