我在一個名為customer的應用程式中有兩個模型。模型是Customer和Account。邏輯是一個客戶可以有多個帳戶。因此,客戶中自動生成的默認“id”列是Account中的外鍵。帳戶還有一個自動生成的默認“id”列。我為Account中的account_id欄位創建了一個函式,該函式將通過一個簡單的演算法生成。來自Account的customer_id和id將連接為“customer_id”_“id”(例如 1_1)。該函式將首先檢查當前的customer_id創建帳戶實體,然后檢查哪個是該customer_id的最后一個id。對于新添加的帳戶,它將從account_id中捕獲id部分(即下劃線后的部分)并將其加 1。然后將相同的customer_id和id連接起來。以下是代碼-:
模型.py
from asyncio.windows_events import NULL
from django.db import models
from django.db.models.signals import pre_save
from django.contrib.auth.models import AbstractBaseUser
from .utils import set_account_id
class Customer(AbstractBaseUser):
name = models.CharField(max_length=50)
phone = models.BigIntegerField(null=True)
email = models.EmailField(max_length=100, null=True)
org_name = models.CharField(max_length=100, null = True)
org_logo = models.ImageField(upload_to='logos', blank=True)
subscription_start_date = models.DateField()
subscription_end_date = models.DateField()
password = models.CharField(max_length=50, blank=True, null=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name', 'email']
def __str__(self):
return self.name
class Account(AbstractBaseUser):
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
account_id = models.CharField(max_length=100, default=None, blank=True, null=True)
name = models.CharField(max_length=50)
phone = models.BigIntegerField(null=True)
email = models.EmailField(max_length=100, null=True)
password = models.CharField(max_length=50, blank=True, null=True)
account_type = models.CharField(max_length=50, choices=[
("survey", "SURVEY"),
("sme", "SME"),
("residents", "RESIDENTS")
])
account_subscription_date = models.DateField(null=True)
account_expiry_date = models.DateField(null=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name', 'email']
def __str__(self):
return self.name
def pre_save_set_account_id(sender, instance, *args, **kwargs):
if not instance.account_id:
instance.account_id = set_account_id(instance) #Error here
print(instance.account_id)
pre_save.connect(pre_save_set_account_id, sender=Account)
實用程式.py
def set_account_id(instance):
Klass = instance.__class__
fk = 'customer_id'
obj = Klass.objects.first()
field_object = Klass._meta.get_field(fk)
fk_value = field_object.value_from_object(obj) #Error here
last_account = Klass.objects.filter(customer_id=fk_value).all().order_by('id').last()
accounts_pk = last_account.id
new_account_int = accounts_pk 1
new_account_id = fk_value '_' new_account_int
return new_account_id
現在,當我在客戶中輸入一條記錄并嘗試通過管理面板在帳戶中輸入一條記錄時。當我在管理面板中填寫帳戶表格時,我將account_id留空。單擊Save后,我收到以下錯誤:
/admin/customer/account/add/ 'NoneType' 物件的 AttributeError 沒有屬性 'customer_id'
我已經在 Django 除錯器指出錯誤的特定行上評論了“此處的錯誤”。我是 Django 新手,嘗試了很多但無法解決錯誤。請幫忙。
uj5u.com熱心網友回復:
嘿,我不知道你為什么首先需要這個 account_id 作為 db 欄位?如果您需要它來執行過濾,我建議使用 post_save 信號,例如:
def post_save_set_account_id(sender, instance, *args, **kwargs):
if instance.account_id is None:
instance.account_id = f'{instance.customer.id}_{instance.id}'
instance.save()
但是,如果您不需要基于 account_id 欄位構建 sql 查詢,我建議在 Account 模型中使用 @property:
@property
def account_id(self):
return f'{self.id}_{self.account.id}'
然后您將其用作模型欄位,例如:
account = Account.objects.get(pk=1)
account.account_id
列印例如:
1_1
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/487021.html
上一篇:在Djnago中過濾欄位具有特定值或為空的QuerySet
下一篇:嵌套序列化程式創建新物件
