Greetings,

I'm defining a rather complex serializer for a form that crosses many 
models.  I'm having an issue where I'm incorrectly getting required field 
errors on the individual fields of the nested serializer that I'm having 
trouble determining the best way to deal with.

>From a functional perspective this is what I am trying to do:
The form is to define a client (i.e. so I'm defining a ClientSerializer to 
use for the form).  
The client has many possible phone number types - primary, work, spouse, 
spousework etc.  Only the primary phone is required.

Each phone number maps to a PhoneSerializer() that is a model serializer 
for the PhoneNumber table.  There are individual required fields in the 
phone number table, i.e. area code and phone number as well as non required 
fields such as extension.

IF a phone type is provided, the area code and phone number must be 
populated.  However, I do not want to require the work or spouse phone 
numbers so it should be valid to leave all fields for them blank in the 
form.

What is happening though is if in the form I do not provide a work or 
spouse phone number, I get an error saying area code and ohone number are 
required fields for work phone and spouse phone.

I don't want to make the area code and phone number fields allow null, 
because I don't want models created without those fields.  But if my 
spouse, work, or spousework phone numbers are all blank then I don't want 
to create models for them.  I am not sure how to represent that in my 
ClientSerializer to both ensure correct validation and correct persistance, 
so any assistance is appreciated! 

I am guessing the problem may be that the default form renderer 
(render_form serializer) is appending blanks for all the fields in the 
phone numbers that I don't populate and the validator is interpreting this 
as a non-null phone that requires validation?

Here are my pertinent definitions:
in ClientSerializer:

primary_phone = PhoneSerializer()
spouse_phone = PhoneSerializer(required=False, allow_null=True)
work_phone = PhoneSerializer(required=False, allow_null=True)
spouse_work_phone = PhoneSerializer(required=False, allow_null=True)


    def create(self, validated_data)

    #do other stuff to create the client & user objects


    spouse_phone_data = validated_data.pop('spouse_phone', None)

    if spouse_phone_data:
        PhoneNumber.objects.create(user=user, phone_type=PHONE_TYPE_SCELL, 
**spouse_phone_data)

    work_phone_data = validated_data.pop('work_phone', None)
    if work_phone_data:
        PhoneNumber.objects.create(user=user, phone_type=PHONE_TYPE_WORK, 
**work_phone_data)

    spouse_work_phone_data = validated_data.pop('spouse_work_phone', None)
    if spouse_work_phone_data:
        PhoneNumber.objects.create(user=user, phone_type=PHONE_TYPE_SWORK, 
**spouse_work_phone_data)


class PhoneSerializer(serializers.ModelSerializer):

    class Meta:
        model = PhoneNumber
        fields = 
('area_code','phone_number','extension','description','country_code')


in the models:


class PhoneNumber(models.Model):

    phone_type = models.CharField(choices=PHONE_TYPE_CHOICES, max_length=20)
    description = models.CharField(max_length=128, null=True, blank=True)
    country_code = models.IntegerField(default=1)
    area_code = models.IntegerField()
    phone_number = models.IntegerField()
    extension = models.IntegerField(null=True, blank=True)

    user = models.ForeignKey("accounts.User", related_name="phone_user", 
null=True, blank=True)


Thanks in advance!

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to