This is django and django rest framework. I have 2 models: User and phone.
The 1st problem:
I want to be able to update user data(email) alongside phone data(phone numbers) in 1 single api update response. Phone number can be 0 or many. Well, like partial=True actually. If a user just want to update phone numbers, don't update email and vice versa.
Additional info: At the time of registering, phone is not included. Just basic user info (last name, first name, email, password). The phone can only get updated in the user profile form after registration is done. The user profile form is actually linking to multiple models, which is User and Phone
The 2nd problem:
How to serialize multiple phone_numbers and save to db?
class User(AbstractBaseUser):
email = models.EmailField(unique=True, default='')
USERNAME_FIELD = 'email'
class Phone(models.Model):
phone_number = models.CharField(max_length=10)
owner = models.ForeignKey(User)
--------------------------------------
class UserSerializer(serializers.ModelSerializer):
phone_number = PhoneSerializer(required=False, many=True)
class Meta:
model = User
fields = ('email, 'phone_number')
class PhoneSerializer(serializers.ModelSerializer):
class Meta:
Model = Phone
fields = ('phone_number')
The html form would have plus sign at the phone number field to indicate a new phone number can be added. example is here
email : admin@admin.com
phone number: 23423432423
(add more)
phone number: 3423423423
(add more)
...
The expected json
{
'email': 'email@email.com',
'phone_number': '32432433223234'
}
or if many phone numbers are added
{
'email': 'email@email.com',
'phone_number': '32432433223234',
'phone_number': '324342322342323'
}
or maybe
{
'email': 'email@email.com',
'phone_number': ['32432433223234','324342322342323']
}
or maybe
{
'email': 'email@email.com',
'Phone': [{'id': 1, 'phone_number': '32432433223234'}, {'id': 2, 'phone_number': '324342322342323'}]
}
is this json possible to do? how can serializer and modelviewset do it? sorry I'm totally new to drf