DRF using prefetch_related - django

I have two classes Vessels and Components, each vessel has several components.
I just want to fetch all vessels and all their related components in one query, I thought prefretch_related does that trick but in DRF in the api i am only receiving the vessels without their related components
models.py
class Vessel(models.Model):
name = models.CharField(max_length=255, null=True, blank=True)
imo = models.CharField(max_length=255, null=True, blank=True)
def __str__(self):
return self.name
class Component(models.Model):
vessel = models.ForeignKey(
Vessel, blank=True, null=True, on_delete=models.CASCADE, related_name='vessel_components')
name = models.CharField(max_length=200, blank=True, null=True)
manufacturer = models.CharField(max_length=200, blank=True, null=True)
model = models.CharField(max_length=200, blank=True, null=True)
type = models.CharField(max_length=200, blank=True, null=True)
remarks = models.TextField(blank=True, null=True)
def __str__(self):
return self.name
serializers.py
class VesselSerializer(serializers.ModelSerializer):
class Meta:
model = Vessel
fields = '__all__'
class ComponentSerializer(serializers.ModelSerializer):
class Meta:
model = Component
fields = '__all__'
the view :
#api_view(['GET'])
def getVessels(request):
vessels = Vessel.objects.all().prefetch_related('vessel_components')
vSerializer = VesselSerializer(vessels, many=True)
return Response(vSerializer.data)
the result i am getting :

I thought prefretch_related does that trick but in DRF.
This will fetch the Components for the Vessels, but since your serializers do not serialize these components, these will not end up in the result.
You should define the ComponentSerializer as subserializer for VesselSerializer, so:
class ComponentSerializer(serializers.ModelSerializer):
class Meta:
model = Component
fields = '__all__'
class VesselSerializer(serializers.ModelSerializer):
vessel_components = ComponentSerializer(many=True) # 🖘 subserializer
class Meta:
model = Vessel
fields = '__all__'

Related

Optimizing DRF performance

I am trying to optimize a DRF view that has very low performance, is there a way to make it faster? This is where I am right now
Models:
class CategoryVideos(models.Model):
class Meta:
verbose_name = 'Category'
verbose_name_plural = 'Categories'
name = models.CharField(max_length=100, null=False, blank=False)
def __str__(self):
return f"{self.name} and {self.id}"
class Videos(models.Model):
category = models.ManyToManyField(CategoryVideos, null=True, blank=True)
illustration = models.FileField(null=True, blank=True)
video = models.FileField(null=True, blank=True)
title = models.CharField(max_length=255, null=True, blank=True)
description = models.CharField(max_length=255, null=True, blank=True)
url_video = models.CharField(max_length=255, null=True, blank=True)
url_image = models.CharField(max_length=255, null=True, blank=True)
Serializers:
class CategorySerializer2(serializers.ModelSerializer):
class Meta:
model = CategoryVideos
fields = ["name"]
class VideoSerializer(serializers.ModelSerializer):
category = CategorySerializer2(many=True, read_only=True)
class Meta:
model = Videos
fields = ["id", "description", "title", "url_video", "url_image", "category"]
read_only=True
View:
class OfferListAPIView(generics.ListAPIView):
queryset = Videos.objects.prefetch_related('category').all()
serializer_class=VideoSerializer
For the moment, with 400 videos and just 6 categories, it takes approx 2.8 secs to get an answer which is way too high.
Many thanks
As you are using only for listing you can add
db_index=True attribute for any field you can also use db_index=True to create an index on a ForeignKey and ManyToManyFields for performance.
make note that creating indexes can slow down the performance of some database operations, such as inserting or updating large amounts of data.

How to inner join multi tables or merge multi serializers in Django?

i have this code here for the models:
class Users(models.Model):
first_name = models.CharField(max_length=32, blank=True, null=True)
last_name = models.CharField(max_length=32, blank=True, null=True)
email = models.EmailField(max_length=254, unique=True)
class Images(models.Model):
user= models.ForeignKey(Users, on_delete=models.RESTRICT)
encoded_pic = models.JSONField(
encoder=None, default=dict, blank=True, null=True)
pic_thumbnail_path = models.CharField(max_length=222, blank=True, null=True)
class WorkingDays(models.Model):
user= models.ForeignKey(Users, on_delete=models.RESTRICT)
day = models.DateField(blank=True, null=True)
enter_time = models.DateTimeField(blank=True, null=True)
exit_time = models.CharField(max_length=32, blank=True, null=True)
class Departments(models.Model):
user= models.ForeignKey(Users, on_delete=models.RESTRICT)
department_name = models.CharField(max_length=32, blank=True, null=True)
These are the serializers:
class UserssSerializer(serializers.ModelSerializer):
class Meta:
model = Users
fields = '__all__'
class ImagesSerializer(serializers.ModelSerializer):
class Meta:
model = Images
fields = '__all__'
class WorkingDaysSerializer(serializers.ModelSerializer):
class Meta:
model = WorkingDays
fields = '__all__'
class DepartmentsSerializer(serializers.ModelSerializer):
class Meta:
model = WorkingDays
fields = '__all__'
I tried this code but it only returns the images fields not the others
data=Images.objects.filter(user_id__workingdays=pk).values()
What i want to do is to inner join images, workingdays and departments table using user_id field
also is there away to merge serializers of all these 3 tables to make the serializer returns all the fields from all the 3 tables?

