class UserInfo(models.Model):
user_id = models.AutoField(primary_key=True)
user_firstname = models.CharField(max_length=20, verbose_name = "First Name")
user_lastname = models.CharField(max_length=20, verbose_name = "Last Name")
user_email = models.EmailField(max_length=50, verbose_name = "Email Id")
user_dob = models.DateField(auto_now = False, auto_now_add=False, verbose_name = "Date of Birth")
user_mobileno = models.CharField(max_length=14, verbose_name = "Contact No")
user_image = models.ImageField(upload_to = upload_location,
null=True,
blank=True,
width_field = "width_field",
height_field = "height_field", verbose_name = "Profile Picture")
width_field = models.IntegerField(default=0)
height_field = models.IntegerField(default=0)
user_password = models.CharField(max_length=20, verbose_name = "Password")
user_blood_group = models.CharField(max_length=5, verbose_name = "Blood Group")
user_location_id = models.ForeignKey(Location, on_delete=models.CASCADE, verbose_name = "Current Location")
user_profession = models.CharField(max_length=20, verbose_name = "Profession")
user_fb_id = models.CharField(max_length=20, verbose_name = "Facebook Contact", blank=True)
user_random_id = models.CharField(max_length=20, verbose_name = "Registration Id")
user_created_date = models.DateField(auto_now = True, auto_now_add=False)
user_updated_date = models.DateField(auto_now = True, auto_now_add=False)
def get_name(self):
return "%s %s" % (self.user_firstname, self.user_lastname)
def __str__(self):
return "%s %s" % (self.user_firstname, self.user_lastname)
当我发出跟踪请求后,它正在保存user_firstname&姓氏甚至没有更新。并且get_name方法返回“”。
Folowing是我的序列化器
class UserSerializer(serializers.ModelSerializer):
name = serializers.CharField(source='get_name')
class Meta:
model = UserInfo
fields = ['user_id','name']
任何人都可以帮忙解决这个问题吗?
答案 0 :(得分:2)
根据序列化程序中定义的名称传递用于创建对象的参数,该名称将按照您的需要运行,确保为所有不能为null的参数传递值。现在,您希望用户传递参数名称而不是user_firstname& user_lastname您需要在创建&时获取name的值。拆分名称获取名字&姓氏和将其分配给user_firstname& user_lastname。