Could you help me I have another requirement like I want to Show some
fields for get method and for post method I want to serialize some other
fields also. So how i can do that using same ProfileSerializers.

Plase help me.

Thank You

Regards,
Soumen

On Sun, Jul 28, 2019 at 2:59 PM Pradeep Sukhwani <[email protected]>
wrote:

> you need to pass the username in the serializer.
>
> Then in serializer, you can do something like this:
>
> serializers.py:
>
> class ProfileSerializer(serializers.ModelSerializer):
>     user = UserCreateSerializer() # create the serializer for user
>     # mention all the other fields in profile model
>
>     class Meta:
>         model = Profile
>         fields = ('user',) # change this as per your requirement
>
>    def validate(self, attrs):
>         # validate the data if you have any conditions.
>         return attrs
>
>
>     def create(self, validated_data):
>         # pop out the username from dict to create profile data and then
> save the user.
>         user = validated_data.pop('username')
>         profile_obj = Profile.objects.create(**validated_data)
>         profile_obj.user = user
>         profile_obj.save()
>         return {"success": True} # You can return anything that you want
> so that you can send this in reponse from api.py.
>
> api.py
>
> def post(self, request):
>      request_data = request.data
>      request_data.update({user: request.user.username})
>      serializer = ProfileSerializer(data=request_data)
>      if serializer.is_valid():
>          return Response(serializer.validated_data)
>
>
>
>
> On Sunday, July 28, 2019 at 2:38:47 PM UTC+5:30, Soumen Khatua wrote:
>>
>> How I can hold Profile details and them save it after provide the user
>> field details, Cn you tell me how i can do that?
>>
>> Thank you
>>
>> Regards,
>> Soumen
>>
>> On Sun, Jul 28, 2019 at 2:22 PM Pradeep Sukhwani <[email protected]>
>> wrote:
>>
>>> Hi Soumen,
>>>
>>> ModelSerializers do not behave in exactly the same way as ModelForm. 
>>> However,
>>> there are intentional design decisions behind these differences. commit is
>>> not (and won't ever be) a keyword argument to save().
>>>
>>>
>>> I believe that the usage of .save() is pretty much adequately
>>> documented here
>>> <http://www.django-rest-framework.org/api-guide/serializers/#saving-instances>
>>> .
>>>
>>>
>>> On Sunday, July 28, 2019 at 2:14:31 PM UTC+5:30, Soumen Khatua wrote:
>>>>
>>>> Hi Folks,
>>>>
>>>> I'm getting this error when I declared like this:
>>>>
>>>>
>>>> def post(self, request):
>>>>         serializer = ProfileSerializer(data=request.data)
>>>>         username = request.user.username
>>>>         user_details = User.objects.get(username = username)
>>>>         if serializer.is_valid():
>>>>             serializer_data = serializer.save(commit=False)
>>>>             serializer_data.user = user_details
>>>>             serializer_data.save()
>>>>
>>>>  File
>>>> "/home/sou/halanx/env/lib/python3.6/site-packages/rest_framework/serializers.py",
>>>> line 188, in save
>>>>     "'commit' is not a valid keyword argument to the 'save()' method. "
>>>> AssertionError: 'commit' is not a valid keyword argument to the
>>>> 'save()' method. If you need to access data before committing to the
>>>> database then inspect 'serializer.validated_data' instead. You can also
>>>> pass additional keyword arguments to 'save()' if you need to set extra
>>>> attributes on the saved model instance. For example:
>>>> 'serializer.save(owner=request.user)'.'
>>>>
>>>>
>>>> please help me guys for this issue!!!
>>>>
>>>> Thank You
>>>>
>>>> Regards,
>>>> Soumen
>>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/38571245-f03d-454b-8d8b-5258af7ee8b3%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/38571245-f03d-454b-8d8b-5258af7ee8b3%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/2699fc81-c792-4156-920f-70ead8edeff7%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/2699fc81-c792-4156-920f-70ead8edeff7%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAPUw6WbcMJdMC6wAa4Ryxa-%2Bd-Lkd_n9fkE1q2vVR7n4k9ktTg%40mail.gmail.com.

Reply via email to