2

I have an installation of Django 1.10 and Django REST framework.

I am getting POST requests well, but I would like it before the content is created, doing some tasks with the fields, I have the following in my views.py file

from rest_framework.decorators import detail_route from rest_framework import serializers from suds.client import Client import base64 from . import views # Create your views here. from cfdipanel.models import Report, Invoice, UserProfile from cfdi.serializers import ReportSerializer, InvoiceSerializer, UserProfileSerializer class ReportViewSet(viewsets.ModelViewSet): queryset = Report.objects.all() serializer_class = ReportSerializer @detail_route(methods=['post']) def set_invoice(self, request, pk=None): #get report object my_report = self.get_object() serializer = InvoiceSerializer(data=request.data) if serializer.is_valid(): serializer.save(report=my_report) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) 

And this a fragment of my file serializer.py

from rest_framework import serializers from cfdipanel.models import Report, Invoice, UserProfile class UserProfileSerializer(serializers.ModelSerializer): class Meta: model = UserProfile fields = ('user', 'karma') class InvoiceSerializer(serializers.ModelSerializer): class Meta: model = Invoice fields = ('uuid', 're', 'rr', 'tt', 'emision_date', 'type_invoice', 'status', 'owner') class ReportSerializer(serializers.ModelSerializer): owner = UserProfileSerializer(read_only=True) ownerId = serializers.PrimaryKeyRelatedField(write_only=True, queryset=UserProfile.objects.all(), source='owner') invoices = InvoiceSerializer(many=True, read_only=True, source='invoice_set') class Meta: model = Report fields = ('id', 'title', 'body', 'owner', 'ownerId', 'invoices') 

And this other is a snippet of models.py

class Invoice(models.Model): owner = models.ForeignKey(UserProfile) report = models.ForeignKey(Report) uuid = models.CharField(max_length=36) emision_date = models.CharField(max_length=28) re = models.CharField(max_length=13) rr = models.CharField(max_length=13) type_invoice = models.CharField(max_length=10) status = models.CharField(max_length=2, blank=True) tt = models.DecimalField(max_digits=19, decimal_places=9, default=Decimal(0)) def __str__(self): return self.uuid 

The status field comes empty, to fill it I want to do something similar to the following code in view.py before the fields are saved:

url = 'https://an-api.com/ConsultService.svc?wsdl' client = Client(url) string = "?re=" + re + "&rr=" + rr + \ "&tt=" + tt + "&id=" + uuid response = client.service.Consulta(string) # Create content for field status status = response.Status 

And after you get the response from the webservice then save and create the Invoice content.

2 Answers 2

1

request.datareturns a dict instance, and InvoiceSerializer returns an Invoice "instance", so you can modify the data before of after deserializing it. using:

data = request.data data['field'] = 'foo' 

or

serializer = InvoiceSerializer(data=request.data) serializer.object.invoice_field = 'foo' 

for DRF 3

serializer = InvoiceSerializer(data=request.data) serializer.save(invoice_field ='foo') 
Sign up to request clarification or add additional context in comments.

2 Comments

Show error: AttributeError: This QueryDict instance is immutable
the problem is used header Content-Type: application/x-www-form-urlencoded delete header and work fine!!
1

This may help you:

Try to print request.data

You can access the each field send via request using request.data['keyName'] it will print the value send in key called keyName.so that you can manupulate the request and then sent it to serializer.

6 Comments

print request.data <QueryDict: {'------WebKitFormBoundaryxzOtd3tWkoWOdACG\r\nContent-Disposition: form-data': [''], ' name': ['"owner"\r\n\r\n2\r\n------WebKitFormBoundaryxzOtd3tWkoWOdACG\r\nContent-Disposition: form-data', '"uuid"\r\n\r\n72DB605A-CCD1-4FA5-96ED-C8E8A22258D7\r\n------WebKitFormBoundaryxzOtd3tWkoWOdACG\r\nContent-Disposition: form-data', '"re"\r\n\r\nAER990218E83\r\n------WebKitFormBoundaryxzOtd3tWkoWOdACG\r\nContent-Disposition: form-data', '"rr"\r\n\r\nOFF151023V9A\r\n------WebKitFormBoundaryxzOtd3tWkoWOdACG\r\nContent-Disposition: form-data', and more ....
@laur , so now you can access any data using data['keyName'] you want and manipulate it before passing to the serializer.
print (data['uuid']) y show raise MultiValueDictKeyError(repr(key)) django.utils.datastructures.MultiValueDictKeyError: "'uuid'"
sorry I think inside name everything is in character so first just serialize th request.data properly then try to access it.
Do you advise me to access the data after serializing? print (['name']['uuid']) show TypeError: list indices must be integers or slices, not str
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.