How do I update data in a linked django rest model
Here is my json
{
"doc_type": 1,
"warehouse": 4,
"date": "2022-06-09",
"number": 980,
"contragent": 2,
"comment": "testcom;mgment",
"items": [
{
"product": 7,
"buy_price": "168.00",
"sell_price": "500.00",
"quantity": 17
},
{
"product": 8,
"buy_price": "168.00",
"sell_price": "500.00",
"quantity": 17
}
]
}
I can't change the data in the "items" dictionary
If I submit only one "product" element, will the other one be deleted? And if, for example, three, will it be added?
this is what I have at the moment, records are created without problems, but I can only update the "ConsignmentNote" model
#serialisers.py
class ConsignmentNoteSerializer(serializers.ModelSerializer):
creator = serializers.HiddenField(default=serializers.CurrentUserDefault())
doc_type = serializers.IntegerField(read_only=True)
# contragent_detail = ContragentSerializer(source='contragent', read_only=True)
items = ConsignmentItemSerializer(many=True)
class Meta:
model = ConsignmentNote
fields = ['doc_type', "warehouse", 'date', 'number', 'contragent', 'comment', 'creator', 'items']
read_only_fields = ['id' ]
def create(self, validated_data):
items = validated_data.pop('items')
note = ConsignmentNote.objects.create(**validated_data, doc_type = 1)
for item in items:
product = item.pop('product')
item = ConsignmentItem.objects.create(consignmentnote=note, product=product ,**item)
return note
def update(self, instance, validated_data):
instance.date = validated_data.pop('date', instance.date)
instance.comment = validated_data.pop('comment', instance.comment)
return instance
Related
I'm hoping someone might be able to help me. I am working with Django Rest Framework, and attempting to create an API that allows users to search for Providers that provide specific Procedures in particular Regions, and only return the relevant details.
Set up
I have these models (heavily simplified):
# models.py
from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator
class Region(models.Model):
name = models.CharField(max_length=100)
class Meta:
ordering = ["name"]
class Procedure(models.Model):
description = models.CharField(max_length=100)
class Meta:
ordering = ["description"]
class Provider(models.Model):
provider_name = models.CharField(max_length=200)
class Meta:
ordering = ["provider_name"]
class Provision(models.Model):
fk_procedure = models.ForeignKey(
Procedure,
related_name="providers",
on_delete=models.RESTRICT,
)
fk_provider = models.ForeignKey(
Provider,
related_name="services",
on_delete=models.CASCADE,
)
discount = models.FloatField(
validators=[MaxValueValidator(100), MinValueValidator(0)],
default=0,
)
class Meta:
ordering = ["-discount"]
unique_together = ["fk_procedure", "fk_provider"]
class ProvisionLinkRegion(models.Model):
fk_provision = models.ForeignKey(
Provision,
related_name="regions",
on_delete=models.CASCADE,
)
fk_region = models.ForeignKey(
Region,
related_name="services",
on_delete=models.RESTRICT,
)
location = models.BooleanField(default=False)
As you can see, there is a ManyToMany link between Provision and Region via ProvisionLinkRegion. I haven't defined this as a ManyToMany field though, as I need to store additional details (location) about the pairing.
I have defined the following serializers on these models:
# serializers.py
from rest_framework import serializers
from models import (
Provider,
Region,
Procedure,
ProvisionLinkRegion,
Provision,
)
class ProvisionLinkRegionSerializer(serializers.ModelSerializer):
class Meta:
model = ProvisionLinkRegion
fields = ["fk_region", "location"]
class ProvisionDetailsSerializer(serializers.ModelSerializer):
regions = ProvisionLinkRegionSerializer(many=True)
class Meta:
model = Provision
fields = ["fk_procedure", "discount", "mff_opt_out", "regions"]
class ProviderProvisionSerializer(serializers.ModelSerializer):
services = ProvisionDetailsSerializer(many=True)
number_services = serializers.IntegerField()
class Meta:
model = Provider
fields = [
"provider_name",
"services",
"number_services",
]
And have defined my API like this:
# api.py
from django.db.models import Prefetch, Count
from rest_framework import generics, pagination, permissions, status
from rest_framework.response import Response
from serializers import ProviderProvisionSerializer
from models import (
Provider,
ProvisionLinkRegion,
Provision,
)
class CustomPagination(pagination.PageNumberPagination):
page_size_query_param = "limit"
def get_paginated_response(self, data):
return Response(
{
"pagination": {
"previous": self.get_previous_link(),
"next": self.get_next_link(),
"count": self.page.paginator.count,
"current_page": self.page.number,
"total_pages": self.page.paginator.num_pages,
"items_on_page": len(data),
},
"results": data,
}
)
class ProvisionListAPI(generics.ListAPIView):
permission_classes = [permissions.IsAuthenticated]
serializer_class = ProviderProvisionSerializer
pagination_class = CustomPagination
def get_queryset(self):
queryset = Provider.objects.distinct()
# Extract the query parameters
params = self.request.query_params
region_list = params["region"].split(",")
procedure_list = param["procedure"].split(",")
# Build up the prefetch Provision table filtering on the regions
# and services
services_prefetch_qs = (
Provision.objects.distinct()
.filter(regions__fk_region__in=region_list)
.filter(fk_procedure__in=procedure_list)
.prefetch_related(
Prefetch(
"regions",
queryset=ProvisionLinkRegion.objects.filter(
fk_region__in=region_list
),
)
)
)
# Apply the filters and prefetch required tables
queryset = queryset.filter(
services__regions__fk_region__in=region_list
).prefetch_related(
Prefetch("services", queryset=services_prefetch_qs),
)
# Add the ordering parameters
queryset = (
queryset.annotate(
number_services=Count("services", distinct=True) # FIXME
)
.filter(number_services__gt=0)
.order_by("-number_services")
)
return queryset.all()
def list(self, response):
# Check it has the right headers
params = self.request.query_params
if "procedure" not in params:
return Response(
{"detail": "procedure not provided as a query parameter"},
status.HTTP_400_BAD_REQUEST,
)
if "region" not in params:
return Response(
{"detail": "region not provided as a query parameter"},
status.HTTP_400_BAD_REQUEST,
)
# Paginate and filter queryset
queryset = self.get_queryset()
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
My problem is, the number_services value is not correct, as it's not doing the count on the full filtered results. It's only doing it on the pre-filtered ones (although the region filter does work). I also don't want any providers to appear when they don't have any services (hence the .filter(number_services__gt=0)).
I think that it's related to not filtering on the main Provider queryset like I do with the region i.e. to include:
queryset = queryset.filter(services__fk_procedure__in=procedure_list)
But when I include this, it doesn't actually remove the services, but just the providers that don't provide ANY of those services, so the count is still off.
Example
If my data with no filtering or prefetching looks like this:
"results": [
{
"provider_name": "Provider 2.0",
"services": [
{
"fk_procedure": 3,
"discount": 0.05,
"regions": [
{
"fk_region": 1,
"location": true
},
{
"fk_region": 2,
"location": false
}
{
"fk_region": 3,
"location": true
}
]
},
{
"fk_procedure": 5,
"discount": 0.05,
"regions": [
{
"fk_region": 1,
"location": true
}
]
}
]
},
{
"provider_name": "Test Provider",
"services": [
{
"fk_procedure": 2,
"discount": 0.00,
"regions": [
{
"fk_region": 1,
"location": true
}
]
}
]
}
]
If I then run this on it:
GET /api/v1/provision?page=1&limit=10®ion=1,3&services=3`
I want to show all the providers and the relevant details where they relate to either region 1 or 3, and procedure 3.
Actual Result
{
"pagination": {
"previous": null,
"next": null,
"count": 2,
"current_page": 1,
"total_pages": 1,
"items_on_page": 2
},
"results": [
{
"provider_name": "Provider 2.0",
"services": [
{
"fk_procedure": 3,
"discount": 0.05,
"regions": [
{
"fk_region": 1,
"location": true
},
{
"fk_region": 3,
"location": true
}
]
}
],
"number_services": 2
},
{
"provider_name": "Test Provider",
"services": [],
"number_services": 1
}
]
}
Desired Result
{
"pagination": {
"previous": null,
"next": null,
"count": 2,
"current_page": 1,
"total_pages": 1,
"items_on_page": 2
},
"results": [
{
"provider_name": "Provider 2.0",
"services": [
{
"fk_procedure": 3,
"discount": 0.05,
"regions": [
{
"fk_region": 1,
"location": true
},
{
"fk_region": 3,
"location": true
}
]
}
],
"number_services": 1,
}
]
}
Things I've tried
SerializerMethodField
I've been able to get number_services to work using a SerializerMethodField by including:
# serializers.py
class ProviderProvisionSerializer(serializers.ModelSerializer):
...
number_services = serializers.SerializerMethodField()
...
class Meta:
...
def get_number_services(self, obj):
return obj.services.count()
Unfortunately though, I can't use this for ordering, or filtering within the API, and I also can't use it with pagination either, so is pretty useless for what I need it for.
Subquery
In the get_queryset method for the API, I've also tried using what I currently have as a subquery, and using the main queryset where the ID is in the other one, but then I lose all the prefetch from the first subquery, and have regions and services that don't relate to my filter.
## TLDR
How do I filter a queryset in a get_queryset method for a ListAPIView on properties of the children of the main model, and be able to return a count of the remaining children after the filter has taken place?
I'm using the Writable Nested Serializer to serialize my request. I had no problem serializing doing PUT/POST when the data is nested in 1 layer.
(i.e. {name:'personA', project:{ name:'projA', client:'clientA'}})
However, I ran into a problem when it is nested in 2 layers - I couldn't figure out on how to modify the update() function. Please help!
data sample
{
"id": 6106,
"name": {
"id": 213,
"name": "personA"
},
"project": {
"id": 1663,
"project": "ProjectA",
"client": {
"id": 72,
"name": "ClientA"
},
"project_manager": {
"id": 32,
"name": "personB"
}
},
"booking": 100,
"date": "2017-12-01"
}
serializers.py
class projectSerializer(serializers.ModelSerializer):
client = clientSerializer()
project_manager = userSerializer()
class Meta:
model = project
fields = ('id', 'project', 'client', 'project_manager')
class bookingListSerializer(serializers.ModelSerializer):
project = projectSerializer()
name = userSerializer()
class Meta:
model = bookingList
fields = ('id', 'name', 'project', 'booking', 'date')
def update(self, instance, validated_data):
project_data = validated_data.pop('project')
name_data = validated_data.pop('name')
try:
project_instance = project.objects.filter(**project_data)[0]
name_instance = user.objects.filter(**name_data)[0]
except IndexError:
raise serializers.ValidationError
# update the project if request is valid
instance.project = project_instance
instance.name = name_instance
instance.save()
return instance
views.py
# other viewsets...
class bookingListViewSet(viewsets.ModelViewSet):
queryset = bookingList.objects.all()
serializer_class = bookingListSerializer
I only want to update all order in my_stage by using PUT, something like:
"Payload":
{
"stages": [
{
"stage_id": 1,
"position": 2
},
{
"stage_id": 2,
"position": 3
},
{
"stage_id": 3,
"position": 4
}
]
}
and "Response":
{
"stages": [
{
"stage_id": 1,
"position": 2
},
{
"stage_id": 2,
"position": 3
},
{
"stage_id": 3,
"position": 4
}
]
}
But I don't have "stages" in my model so I cannot use ModelSerializer. But I have to implement create() first.
So what should I do to implement update? Thank you.
My stage model is:
class Stage(models.Model):
class Meta:
db_table = 'stage'
position = models.IntegerField()
Here are my serialzier.py
class StagePositionSerializer(ModelSerializer):
"""Serialize order"""
# company_id = IntegerField(read_only=True)
stage_id = IntegerField(source='id', read_only=True)
position = IntegerField(min_value=0)
class Meta:
model = Stage
fields = [
'stage_id',
'position'
]
class PositionSerializer(Serializer):
stages = StagePositionSerializer(many=True)
and my view.py
class StagePositionListView(APIView):
serializer_class = PositionSerializer
If you only want to have "stages:" prepended to your data you can create a custom endpoint in the viewset and specify the formatting you want returned.
I'm not sure why you have a model serializer as well as a non-model serializer. What does that buy you?
Here is some sample code which would add 'get_stages' to your api url.
views.py:
class StagePositionViewSet(viewsets.ModelViewSet):
queryset = Stage.objects.all()
serializer_class = StagePositionSerializer
#list_route(methods=['GET'])
def get_stages(self, request, **kwargs):
try:
stage_list = Stage.objects.all()
serializer = StagePositionSerializer(stage_list , many=True)
results = dict()
#this is where you add your prepended info
results['stages'] = serializer.data
return Response(results, status=status.HTTP_200_OK)
except Exception as e:
return Response(e, status=status.HTTP_400_BAD_REQUEST)
Then if you perform a GET at the URL whatever_your_url/is/get_stages you will get back the payload format you want.
You could easily take advantage of serializer(many=True), which will match a list of serializers. Your serializer would be:
class StagePositionSerializer(ModelSerializer):
class Meta:
model = Stage
fields = [
'id',
'position'
]
class PositionSerializer(Serializer):
stages = StagePositionSerializer(many=True)
I am trying to make a writable nested serializer in DRF3.
I have a model Concert with a m2m field 'technicians' to my User-model.
I have successfully added a list of the Users that is connected to the Concert instance in it's view. Now I want to be able to add technicians/Users to the Concert-model.
This is my serializer so far:
class ConcertListSerializer(serializers.ModelSerializer):
technicians = UserDetailSerializer(
many=True,
read_only=True
)
class Meta:
model = models.Concert
fields = [
'name',
'date',
'technicians',
'id',
]
def create(self, validated_data):
# list of pk's
technicians_data = validated_data.pop('technicians')
concert = Concert.object.create(**validated_data)
for tech in technicians_data:
tech, created = User.objects.get(id = tech)
concert.technicians.add({
"name": str(tech.name),
"email": str(tech.email),
"is_staff": tech.is_staff,
"is_admin": tech.is_admin,
"is_superuser": tech.is_superuser,
"groups": tech.groups,
"id": tech.id
})
return concert
I want to be able to just add a list of the pk/id of the technicians I want to add. So for example:
"technicians": [1,2,3]
adds user 1, 2, 3 to Concert's technicians-field.
Whenever I do this, I get KeyError that just says 'technicians' and refers to the first line in my create() function...
The fields I am adding in a dictionary are all the fields of the User-model. That is the format they are displayed when I do a GET-request.
This is the Concert-model:
class Concert(models.Model):
name = models.CharField(max_length=255)
date = models.DateTimeField(default =
datetime.now(pytz.timezone('Europe/Oslo'))
+ timedelta(days=30)
)
technicians = models.ManyToManyField(User) # relation to user model
Edit:
This is the response of a GET-request on a premade example-concert:
{
"name": "Concert-name",
"date": "2017-10-28T12:11:26.180000Z",
"technicians": [
{
"name": "",
"email": "test2#test.com",
"is_staff": true,
"is_admin": true,
"is_superuser": false,
"groups": [
5
],
"id": 2
},
{
"name": "",
"email": "test3#test.com",
"is_staff": true,
"is_admin": true,
"is_superuser": false,
"groups": [
5
],
"id": 3
}
],
"id": 1
}
you should get data from context request, because you filed is read only it is missing in the validated_data
def create(self, validated_data):
# list of pk's
# instaed of technicians_data = validated_data.pop('technicians')
# use next two lines
request = self.context.get('request')
technicians_data = request.data.get('technicians')
concert = Concert.object.create(**validated_data)
# Added technicians
for tech in technicians_data:
user = User.objects.get(id=tech)
concert.technicians.add(user)
return concert
I have the following setup in Django REST Framework:
models.py:
class Device(models.Model):
name = models.CharField()
latitude = models.FloatField()
longitude = models.FloatField()
altitude = models.FloatField()
class Event(models.Model):
id_device = models.ForeignKey(Device, related_name='events')
name = models.CharField()
date = models.CharField()
time = models.CharField()
class Values(models.Model):
id_device = models.ForeignKey(Device)
id_event = models.ForeignKey(Event, related_name='values')
x = models.FloatField()
y = models.FloatField()
z = models.FloatField()
time = models.IntegerField()
serializers.py:
class DeviceSerializer(serializers.ModelSerializer):
events = EventSerializer(many=True)
class Meta:
model = Device
fields = ('url', 'id', 'name', 'latitude', 'longitude', 'altitude', 'events')
def create(self, validated_data):
events_data = validated_data.pop('events')
device = Device.objects.create(**validated_data)
for event in events_data:
Events.objects.create(device=device, **event)
return device
class EventSerializer(serializers.ModelSerializer):
values = ValuesSerializer(many=True)
class Meta:
model = Event
fields = ('url', 'id', 'name', 'date', 'time', 'values')
def create(self, validated_data):
values_data = validated_data.pop('values')
event = Event.objects.create(**validated_data)
for value in values_data:
Values.objects.create(event=event, **value)
return event
class ValuesSerializer(serializers.ModelSerializer):
class Meta:
model = Values
fields = ('x', 'y', 'z', 'time')
When I try to post an event with some values assigned using a JSON file like this one:
{
"name": "name_example",
"date": "date_example",
"time": "time_example",
"values": [
{
"x": 1,
"y": 2,
"z": 3,
"time": 1
},
{
"x": 10,
"y": 20,
"z": 30,
"time": 2
},
{
"x": 100,
"y": 200,
"z": 300,
"time": 4
}
]
}
I get the error IntegrityError: NOT NULL constraint failed: drf_event.id_device_id
I'm new to this framework, so what can I do in order to post new events with values assigned to an existing device?
You haven't a key pointing to device in your EventSerializer. You miss the id_device.
class EventSerializer(serializers.ModelSerializer):
values = ValuesSerializer(many=True)
class Meta:
model = Event
fields = ('id_device', 'url', 'id', 'name', 'date', 'time', 'values')
And you need to add the key in your json:
{
"id_device": 1,
"name": "name_example",
"date": "date_example",
"time": "time_example",
"values": [
{
"x": 1,
"y": 2,
"z": 3,
"time": 1
},
{
"x": 10,
"y": 20,
"z": 30,
"time": 2
},
{
"x": 100,
"y": 200,
"z": 300,
"time": 4
}
]
}