Dictinct Row in queryset using Django Rest Framework - django

I want to remove the duplicate row, distinct row in a Mysql DB.
"Prpk" has a relation with "MeteoZone" (meteozone).
I've a model like this :
class MeteoZone(models.Model):
name = models.CharField(max_length=45, unique=True)
communes = models.ManyToManyField(Commune, related_name='meteozone', blank=True)
massifs = models.ManyToManyField(Massif, related_name='meteozone', blank=True)
def __str__(self):
return self.name
class Prpk(models.Model):
begin = models.FloatField()
end = models.FloatField()
status = models.CharField(max_length=15)
# Relations
report = models.ForeignKey(Report, on_delete=models.PROTECT, unique=False)
meteozone = models.OneToOneField(MeteoZone, related_name='prpk', on_delete=models.PROTECT, null=False, unique=False)
highway = models.OneToOneField(Highway,related_name='prpk', on_delete=models.PROTECT, null=True, unique=False)
department = models.OneToOneField(Department,on_delete=models.PROTECT, null=True, unique=False)
def __str__(self):
return ("%s-%s" % (self.begin,self.end))
And a serializer :
class PrpkSerializer(serializers.ModelSerializer):
# Get 'meteozones', 'communes' and 'massifs'
meteozone = MeteozoneSerializer(read_only=True)
begin = serializers.FloatField()
end = serializers.FloatField()
status = serializers.CharField(max_length=15)
class Meta:
model = Prpk
fields = ('begin', 'end', 'status', 'meteozone')
ordering = ('begin', )
In my view, in the query_set, I want to display only the unique records.
class PRPKViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows prpk to be viewed
"""
serializer_class = PrpkSerializer
queryset = Prpk.objects.all().order_by('begin').distinct()
# Authentification !
permission_classes = (IsAuthenticated,)
# Only 'get' method
http_method_names = ['get']
#method_decorator(vary_on_cookie)
#method_decorator(cache_page(60*60*1))
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
def get_queryset(self):
""" allow rest api to filter by submissions """
#queryset = Prpk.objects.all().order_by('begin').values_list('begin', flat=True).distinct()
queryset = Prpk.objects.all().order_by('begin').distinct()
highway = self.request.query_params.get('highway', None)
if highway is not None:
queryset = queryset.filter(highway=highway)
return queryset
Look at a part of records :
id begin end status commune_id department_id highway_id massif_id meteozone_id report_id
76 25 25.6 1 NULL 13 2 45 40 1
77 25 25.6 1 NULL 13 2 161 40 1
In this records, I just one only one record in json, but DRF returns 2 records, like this :
[
{
"begin": 25,
"end": 25.6,
"status": "1",
"meteozone": {
"communes": [],
"massifs": [
{
"id": 45,
"name": "abc"
},
{
"id": 161,
"name": "def"
}
],
"id": 40,
"name": "133"
}
},
{
"begin": 25,
"end": 25.6,
"status": "1",
"meteozone": {
"communes": [],
"massifs": [
{
"id": 45,
"name": "abc"
},
{
"id": 161,
"name": "def"
}
],
"id": 40,
"name": "133"
}
},
{
I add a "distinct" method, but it's not working. Can U help me please ? :)
F.

Related

annotate is adding an extra object to my queryset

I'm using DRF. I've got the following code where I annotate a queryset of Company objects with an additional joined field. It seems annotate is adding an extra object to my queryset.
views.py
def get(self, request, **kwargs):
user = request.user
companies = Company.objects.all()
print(companies)
user_in_queue = When(queue__users=user, then = True)
companies = Company.objects.annotate(joined=Case(
user_in_queue, default = False
))
print(companies)
models.py
class Company(models.Model):
name = models.CharField(max_length=15, unique=True)
def __str__(self) -> str:
return self.name
class User(AbstractUser):
def __str__(self) -> str:
return f"{self.username}"
class Queue(models.Model):
company = models.OneToOneField(
Company, on_delete=models.CASCADE, related_name="queue"
)
users = models.ManyToManyField(User, through="QueueDetails", related_name="queues")
class QueueDetails(models.Model):
queue = models.ForeignKey(
Queue,
related_name="queue_details",
on_delete=models.CASCADE,
)
user = models.ForeignKey(
User,
related_name="queue_details",
on_delete=models.CASCADE,
)
the first print gives me
<QuerySet [<Company: kfc>, <Company: kfc>]>
With a json of
[
{
"name": "kfc",
"id": 1,
},
{
"name": "kfc",
"id": 2,
}
]
The second gives me
<QuerySet [<Company: kfc>, <Company: kfc>, <Company: kfc>]>
With a json of
[
{
"name": "kfc",
"id": 1,
"joined": null
},
{
"name": "kfc",
"id": 1,
"joined": true
},
{
"name": "kfc",
"id": 2,
"joined": null
}
]
I want
a json of
[
{
"name": "kfc",
"id": 1,
"joined": true
},
{
"name": "kfc",
"id": 2,
"joined": null
}
]
You want to work with an Exists subquery [Django-doc] to prevent joining:
from django.db.models import Exists, OuterRef
def get(self, request, **kwargs):
user = request.user
companies = Company.objects.annotate(
joined=Exists(
Queue.objects.filter(company_id=OuterRef('pk'), users=request.user)
)
)
# …
For the serializer, you add a joined field:
class CompanySerializer(serializers.ModelSerializer):
joined = serializers.BooleanField()
# …

Getting repeated data white using django orm

im filtering that user whoes order_status is completed and who have listing_id 5001. But im getting output data repeated
Here is my Code:
models.py
class Users(models.Model):
name = models.CharField(max_length=100)
phone = models.CharField(max_length=20, blank=True, null=True)
'''
class Meta:
managed = False
db_table = 'users'
class UserOrder2(models.Model):
order_id = models.AutoField(primary_key=True)
order_status = models.CharField(max_length=30,default='None')
listing_id = models.CharField(max_length=250,default='None')
user_id = models.ForeignKey(Users, on_delete=models.CASCADE, db_column="user_id")
'''
class Meta:
managed = False
db_table = 'user_order'
class UserOrderProduct2(models.Model):
order_id = models.ForeignKey(UserOrder2, on_delete=models.CASCADE, db_column="order_id")
product_name = models.CharField(max_length=100)
...
class Meta:
managed = False
db_table = 'user_order_product'
Views.py
class UserPurchaseQuantityView(generics.GenericAPIView):
def post(self, request):
listing_id= request.data.get('listing_id')
kwargs = {}
kwargs['userorder2__listing_id'] = listing_id
kwargs['userorder2__order_status'] = 'Order Delivered'
queryset = Users.objects.filter(**kwargs)
data = UsersSerializer(queryset, many=True).data
return Response(data)
serializers.py
class UserOrderProductSerializer2(serializers.ModelSerializer):
class Meta:
fields = ["product_id", "product_quantity", "product_price", "sub_total",
"product_name"]
model = UserOrderProduct2
class UserOrderSerializer(serializers.ModelSerializer):
product_detail = UserOrderProductSerializer2(source="userorderproduct2_set", many=True)
class Meta:
fields = ["user_id", "order_date", "product_detail"]
model = UserOrder2
class UsersSerializer(serializers.ModelSerializer):
user_detail = UserOrderSerializer(source="userorder2_set", many=True)
class Meta:
fields = "__all__"
model = Users
I'm getting repeated output like this:
[
{
"id": 51238,
"name": "aaa",
"phone": "123456789",
"email": "aaa#gmail.com",
"user_detail": [
{
"user_id": 51238,
"order_date": "2021-07-27 15:55:56"
"product_detail": [
{
"product_id": 20767,
"product_quantity": 1,
"product_price": 150.0,
"sub_total": 150.0,
"product_name": "EMINAZ 2mg Tablet 10's"
]
},
{
"id": 51238,
"name": "aaa",
"phone": "123456789",
"email": "aaa#gmail.com",
"user_detail": [
{
"user_id": 51238,
"order_date": "2021-07-27 15:55:56"
"product_detail": [
{
"product_id": 20767,
"product_quantity": 1,
"product_price": 150.0,
"sub_total": 150.0,
"product_name": "EMINAZ 2mg Tablet 10's"
] },
{
"id": 51238,
"name": "aaa",
"phone": "123456789",
"email": "aaa#gmail.com",
"user_detail": [
{
"user_id": 51238,
"order_date": "2021-07-27 15:55:56"
"product_detail": [
{
"product_id": 20767,
"product_quantity": 1,
"product_price": 150.0,
"sub_total": 150.0,
"product_name": "EMINAZ 2mg Tablet 10's"
] } ]
I think the issue is in **kwargs. Try this
class UserPurchaseQuantityView(generics.GenericAPIView):
def post(self, request):
listing_id= request.data.get('listing_id')
queryset = Users.objects.filter(userorder2__listing_id=listing_id,
userorder2__order_status='Order Delivered')
data = UsersSerializer(queryset, many=True).data
return Response(data)
Add distinct() to make unique.
queryset = Users.objects.filter(**kwargs).distinct()

How to group an array of objects by one of the model's attribute in Django?

I would like to do the something similar to this example How to group an array of objects by key but I couldn't find a solution for API Get in Django.
Here's an example of what I have:
[
{
"id": 1,
"nutrition_measurement_id": "1",
"nutrition_type_id": "2",
"feeding_time": "dinner"
},
{
"id": 2,
"nutrition_measurement_id": "2",
"nutrition_type_id": "1",
"feeding_time": "dinner"
},
{
"id": 3,
"nutrition_measurement_id": "3",
"nutrition_type_id": "2",
"feeding_time": "breakfast"
},
{
"id": 4,
"nutrition_measurement_id": "2",
"nutrition_type_id": "1",
"feeding_time": "breakfast"
}
]
Here's an example of what i want to achieve:
{
"dinner": [
{
"id": 3,
"nutrition_type_id": 2,
"nutrition_measurement_id": 1
},
{
"id": 3,
"nutrition_type_id": 1,
"nutrition_measurement_id": 2
}
],
"breakfast": [
{
"id": 3,
"nutrition_type_id": 2,
"nutrition_measurement_id": 3
},
{
"id": 5,
"nutrition_type_id": 1,
"nutrition_measurement_id": 4
}
]
}
Here are the models I have:
class HorseNutrition(models.Model):
horse = models.ForeignKey(Horse, models.DO_NOTHING)
nutrition_type = models.ForeignKey('NutritionType', models.DO_NOTHING)
nutrition_measurement = models.ForeignKey('NutritionMeasurement', models.DO_NOTHING)
feeding_time = models.CharField(max_length=10, blank=True, null=True)
class Meta:
managed = False
db_table = 'horse_nutrition'
class NutritionMeasurement(models.Model):
name = models.CharField(unique=True, max_length=30, blank=True, null=True)
class Meta:
managed = False
db_table = 'nutrition_measurement'
def __str__(self):
return self.name
def __unicode__(self):
return self.name
class NutritionType(models.Model):
name = models.CharField(unique=True, max_length=30, blank=True, null=True)
class Meta:
managed = False
db_table = 'nutrition_type'
def __str__(self):
return self.name
def __unicode__(self):
return self.name
Here's my serializer
class HorseNutritionSerializer(serializers.ModelSerializer):
nutrition_measurement_id = serializers.ReadOnlyField(source = 'nutrition_measurement.name')
nutrition_type_id = serializers.ReadOnlyField(source = 'nutrition_type.name')
class Meta:
model = HorseNutrition
fields = ['id', 'nutrition_measurement_id', 'nutrition_type_id', 'feeding_time']
Here's my view:
class HorseNutritionView(APIView):
def get (self, request, format = None):
#id = int(request.GET.get(self.lookup_url_kwarg))
id = 1
if id != None:
queryset = HorseNutrition.objects.filter(horse_id = id)
serializer = HorseNutritionSerializer(queryset, many = True)
return Response(serializer.data)
else:
return Response(status = status.HTTP_400_BAD_REQUEST)
You can use a custom ListSerializer (which we will set list_serializer_class to in the Meta of your serializer) and override its to_representation and use itertools.groupby to group your results:
from itertools import groupby
class HorseNutritionListSerializer(serializers.ListSerializer):
def to_representation(self, data):
ret = super().to_representation(data)
return {key: list(group) for key, group in groupby(ret, key=lambda x: x['feeding_time'])}
#property
def data(self):
ret = serializers.BaseSerializer.data.fget(self)
return serializers.ReturnDict(ret, serializer=self)
class HorseNutritionSerializer(serializers.ModelSerializer):
nutrition_measurement_id = serializers.ReadOnlyField(source = 'nutrition_measurement.name')
nutrition_type_id = serializers.ReadOnlyField(source = 'nutrition_type.name')
class Meta:
model = HorseNutrition
fields = ['id', 'nutrition_measurement_id', 'nutrition_type_id', 'feeding_time']
list_serializer_class = HorseNutritionListSerializer
Also for this grouping to be done properly your queryset must be ordered, which you can do in the view:
class HorseNutritionView(APIView):
def get (self, request, format = None):
#id = int(request.GET.get(self.lookup_url_kwarg))
id = 1
if id != None:
queryset = HorseNutrition.objects.filter(horse_id = id).order_by('feeding_time')
serializer = HorseNutritionSerializer(queryset, many = True)
return Response(serializer.data)
else:
return Response(status = status.HTTP_400_BAD_REQUEST)

Write serializer for a model having multiple foreign key in django rest framework

I am building an api for CRUD operations on a user table which has association with country and state tables as given model definitions:
class Country(models.Model):
""" Model for Country"""
country_abbreviation = models.CharField(max_length=80)
country_name = models.CharField(max_length=80)
is_active = models.SmallIntegerField()
def __str__(self):
return self.country_name
class State(models.Model):
""" model for saving state"""
state_name = models.CharField(max_length=32)
state_abbreviation = models.CharField(max_length=8)
country = models.ForeignKey(Country, related_name='states', on_delete=models.CASCADE)
is_active = models.SmallIntegerField(default=1, blank=True)
def __str__(self):
return self.state_name
class Meta:
""" meta class"""
ordering = ('state_name', )
class User(models.Model):
""" model for saving user information """
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
country = models.ForeignKey(Country, related_name='user_country', on_delete=models.CASCADE)
state = models.ForeignKey(State, related_name='user_state', on_delete=models.CASCADE)
address = models.CharField(max_length=150)
def __str__(self):
return '%d: %s %s' % (self.id, self.first_name, self.last_name)
class Meta:
""" meta class """
ordering = ('first_name', )
I am writing serializers in a way that while I am getting records from user table, for every row in the table there must be available all the country and state info associated with that particular row instead of just their ids respectively:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['id', 'first_name', 'last_name', 'country', 'state', 'address']
Expected response :
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"id": 1,
"first_name": "Satish",
"last_name": "Kumar",
"country": {
"id": 23,
"country_name": "India"
},
"state": {
"id": 22,
"state_name": "Delhi"
},
"address": "New Delhi"
}
],
"page_size": 10,
"model_type": "User"
}
I am getting:
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"id": 1,
"first_name": "Satish",
"last_name": "Kumar",
"country": 23,
"state": 22,
"address": "New Delhi"
}
],
"page_size": 10,
"model_type": "User"
}
In the views.py the codes look like:
class UserList(generics.ListCreateAPIView):
filter_backends = [filters.SearchFilter, filters.OrderingFilter]
search_fields = ['first_name', 'last_name']
ordering_fields = ['id', 'first_name', 'last_name']
ordering = ['first_name']
queryset = User.objects.all()
serializer_class = UserSerializer
permission_classes = [permissions.IsAuthenticated]
def list(self, request, *args, **kwargs):
page_size_req = self.request.query_params.get('page_size', None)
if page_size_req is not None:
records_per_page = page_size_req
pagination.PageNumberPagination.page_size = int(page_size_req)
else:
records_per_page = 10
response = super().list(request, args, kwargs)
# Add additional info required:
response.data['page_size'] = records_per_page
response.data['model_type'] = 'User'
return response
Can someone please help me figure out, how I can achieve the desired results in this case? Thanks for your time in advance.
In that case, add depth to your serializer:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['id', 'first_name', 'last_name', 'country', 'state', 'address']
depth = 1

Django Rest Framework - Group data by dynamic Key

I'm trying to format data when querying my API. I can retrieve my data like that :
"results": [
{
"Cat1": [
{
"job": String,
"position": Integer
}
]
},
{
"Cat1": [
{
"job": String,
"position": Integer
}
]
},
{
"Cat2": [
{
"job": String,
"position": Integer
}
]
}
]
But I want something like that:
"results": [
{
"Cat1": [
{
"job": String,
"position": Integer
},
{
"job": String,
"position": Integer
}
]
},
{
"Cat2": [
{
"job": String,
"position": Integer
}
]
}
]
I use a serializer like this:
class CustomSerializer(serializers.ModelSerializer):
category = CatSerializer()
job = JobSerializer()
class Meta:
model = MyModel
fields = '__all__'
def to_representation(self, value):
return {
value.category.name: [{"job": value.job.name,
"position": value.position, }]
cat1 and cat2 are dynamics, they are from another table. I don't understand how to create my arrays properly using those serializers. The category is a #Property field in my model who's a foreign key of job.
My models:
class MyModel(models.Model):
CHOICES = [(i, i) for i in range(4)]
partner = models.ForeignKey(Partner, on_delete=models.CASCADE)
job = models.ForeignKey(
Job, on_delete=models.CASCADE)
position = models.IntegerField(choices=CHOICES)
#property
def category(self):
return self.job.category.domain
def __str__(self):
return '%s | %s | %s | position: %s' % (self.partner.name, self.domain.name, self.job.name, self.position)
class Job(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
code = models.CharField(
max_length=255, unique=True)
name = models.CharField(
max_length=255)
class Category(models.Model):
domain = models.ForeignKey(Domain, on_delete=models.CASCADE)
code = models.CharField(
max_length=5)
name = models.CharField(max_length=255)
hourly_rate = models.FloatField(
null=True, blank=True)
How should I deal with serializers to format my data properly ?
EDIT:
I ended with something like that, except for the ListSerializer.
I used 2 ModelSerilizers
class MyModelCustomSerializer(serializers.ModelSerializer):
position = serializers.IntegerField(read_only=True)
job = serializers.CharField(source='job.name', read_only=True)
class Meta:
model = MyModel
fields = ['job', 'position']
def to_representation(self, value):
return {"position": value.position,
"job": {"name": value.job.name, "slug": value.job.slug,
"title": value.job.seo_title}
}
And
class CategoryCustomSerializer(serializers.ModelSerializer):
models = MyModelustomerSerializer(many=True)
class Meta:
model = Category
fields = ['category', 'MyModel']
def to_representation(self, value):
filters = {'job__category__domain__name': value.name}
myModels = MyModel.objects.filter(**filters)
serializer = MyModelCustomSerializer(instance=myModels, many=True,)
return {value.name: serializer.data}
But if I try to use a jobSerializer who already exist instead of
"job": {"name": value.job.name, "slug": value.job.slug,
"title": value.job.seo_title}
},
I got this error: Object of type 'Job' is not JSON serializable, but it's working anyway because i don't need all fields
I would go the direction of implementing a custom ListSerializer for the ModelSerializer and overriding its to_representation method.
from rest_framework import serializers
from collections import OrderedDict
class CustomListSerializer(serializers.ListSerializer):
def to_representation(self, data):
iterable = data.all() if isinstance(data, models.Manager) else data
list_rep = OrderedDict()
for item in iterable:
child_rep = self.child.to_representation(item)
k, v = list(child_rep.items()).pop()
list_rep.setdefault(k, []).append(v)
return [
{k: v}
for k, v in list_rep.items()
]
Then set the model Meta to use it
class CustomSerializer(serializers.ModelSerializer):
category = CatSerializer()
job = JobSerializer()
class Meta:
model = MyModel
fields = '__all__'
list_serializer_class = CustomListSerializer
def to_representation(self, value):
return {
value.category.name: [{"job": value.job.name,
"position": value.position, }]