How to display all child category from parent category in django-rest-framework

I'm trying to show my all children category from parent category. I want to just hit one API end and show all tables which is related to that item. I want to hit "Master-Category" and show all releated "Category","Sub-Category" and "Root-Item" in Hierarchy form. I display all the data but cannot in Hierarchy form. Can anyone please give me the solution for this problem.
Model.py
from django.db import models
from django.contrib.auth.models import User
class MasterCategory(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True,
verbose_name="Created By")
title = models.CharField(max_length=100, null=False, blank=False)
description = models.TextField(default='')
def __str__(self):
return str(self.title)
#property
def category(self):
data = NewCategory.objects.filter(master_category__id=self.id).values
return data
#property
def sub_category(self):
data = NewSubcategory.objects.filter(category__id=self.id).values
return data
#property
def root_item(self):
data = Rootitem.objects.filter(sub_category__id=self.id).values
return data
class NewCategory(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True,
verbose_name="Created By")
title = models.CharField(max_length=100, null=False, blank=False)
description = models.TextField(default="")
master_category = models.ForeignKey(
MasterCategory, on_delete=models.CASCADE, null=True, blank=True)
def __str__(self):
return str(self.title)
class NewSubcategory(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True,
verbose_name="Created By")
title = models.CharField(max_length=100, null=False, blank=False)
description = models.TextField(default="")
category = models.ForeignKey(NewCategory, on_delete=models.CASCADE, null=True,
blank=True)
def __str__(self):
return str(self.title)
class Rootitem(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True,
verbose_name="Created By")
title = models.CharField(max_length=100, null=False, blank=False)
description = models.TextField(default="")
sub_category = models.ForeignKey(NewSubcategory, on_delete=models.CASCADE,
null=True, blank=True)
def __str__(self):
return str(self.title)
Serializers.py
I add #property function name in MasterCategorySerializer fields, "category", "sub_category", "root_item"
from .models import MasterCategory, NewCategory, NewSubcategory, Rootitem
from rest_framework import serializers
class MasterCategorySerializer(serializers.ModelSerializer):
class Meta:
model = MasterCategory
fields = ["title", 'category', 'sub_category', 'root_item']
class NewCategorySerializer(serializers.ModelSerializer):
class Meta:
model = NewCategory
fields = "__all__"
class NewSubcategorySerializer(serializers.ModelSerializer):
new_cat = NewCategorySerializer(source='category',read_only=True, many=True)
class Meta:
model = NewSubcategory
fields = "__all__"
class RootitemSerializer(serializers.ModelSerializer):
class Meta:
model = Rootitem
fields = "__all__"
**Viewset.py**
from API_app.models import MasterCategory
from API_app.serializers import MasterCategorySerializer
from rest_framework import viewsets
class MasterCategoryViewSet(viewsets.ModelViewSet):
queryset = MasterCategory.objects.all()
serializer_class = MasterCategorySerializer
My Desired Output, what i want.
{
Electronics <---- Master-Category
{
Smart-Phone <---- Category
{
Samsung <---- Sub-Category
{
Samsung S20 Ultra <---- Root-Item
}
}
}
}
Change your serializers as below. For this nested structure you don't need properties. As tables are connected with foreign key you can define related name between models and assign to its serializer. Default related name between table is tablename_set.
class RootitemSerializer(serializers.ModelSerializer):
class Meta:
model = Rootitem
fields = "__all__"
class NewSubcategorySerializer(serializers.ModelSerializer):
rootitem_set = RootitemSerializer(many=True)
class Meta:
model = NewSubcategory
fields = "__all__"
class NewCategorySerializer(serializers.ModelSerializer):
newsubcategory_set = NewSubcategorySerializer(many=True)
class Meta:
model = NewCategory
fields = "__all__"
class MasterCategorySerializer(serializers.ModelSerializer):
newcategory_set = NewCategorySerializer(many=True)
class Meta:
model = MasterCategory
fields = "__all__"
We have relationship between MasterCategory and NewCategory. As you don't define related_name therefore related name is newcategory_set and its response is NewCategorySeralizer. Make many=True because they are related with foreign key as there can be multiple newcategory related to mastercategory. Other relations are same as above explanation.
If you want to change this default related name then look at related_name, you can define it inside models.ForeignKey()

