django - DjangoRestFramework - How to access other fields of a OneToOneField reverse relationship using a model serializer? -
i using default user model , extending userextended model:
class country(models.model): countryname = models.charfield(max_length=50, unique=true) countrycode = models.charfield(max_length=10, unique=true) class userextended(models.model): user = models.onetoonefield(user, related_name="userextended") country = models.foreignkey(country)
i trying follow documented here: http://www.django-rest-framework.org/api-guide/relations/#reverse-relations
this userserializer:
class userserializer(serializers.modelserializer): def __init__(self, *args, **kwargs): super(userserializer, self).__init__(*args, **kwargs) # call super() field in self.fields: # iterate on serializer fields self.fields[field].error_messages['required'] = 'enter valid %s.'%field # set custom error message class meta: model = user fields = ('username', 'password', 'email', 'userextended') extra_kwargs = { 'password': { 'write_only': true, } } def create(self, validated_data): user = user.objects.create_user( email = validated_data['email'], username = validated_data['username'], password = validated_data['password'], ) return user
my issue is, if end user not fill out "country" section of form before submitting it, django sends error message front end saying "enter valid userextended". "userextended" name of reverse relationship, , every user object linked userextended object , vise-versa. "userextended" mandatory, end user not have specify "userextended" field when creating user object because given (the moment end user creates user object, user object have reverse relationship userextended object default).
the end user has fill out "country" section in form because country required end user. said, how django "enter valid country" rather "enter valid userextended"?
if end user saves user object, , submits "canada" in "country" section in form, how drf know save "canada" country? because currently, looks drf thinks "country" section in form refers "userextended" reverse relationship field.
what i'd expect is, in "fields" array user serializer, use dot notation? this?:
fields = ('username', 'password', 'email', 'userextended.country')
edit: created userextended serializer well, so:
class userextendedserializer(serializers.modelserializer): class meta: model = userextended fields = ('country')
but don't know how "merge" serializer original userserializer.
solution given igor works when want fetch details of users because of read_only field. serializer doesn't support nested write default. write nested object should override create method of userserializer.
class userserializer(serializers.modelserializer): def __init__(self, *args, **kwargs): super(userserializer, self).__init__(*args, **kwargs) # call super() field in self.fields: # iterate on serializer fields self.fields[field].error_messages['required'] = 'enter valid %s.'%field # set custom error message country = serializers.relatedfield(source='userextended.country') class meta: model = user fields = ('username', 'password', 'email', 'country') extra_kwargs = { 'password': { 'write_only': true, } } def create(self, validated_data): user = user.objects.create_user( email = validated_data['email'], username = validated_data['username'], password = validated_data['password'], ) country=country.objects.get(pk=validated_data['country']) userextended.objects.create(user=user, country=country) return user
data send when creating:
{'username':'sachin', 'email': 'email@gmail.com', 'password': 'abc', 'country':2}
Comments
Post a Comment