python - post contact for specific rent in rest framework -


i developing rental application practice rest framework. there feature buyer can contact rent owner clicking contact button on rent has fill his/her email_id etc. know if want contact specific rent have this

rent = rental.objects.get(id=token) contact.objects.create(rent=rent, email_id=email_id, buyer=request.user) 

i confuse in rest framework create function of serializer , post function of apiview. can enlighten me example?

class rental(models.model):     user = models.foreignkey(user)     name = models.charfield(max_length=300, blank=false, null=false)     phone_number = models.positiveintegerfield(null=false, blank=false)   class contact(models.model):     buyer = models.foreignkey(user)     rental = models.foreignkey(rental, related_name="rent")     email_id = models.emailfield(blank=false, null=false)  class rentalserializer(serializers.modelserializer):     user = serializers.readonlyfield(source='user.username')     galleries = galleryserializer(many=true)     class meta:         model = rental         fields = ('__all__')  class contactserializer(serializers.modelserializer):     # buyer = userserializer(many=true)     # rent = rentalserializer(many=true)     class meta:         model = contact         fields = '__all__'  class contactedrent(apiview):     serializer_class = contactserializer     def get(self, request, token=none, format=none):         """         returns list of contactedrent         """         reply = {}         try:             contacted_rent = contact.objects.filter(buyer_id=request.user.id)             if token:                 specific_contacted_rent = contacted_rent.get(rental__id=token)                 reply['data'] = self.serializer_class(specific_contacted_rent).data             else:                 print ('no token')                 reply['data'] = self.serializer_class(contacted_rent, many=true).data         except contact.doesnotexist:             return error.requestedresourcenotfound().as_response()         except:             return error.unknownerror().as_response()         else:             return response(reply, status.http_200_ok)      def post(self, request, token=none, format=none):         """         create new contact rent         """         rent = none         if not token none:             try:                 # contact = contact.objects.get(buyer_id=request.user.id, rental__id=token)                 rent = rental.objects.get(id=token)             except rental.doesnotexist:                 return requestedresourcenotfound().as_response()             except:                 return unknownerror.as_response()         serialized_data = self.serializer_class(instance=rent, data=request.data, partial=true)         if serialized_data.is_valid():             serialized_data.save()         else:             return error.validationerror(serialized_data.errors).as_response()  

please me understand create , post function of apiview example.

update

url(r'^contact/(?p<token>[0-9a-z]+)/$', contactedrent.as_view(), name="create-contact-to-rent"), 

here token id of rent going contacted giving buyer information(email_id)

since don't want rental passed submitted data, might want remove serializer.

class contactserializer(serializers.modelserializer):     class meta:         model = contact         fields = ['buyer', 'email_id']  # omit `rental` field 

the partial argument serializer patch requests, when want validate submitted fields, and update fields. (i feel you're passing partial=true workaround because serializer doesn't validate if don't pass rental.)

once you've removed rental serializer, should able do:

serialized_data = self.serializer_class(data=request.data) 

which should validate, given passed valid buyer , email_id values. then, when call save, pass rental instance

serialized_data.save(rental=rent) 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -