So I have a fairly straightforward serializer in serializers.py
class ScheduleSerializer(serializers.ModelSerializer):
class Meta:
model = FrozenSchedule
fields = ['startDate', 'endDate', 'client', 'url']
startDate = serializers.DateField(source='start_date')
endDate = serializers.DateField(source='end_date')
client = serializers.StringRelatedField(many=False)
url = serializers.URLField(source='get_absolute_url')
get_absolute_url in my models.py
def get_absolute_url(self):
return reverse('reports:frozenschedule-detail', kwargs={
'slug': self.client.slug, 'pk': self.id
})
And it's related ViewSet in viewsets.py
class ScheduleViewSet(viewsets.ReadOnlyModelViewSet):
queryset = FrozenSchedule.objects.not_abandoned().future()\
.filter(signed=False).order_by('start_date')
serializer_class = serializers.ScheduleSerializer
It returns JSON which looks like this:
[
{
"startDate": "2016-10-01",
"endDate": null,
"client": "Abscissa.Com Limited",
"url": "/clients/abscissac/frozenschedule/1",
}
]
But I'd like it to return the complete URL, not just the relative path
[
{
"startDate": "2016-10-01",
"endDate": null,
"client": "Abscissa.Com Limited",
"url": "http://localhost:8000/clients/abscissac/frozenschedule/1",
}
]
Could I serialize URL's this way inside my Serializer?
The Restful documentation states that the rest_framework reverse function does exactly what I need. But it requires the request object to build the UR
http://www.django-rest-framework.org/api-guide/reverse/
Override HyperlinkedIdentityField. It has the following method,
get_url(self, obj, view_name, request, format)
which can be used to map the object instance to its URL representation. ie,
class UrlHyperlinkedIdentityField(HyperlinkedIdentityField):
def get_url(self, obj, view_name, request, format):
if obj.pk is None:
return None
return self.reverse(view_name,
kwargs={
'slug': obj.client.slug,
'pk': obj.id,
},
request=request,
format=format,
)
Then in serializers.py :
url = UrlHyperlinkedIdentityField(view_name='reports:frozenschedule-detail')
Related
Whenever i called my booking function with a post method in that it throws me an error of 'Invalid Data Type it expected dict but its an int'. So when i debug that i found a problem in my serializers so Please help me to resolve the error
model.py
class Booking(models.Model):
details = models.ForeignKey(PersonalDetails, on_delete=models.CASCADE)
quantity = models.IntegerField(null=False)
total_amount = models.IntegerField()
show = models.ForeignKey(ShowTime, on_delete=models.CASCADE)
def __str__(self) -> str:
return self.total_amount
views.py
class BookingTicketsQuery(APIView):
permission_classes = [IsAuthenticated]
def get(self, request, format=None):
booking_id = request.query_params.get('booking_no', None)
if booking_id is not None:
queryset = Booking.objects.get(id=booking_id)
else:
return Response("invalid", status=status.HTTP_409_CONFLICT)
seriliazer = BookingSerializer(queryset)
return Response(seriliazer.data)
def post(self, request, format=None):
recieve_data = JSONParser().parse(request)
showtime = ShowTime.objects.get(id=recieve_data['show'])
print(showtime)
if recieve_data['quantity'] > (showtime.total_seats - showtime.booked_seats):
return Response("Error: No seats available", status=status.HTTP_409_CONFLICT)
recieve_data['total_amount'] = showtime.total_price * \
recieve_data['quantity']
showtime.booked_seats += recieve_data['quantity']
showtime.save()
serializer = BookingSerializer(data=recieve_data)
if serializer.is_valid():
booking_obj = serializer.save()
return Response(booking_obj.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
serializers.py
class ShowMovieSerializer(serializers.ModelSerializer):
cinema = CinemaSerializer()
movie = MovieSerializer()
class Meta:
model = ShowTime
fields = ('id', 'show_start_time', 'show_end_time',
'total_seats', 'booked_seats', 'cinema', 'movie')
class BookingSerializer(serializers.ModelSerializer):
show = ShowMovieSerializer()
details = PersonalDetailsSerializer()
class Meta:
model = Booking
fields = ('id', 'total_amount', 'quantity', 'details', 'show')
traceback
Bad Request: /api/v1/bookings/
[19/Jul/2021 12:07:20] "POST /api/v1/bookings/ HTTP/1.1" 400 168
{
"details": {
"non_field_errors": [
"Invalid data. Expected a dictionary, but got int."
]
},
"show": {
"non_field_errors": [
"Invalid data. Expected a dictionary, but got int."
]
}
}
my post data:
{
"details": 1,
"quantity":1,
"show":1
}
You added a ShowMovieSerializer() and PersonalDetailsSerializer(). Therefore drf expects data that represents both models (nested data). What you have to do instead of posting { "details": 1, "quantity":1, "show":1 }-data you have to adjusted your data that represents your model. You are posting data with a pk of your Movie and Show.
For instance:
{
"id":....,
"total_amount": ....,
....
"movie": {
"title": ...., # fields are only for explanation
"length": ....
},
"show": {
"title": ....,
"actor": ....
}
}
If you only want to establish a relationship between you ShowTime and Booking you have to remove your serializers.
dealing with nested data
I m working on this POST request in drf and I lost it somewhere please help.
my models.py
class TargetDefination(models.Model):
targetName=models.CharField(max_length=50)
displayName=models.CharField(max_length=100)
def __str__(self):
return self.targetName
class Target(models.Model):
targetDefn=models.ForeignKey(TargetDefination,on_delete=models.CASCADE)
roleId=models.ForeignKey(Role,on_delete=models.CASCADE)
empId=models.ForeignKey(Employee,on_delete=models.CASCADE)
startDate= models.DateField(default=datetime.date.today)
endDate= models.DateField(null=True,blank=True)
value=models.PositiveIntegerField(default=0)
def __str__(self):
return str(self.empId) + ' ' +str(self.targetDefn)
serializer.py
class TargetSerializers(serializers.ModelSerializer):
targetDefn=TargetDefinationSerializers()
roleId=RoleSerializers()
empId=OnlyEmployeeSerializers()
class Meta:
model = Target
fields = (
'id',
'targetDefn',
'roleId',
'empId',
'startDate',
'endDate',
'value'
)
and this is what I have tried:
views.py
#api_view(['GET','POST'])
def setTarget(request, *args, **kwargs):
if request.method=='GET':
setTrgt=Target.objects.all()
serial=TargetSerializers(setTrgt,many=True)
return JsonResponse(serial.data,safe=False)
elif request.method == 'POST':
data=JSONParser().parse(request)
serial=TargetSerializers(data=data)
if serial.is_valid():
print("working")
target = serial.save()
serializer = TargetSerializers(target)
return JsonResponse(serializer.data,status=status.HTTP_200_OK,safe=False)
return JsonResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
I need to create POST request and the format will be:
{
"id": 6,
"targetDefn": {
"id": 1,
"targetName": "MIN SALES",
"displayName": "MIN SALES"
},
"roleId": {
"id": 1,
"roleName": "CEO",
"description": "Chief executive officer",
"roleReportsTo": null,
"roleReportsToName": null
},
"empId": {
"id": 5,
"empName": "Emp05",
"startDate": "2021-05-04",
"termDate": null
},
"startDate": "2021-05-05",
"endDate": null,
"value": 123
}
this is an example with just values in it.
errors I m getting:
The `.create()` method does not support writable nested fields by default.
Write an explicit `.create()` method for serializer `employee.serializers.TargetSerializers`, or set `read_only=True` on nested serializer fields.
You will have to implement a create() method in your serializer. The implementation of this method should break the request data and then write it into its respective models.
Here is how you can implement the method -
def create(self, validated_data):
#split the objects into multiple objects.
targetDef = validated_data.pop(targetDefn)
#save the objects into its respective models.
targetDefId = TargetDefination.objects.create(**targetDef)
#get the objects of roleId and empID
role = list(validated_data['roleId'].items())
role_id = Role.objects.get(roleName =role[0][1])
emp_id = Employee.objects.get(pk=validated_data['empId']['id'])
target_obj = Target.object.create(targetDef=targetDefId, roleId=role_id, empID=emp_id, startDate=validated_data['startDate'], endDate=validated_data['endDate'], value=validated_data['value'])
return target_obj
I want to add 'isSuccess' at the pagination returns.
For example,
{
"count": 1234,
"next": "http://mydomain/?page=2",
"previous": null,
"isSuccess" : 'Success' # <---- this one
"results": [
{
'id':123,
'name':'abc'
},
{
'id':234,
'name':'efg'
},
...
]
}
I found this way but It didn't work. How can I add new values at Django pagination return?
this is my try:
class Testing(generics.GenericAPIView):
queryset = Problem.objects.all()
serializer_class = userSerializer
pagination_class = userPagination
def get(self, request):
queryset = self.get_queryset()
page = self.request.query_params.get('page')
if page is not None:
paginate_queryset = self.paginate_queryset(queryset)
serializer = self.serializer_class(paginate_queryset, many=True)
tmp = serializer.data
tmp['isSuccess'] = 'Success'
return self.get_paginated_response(tmp)
Give this a try, instead of adding isSuccess to the serializer.data, add it to get_paginated_response().data
def get(self, request):
queryset = self.get_queryset()
page = self.request.query_params.get('page')
if page is not None:
paginate_queryset = self.paginate_queryset(queryset)
serializer = self.serializer_class(paginate_queryset, many=True)
tmp = serializer.data
# tmp['isSuccess'] = 'Success'
response = self.get_paginated_response(tmp)
response.data['isSuccess'] = "Success"
return Response(data=response.data, status=status.HTTP_200_OK)
return Response(data="No Page parameter found", status=status.HTTP_200_OK)
The response will be something like
{
"count": 1234,
"next": null,
"previous": null,
"results": [
{
'id':123,
'name':'abc'
},
...
],
"isSuccess" : 'Success'
}
You can try overriding the get_paginated_response method in the PageNumberPagination
Example:
from rest_framework.pagination import PageNumberPagination
from collections import OrderedDict
class CustomPagination(PageNumberPagination):
def get_paginated_response(self, data):
return Response(OrderedDict([
('count', self.page.paginator.count),
('next', self.get_next_link()),
('previous', self.get_previous_link()),
('results', data),
('isSuccess', "Success") # extra
]))
I'm currently trying to make a post request with only data field in models.py:
class Filter(models.Model):
class Meta:
verbose_name_plural = "Filter"
verbose_name = "Filter"
db_table= "listing_filter"
data = JSONField(default={})
user = models.ForeignKey('backend.user', on_delete=models.CASCADE)
I have the following Serializer , i use JSONField following the drf document docs:
class FilterSerializer(serializers.ModelSerializer):
data = serializers.JSONField(required=True)
user = serializers.CharField(required=False)
class Meta:
model = Filter
fields = ('__all__')
and use it in the APIView:
class FilterView(APIView):
def post(self, request):
login_user = request.user
received_json_data=json.loads(request.body)
valid_ser = FilterSerializer(data=received_json_data)
if valid_ser.is_valid():
post_data = received_json_data["data"]
filter = Filter.objects.create(data=post_data, user=login_user)
filter.save()
return JsonResponse({'code':'200','data': filter.id}, status=200)
else:
return JsonResponse({'code':'400','errors':valid_ser.errors}, status=400)
When i send the following data in body it worked and saved the object:
{
"data": {
"http://example.org/about": {
"http://purl.org/dc/terms/title": [
{
"type": "literal",
"value": "Anna's Homepage"
}
]
}
}
}
But when i send data as a string(which is not a json object) it still save, how do i prevent this ?
{
"data" : "abc"
}
I'm using Django rest framework to retrieve data from server.
Now I create a view:
class Snipped(APIView):
authentication_classes = (authentication.SessionAuthentication,)
permission_classes = (permissions.AllowAny,)
##ensure_csrf_cookie
def get(self, request, format=None):
request.session["active"] = True
snippets = Snippet.objects.all()
serializer = SnippetSerializer(snippets, many=True)
return JsonResponse(serializer.data, safe=False)
def post(self, request, format=None):
serializer = SnippetSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
the model is this:
# Create your models here.
class Snippet(models.Model):
created = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=100, blank=True, default='')
code = models.TextField()
def __str__(self):
return self.title
so at this point I'd like to know metadata of data passed to this endpoint, I 've found http://www.django-rest-framework.org/api-guide/metadata/
but if a send OPTION I don't obtain informations about data but only this json answer
{
"name": "Snipped",
"description": "",
"renders": [
"application/json",
"text/html"
],
"parses": [
"application/json",
"application/x-www-form-urlencoded",
"multipart/form-data"
],
}
without (see list at http://www.django-rest-framework.org/api-guide/metadata/)
"parses": [
"application/json",
"application/x-www-form-urlencoded",
"multipart/form-data"
],
"actions": {
"POST": {
"note": {
"type": "string",
"required": false,
"read_only": false,
"label": "title",
"max_length": 100
}
}
}
any idea how can achieve above results with an APIView ?
Actions will only work if the view defines a get_serializer.
You need to define it so you can benefit from the automatic schema generation and return a SnippetSerializer instance.
Refer to generic views to see an implementation example.