drf create manytomany fields

models
class CreatorRawArtwork(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=500)
descripton = models.TextField()
editions = models.IntegerField(null=True, blank=True)
price = models.CharField(max_length=500)
created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True)
medias = models.FileField(null=True, blank=True, upload_to="raw-medias")
user = models.ForeignKey(to=Login, on_delete=models.CASCADE, related_name="creatorrawartwork", null=True, blank=True
)
collection = models.ForeignKey(
to=DesignerCollection, on_delete=models.CASCADE, related_name="creatorrawartwork", null=True, blank=True)
categories = models.ManyToManyField(DesignerCategories, related_name='creatorrawartwork')
def __str__(self):
return self.title
serializer
class CreatorRawArtworkSerializer(serializers.ModelSerializer):
categories = serializers.PrimaryKeyRelatedField(queryset=DesignerCategories.objects.all(), many=True)
class Meta:
model = CreatorRawArtwork
fields = "__all__"
depth=1
views
class CreatorRawArtworkView(viewsets.ModelViewSet):
queryset = CreatorRawArtwork.objects.all()
serializer_class = CreatorRawArtworkSerializer
Here i am trying to create manytomany fields using drf serialier it is showing some error
plese check the screenshot for parameter and responses
What can be the issue please take a look
class CreatorRawArtworkSerializer(serializers.ModelSerializer):
collection = DesignerCollectionSerializer(read_only=True) #assuming you have already defined serializer for *DesignerCollectionSerializer*
categories = DesignerCategoriesSerializer(many=True)
class Meta:
model = CreatorRawArtwork
fields = "__all__"
depth=1
I tested with your code and your code is working fine
just make sure your request data is json

Tricky issue passing data from form to form in django

I am a django newbie and have one more big struggle for longer time... :/
User can choose a 'main language' which is set as ForeignKey. User can choose 'further languages' as ManyToMany (Checkbox). Assuming, user selects english as 'main' language, so english has to be filterd out from the 'further languages'... have been searching so much and have no idea how to do it. Is this even possible without JavaScript?
Of course, I could set the 'queryset' in the second form but it would filter the objects after the submit... The similar problem is, when a selected country has to be connected to the proper zipcodes...
I am very thankful for any hints.
Best regards.
class Country(models.Model):
enter code here
country = models.CharField(max_length=40)
active = models.BooleanField(default=True)
class Meta:
verbose_name_plural = 'Länder'
def __str__(self):
return self.country
class ZipCode(models.Model):
zipcode = models.CharField(max_length=5)
city = models.CharField(max_length=255)
active = models.BooleanField(default=False)
class Meta:
verbose_name_plural = 'Postleitzahlen'
def __str__(self):
return '{0} {1}'.format(self.zipcode, self.city)
class MainLanguage(models.Model):
language = models.CharField(verbose_name='Hauptsprache', max_length=40)
active = models.BooleanField(default=True)
class Meta:
verbose_name_plural = 'Hauptsprachen'
ordering = ['language']
def __str__(self):
return self.language
class SecondLanguage(models.Model):
language = models.CharField(verbose_name='weitere Sprachen', max_length=40)
active = models.BooleanField(default=False)
class Meta:
verbose_name_plural = 'weitere Sprachen'
ordering = ['language']
def __str__(self):
return self.language
class CustomUserprofile(models.Model):
user = models.OneToOneField(User)
name = models.CharField(verbose_name='Vorname', max_length=40,
null=True, blank=True)
country = models.ForeignKey(Country, verbose_name='Land',
null=True, blank=True)
zipcode = models.ForeignKey(ZipCode, blank=True, null=True)
main_language = models.ForeignKey(
MainLanguage, verbose_name='Hauptsprache',
null=True, blank=True)
second_language = models.ManyToManyField(
SecondLanguage, verbose_name='weitere Sprachen',
null=True, blank=True)
class UserProfileForm(forms.ModelForm):
second_language = forms.ModelMultipleChoiceField(
queryset=SecondLanguage.objects.all(),
required=False,
widget=forms.CheckboxSelectMultiple)
class Meta:
model = CustomUserprofile
exclude = ('user',)