gpt4 book ai didi

django - 将一些新属性/字段附加到 django rest 框架中的validated_data

转载 作者:行者123 更新时间:2023-12-04 17:59:10 25 4
gpt4 key购买 nike

我目前正在研究多用户类型身份验证系统,目前我遇到了一个问题,我想将用户 ID 添加到validated_data 字段,以便 user_id 可以存储在供应商表中

serializers.py


class CustomerSerializers(serializers.ModelSerializer):
class Meta:
model = Customer
fields = "__all__"
def create(self,validated_data):
customer = Customer.objects.create_user(**validated_data)
return customer
class VendorsSerializers(serializers.ModelSerializer):
class Meta:
model = Vendors
fields = "__all__"
read_only_fields = ('added_on','modified_on')
def create(self, validated_data):
customer_data={"mobile_number":validated_data.pop('mobile_number'),
"password":validated_datapop.('password'),
"modified_by":validated_data.pop('modified_by'),
"added_by":validated_data.pop("added_by"),
"added_source":validated_data.pop("added_source"),
"customer_role":validated_data.pop("customer_role") }
customer = Customer.objects.create_user(**customer_data)

#Some code here to append comstomer_id to validated_data

vendor = Vendors.objects.create_user({},**validated_data)
return vendor

models.py


class CustomerManager(BaseUserManager):

def _create_user( self, mobile_number, password,customer_role,is_active, is_admin, **extra_fields):
now=timezone.now()
customer = self.model(mobile_number=mobile_number, customer_role=customer_role, is_admin=is_admin, is_active=is_active, last_login=now, added_on=now, modified_on=now,**extra_fields)
customer.set_password(password)
customer.save()
return customer
def create_user(self, mobile_number,customer_role,password=None,**extra_fields):
return self._create_user(mobile_number,password,customer_role,True,False,**extra_fields)

def create_superuser(self, mobile_number,customer_role,password=None,**extra_fields):
return self._create_user(mobile_number,password,'super',True,True,**extra_fields)

class Customer(AbstractBaseUser,PermissionsMixin):
objects = CustomerManager()
ADDED_SOURCE = (('website', 'website'),('android', 'android'),('ios', 'ios'),('super', 'super'),('vendor', 'vendor'),)
id = models.AutoField(db_column='Id', primary_key=True)
mobile_number = models.CharField(max_length=10,unique=True)
added_source = models.CharField(max_length=10, choices=ADDED_SOURCE, default='website')
added_by = models.CharField(max_length=30,default='self')
added_on = models.DateTimeField(auto_now_add=True, blank=False)
customer_role = models.CharField(max_length=8,choices=(('user','user'),('vendor','vendor'),('super','super')),default='user')
modified_on = models.DateTimeField(auto_now=True)
modified_by = models.CharField(max_length=30,default='self')
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)

REQUIRED_FIELDS = ['customer_role','modified_by','added_source','added_by',]
USERNAME_FIELD = 'mobile_number'
class Meta:
managed = True
db_table = 'tbl_customer'

def get_full_name(self):
return self.mobile_number

def get_short_name(self):
return self.mobile_number

def __str__(self):
return self.mobile_number

def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin

class Vendors(models.Model):
id = models.AutoField(db_column='Id',primary_key=True)
user = models.OneToOneField('Customer')
username = models.CharField(max_length=100,unique=True)
company = models.CharField(max_length=30)
company_register_address = models.CharField(max_length=255)
company_corporate_address = models.CharField(max_length=255)
contact_number = models.CharField(max_length=15)
bank_name = models.CharField(max_length=50)
bank_account_number = models.CharField(max_length=50)
bank_ifsc_code = models.CharField(max_length=50)
status = models.CharField(max_length=8,choices=(('active','active'),('inactive','inactive')))
class Meta:
db_table='tbl_vendors'
def __str__(self):
return u'%s' % (self.username)
def get_full_name(self):
return u'%s' % (self.company)
def get_short_name(self):
return u'%s' % (self.company)

如何将我将从 Customer 对象创建中获得的客户 ID 添加到 validation_data ?
欢迎任何有关代码的其他建议。

最佳答案

您可以通过user值为创建的参数 customer创建对象时 Vendors对象使用 .objects.create()create() VendorsSerializer的方法.

您可以执行以下操作:

class  VendorsSerializers(serializers.ModelSerializer):
...

def create(self, validated_data):
customer_data={"mobile_number":validated_data.pop('mobile_number'),
"password":validated_datapop.('password'),
"modified_by":validated_data.pop('modified_by'),
"added_by":validated_data.pop("added_by"),
"added_source":validated_data.pop("added_source"),
"customer_role":validated_data.pop("customer_role") }

customer = Customer.objects.create_user(**customer_data)

# pass the created 'customer' object
vendor = Vendors.objects.create(user=customer, **validated_data)
return vendor

关于django - 将一些新属性/字段附加到 django rest 框架中的validated_data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37624003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com