本文介绍了DRF - ModelSerializer,具有非模型write_only字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有以下Model,Serializer和View。我的目标是传递一个自定义字符串,如 referrer =pid = email& af_sub1 = ui_1120& c = xyz$ b在POST方法(下面的RegisterViewSet)中,然后到视图集/序列化程序使用这些信息来填写这个信息,$ b 所以: 1. referrer是write_only字段 2. referrer不是模型中的一个字段,而是信息将用于填充模型中的字段 如何以DRF方式实现? 模型 class User(AbstractUser): first_name = models.CharField(max_length = 100,null = True,blank = True ) last_name = models.CharField(max_length = 100,null = True,blank = True) #社交/病毒功能相关字段 referral_campaign = models.CharField(default = ,max_length = 200,help_text =导致用户注册的广告系列) referral_media_source = models.C harField(default =,max_length = 200,help_text =导致用户注册的活动) inviting_user = models.ForeignKey('self',help_text =Inviting user,null = True,blank =真的) 序列化器 class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields =('first_name','last_name',) write_only_fields =('first_name','last_name','referrer')#how使用此'referrer'字段填充Model字段? read_only_fields =('id',) 查看 class RegisterViewSet(generics.CreateAPIView): model = User serializer_class = UserSerializer permission_classes = [ permissions.AllowAny ] 解决方案首先,您需要在序列化程序中定义此字段: class UserSerializer(serializers.ModelSerializer): referrer = serializers.CharField(max_length = 300,allow_blank = True) class Meta: model = User fields =('id','first_name','last_name','referrer' b $ b write_only_fields =('first_name','last_name','referrer')#how使用这个'referrer'字段填充Model字段? read_only_fields =('id',) (我不确定是否需要'first_name','last_name'在write_only_fields中,因为这意味着你将只收到ID作为回应,但这取决于您的要求) 现在,您需要覆盖serializer restore_object方法: def restore_object(self,attrs,instance = None): referrer = attrs.pop 'referrer')#parse referrer to referral_campaign,referral_media_source,inviting_user ... instance = super(UserSerializer,self).restore_object(attrs,instance = instance) instance .referral_campaign = referral_campaign instance.referral_media_source = referral_media_source instance.inviting_user = inviting_user 返回实例 (此答案假定DRF 2) I have the following Model, Serializer and View. My aim is to pass a custom string like referrer = "pid=email&af_sub1=ui_1120&c=xyz"in the POST method (RegisterViewSet below) and then to the viewset/serializer to use this information to fill in the referral_campaign, referral_media and inviting_userSo:1. referrer is write_only field2. referrer is not a field in the model, but the info would be used to populate fields in the modelHow to achieve this in the DRF way?Modelclass User(AbstractUser): first_name = models.CharField(max_length=100, null=True, blank=True) last_name = models.CharField(max_length=100, null=True, blank=True) # social/viral feature related fields referral_campaign = models.CharField(default="", max_length = 200, help_text="Campaign that led to the user signup") referral_media_source = models.CharField(default="", max_length = 200, help_text="Campaign that led to the user signup") inviting_user = models.ForeignKey('self', help_text="Inviting user", null=True, blank=True)Serializerclass UserSerializer(serializers.ModelSerializer):class Meta: model = User fields = ('first_name', 'last_name',) write_only_fields = ('first_name', 'last_name', 'referrer') #how to use this 'referrer' field to populate the Model fields? read_only_fields = ('id',)Viewclass RegisterViewSet(generics.CreateAPIView): model = User serializer_class = UserSerializer permission_classes = [ permissions.AllowAny ] 解决方案 First, you need to define this field in the serializer:class UserSerializer(serializers.ModelSerializer): referrer = serializers.CharField(max_length=300, allow_blank=True) class Meta: model = User fields = ('id', 'first_name', 'last_name', 'referrer') write_only_fields = ('first_name', 'last_name', 'referrer') #how to use this 'referrer' field to populate the Model fields? read_only_fields = ('id',)(I am not really sure if you need 'first_name', 'last_name' in write_only_fields, as this means you will get only ID in response, but this depends on your requirements)Now, you need to override the serializer restore_object method:def restore_object(self, attrs, instance=None): referrer = attrs.pop('referrer') # parse referrer to referral_campaign, referral_media_source, inviting_user ... instance = super(UserSerializer, self).restore_object(attrs, instance=instance) instance.referral_campaign = referral_campaign instance.referral_media_source = referral_media_source instance.inviting_user = inviting_user return instance(This answer assumes DRF 2) 这篇关于DRF - ModelSerializer,具有非模型write_only字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-13 18:33