DRF, update a single field in nested serializer - django

these are my models:
class ADS(models.Model):
advertiser = models.ForeignKey(User)
campaign = models.ForeignKey(Campaign, related_name="ads")
headline = models.CharField(max_length=50)
description_1 = models.TextField(blank=True)
description_2 = models.TextField(blank=True)
display_url = models.URLField(blank=True)
final_url = models.URLField(blank=True)
mobile_url = models.URLField(blank=True)
class Meta:
db_table = "ads"
and:
class AdsImages(models.Model):
ads = models.ForeignKey(ADS,blank=True, null=True)
image = models.ImageField(upload_to='images/',default='media/None/no-img.jpg')
class Meta:
db_table = "ads_images"
I have a nested serializer, as you can see the second model is a table with images, so I am uploading asynchronously the images via AngularJs. As you can see the ForeignKey(ADS, blank=True, null=True), so I am not filling it for the first time. My question is that how to update after that the ads field.
here are my serializers:
class AdsImagesSerializer(serializers.ModelSerializer):
image = serializers.ImageField(use_url=True,allow_empty_file=True)
class Meta:
model = AdsImages
fields = ('id','image','ads',)
class ADSerializer(serializers.ModelSerializer):
adsImages = AdsImagesSerializer(many=True)
class Meta:
model = ADS
fields = ('headline','description_1','description_2','display_url','final_url','mobile_url','advertiser','adsImages',)

Partial updates
By default, serializers must be passed values for all required fields or they will raise validation errors. You can use the partial argument in order to allow partial updates
# Update `comment` with partial data
serializer = CommentSerializer(comment, data={'content': u'foo bar'}, partial=True)

Related

Django: how to include missing pk field into serializer when updating nested object?

I have a serializer in my Django app that is meant for updating a nested object. Updating works, but I'm facing another problem: I can't delete objects that are not in validated_data['events] because I don't have the id to be compared with my instance id's.
For reference, these are my Models:
class Plan(models.Model):
planId = models.CharField(primary_key=True, max_length=100, unique=True)
name = models.CharField(max_length=200)
class PlanEvent(models.Model):
plan = models.ForeignKey(Plan, on_delete=models.CASCADE)
id = models.CharField(primary_key=True, max_length=100, unique=True, blank=False, null=False)
done = models.BooleanField()
title = models.CharField(max_length=100, blank=True)
This is my PlanEventUpdateSerializer:
class PlanEventUpdateSerializer(serializers.ModelSerializer):
class Meta:
model = PlanEvent
fields = ('done', 'title')
Is there some way to include the id, so I could compare the id's like this in my update method:
class PlanUpdateSerializer(serializers.ModelSerializer):
events = PlanEventUpdateSerializer(many=True)
class Meta:
model = Plan
fields = ('name',)
....
def update(self, instance, validated_data):
events_validated_data = validated_data.pop('events')
events = (instance.events.all())
events = list(events)
event_ids = [item['id'] for item in events_validated_data]
for event in events:
if event.id not in event_ids:
event.delete()
I found a solution. I defined the id as a optional field in the serializer and then I was able to include it in the fields. Sending POST and PUT requests works now and I'm also able to delete objects when updating:
class PlanEventUpdateSerializer(serializers.ModelSerializer):
id = serializers.CharField(source='pk', required=False)
class Meta:
model = PlanEvent
fields = ('id', 'done', 'title')

How to reduces number of sqls generating Django restful services

I need help to improve API performance. Below is my approach also i have tried to do other combination of fields with select_related or prefetch_related but still i am getting like 400sqls.
model.py
class VisVisits(models.Model):
visit_id = models.IntegerField(primary_key=True)
null=True)
class Meta:
managed = False
db_table = 'vis_visits'
def __str__(self):
return str(self.visit_id)
class VisVisitData(models.Model):
vdata_id = models.IntegerField(primary_key=True)
app_local_id = models.IntegerField(blank=True, null=True)
visit = models.ForeignKey('VisVisits', models.DO_NOTHING, blank=True, null=True, related_name='data')
class Meta:
managed = False
db_table = 'vis_visit_data'
def __str__(self):
return str(self.vdata_id)
Serializer
class VisVisitDataSerializer(serializers.ModelSerializer):
class Meta:
model = VisVisitData
field = '__all__'
class VisVisitsSerializer(serializers.ModelSerializer):
data = VisVisitDataSerializer(many=True)
class Meta:
model = VisVisits
fields = ('visit_id','data')
views.py
visit_data = VisVisits.objects.filter(is_valid=1,user_id=u).prefetch_related('school_program__school')
visit_data_serializer = VisVisitsSerializer(visit_data,context={'request':request},many=True)
To reduce the number of queries you must prefetch the data that you serialize in the VisVisitsSerializer, i.e. data. In this serializer you don't include school_program so it's not necessary prefetch this. You can use the django debug toolbar (https://django-debug-toolbar.readthedocs.io/en/latest/) to inspect the repeated queries and find out what to put in select_releted/prefetch_related

related objects queries django rest framework

I have the following models
class STUser(AbstractBaseUser):
email = models.EmailField(unique=True)
name = models.CharField(max_length=255)
companyname = models.CharField(max_length=200, blank=True, null=True)
...
class VenuePermissions(models.Model):
user = models.ForeignKey(STUser, on_delete=models.CASCADE)
venue = models.ForeignKey(Venue, on_delete=models.CASCADE)
signupvaildatestring = models.CharField(max_length=200, blank=True, null=True)
...
I want to grab all the STUser objects and grab all their permissions.
So what I would like is to grab all the VenuePermissions objects. And grab the user and venue object of each venuePermission
Two ways I can do this. use the VenuePermissions_set attribute on STUser but then how do I grab the venue when its just going to be a pk value?
Or focus on the VenuePermissions objects and grab the user and venue from the pk values but how?
I remember nested queries, and I kinda did one in my browse code.
here is an example:
rooms = Room.objects.filter(venue=OuterRef('pk'), sixtyroundseatingoption= True)
venuelist = venuelist.annotate(sixtyrounds=Exists(rooms))
venuelist = venuelist.filter(Q(sixtyrounds = True) | Q(fullbuyoutsixtyroundseatingoption = True))
I've done the set objects in a serializer before
Example serializer:
class RoomAndImageSerializer(serializers.ModelSerializer):
roomimage_set = RoomImageSerializer(many=True, read_only=True)
class Meta:
model = Room
fields = ('pk','name')
any help with this query would be appreciated!
So this is what I am currently trying, I will post an answer if this works:
class VenueUserList(ListAPIView):
serializer_class = VenueUserListSerializer
queryset = VenuePermissions.objects.select_related('user').select_related('venue').filter(signupvaildatestring=None)
class VenueUserListSerializer(serializers.ModelSerializer):
user = UserSerializer()
venue = VenueSerializer()
class Meta:
model = VenuePermissions
fields = ('user', 'venue', 'isvenueviewer', 'isvenueeventplanner', 'isvenueadministrator')
Here is the answer. However I still need to group venues by user. Working on that.
class VenueUserList(ListAPIView):
serializer_class = VenueUserListSerializer
queryset = VenuePermissions.objects.select_related('user').select_related('venue').filter(signupvaildatestring=None)
class VenueUserListSerializer(serializers.ModelSerializer):
user = UserSerializer()
venue = VenueSerializer()
class Meta:
model = VenuePermissions
fields = ('user', 'venue', 'isvenueviewer', 'isvenueeventplanner', 'isvenueadministrator')

how to create serializer for an enum field in django rest framework

i am writing an API in python django rest framework and i am stuck at creating a serializer field for an ENUM, how can i create a serializer field for an ENUM field.
my model code is:
class Queue(models.Model):
class Meta:
db_table = 'queues'
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True)
name = models.CharField(max_length=45)
type = EnumChoiceField(QueueTypes, default=QueueTypes.pending)
date = models.DateTimeField(auto_now=True)
and i am writing a serializer for this model.
class QueueSerializer(serializers.ModelSerializer):
class Meta:
model = Queue
fields = ('__all__')
id = serializers.CharField(read_only=True)
name = serializers.CharField(max_length=45, required=True)
type=?????# what am i supposed to do here?
date = serializers.DateTimeField(read_only=True)
After doing a lot of searches on google i finally found the answer to the serializing issue with the EnumchoiceField the following changes did the job.
my Model.py:
from enumchoicefield import ChoiceEnum, EnumChoiceField
class QueueTypes(ChoiceEnum):
appointment = "appointment"
wait = "wait"
process = "process"
pending = "pending"
class Queue(models.Model):
class Meta:
db_table = 'queues'
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True)
name = models.CharField(max_length=45)
type = EnumChoiceField(enum_class=QueueTypes , default=QueueTypes.process)
date = models.DateTimeField(auto_now=True)
fk_department = models.ForeignKey(Department, related_name='department',null=True, on_delete=models.CASCADE)
my Serialize.py:
from enumchoicefield import ChoiceEnum, EnumChoiceField
class QueueSerializer(serializers.ModelSerializer):
class Meta:
model = Queue
fields = ('__all__')
id = serializers.UUIDField(read_only=True)
name = serializers.CharField(max_length=45, required=True)
type = EnumChoiceField(enum_class=QueueTypes)
date = serializers.DateTimeField(read_only=True)
The EnumChoiceField extension seems to work fine but does not correctly support the HTML support render of the REST Framework, fields serialized as EnumChoiceField are not rendered.

How to use a model field for multiple models in Django Rest Framework

I have a model BstUserActionLog with a foreign key to Django model User. I have another model for user profile information, BstUserProfile. When I do serialize BstUserActionLog with ModelSerializer I do have Django User info serialized as it is supposed to be. But I also need to add BstUserProfile serialized using the same user_id used for User model.
How can I serialize BstUserActionLog with model User and BstUserProfile are both serialized?
From my models.py:
class BstUserActionLog(models.Model):
id = models.AutoField(primary_key=True)
user = models.ForeignKey(User)
bst_action_type = models.ForeignKey(BstActionType)
action_date = models.DateTimeField(auto_now_add=True)
bst_book = models.ForeignKey(BstBook)
new_value_id = models.IntegerField(blank=True, null=True)
old_value_id = models.IntegerField(blank=True, null=True)
class Meta:
managed = False
db_table = 'bst_user_action_log'
class BstUserProfile(models.Model):
id = models.AutoField(primary_key=True)
user = models.ForeignKey(User, unique=True)
website = models.CharField(max_length=200)
picture = models.CharField(max_length=100)
is_avatar_uploaded = models.BooleanField(default=False)
is_cover_uploaded = models.BooleanField(default=False)
class Meta:
managed = False
db_table = 'bst_user_profile'
app_label = 'bst'
From my serializers.py:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id','username',)
class BstUserActionLogSerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta:
model = BstUserActionLog
fields = ('id', 'user', 'bst_action_type', 'action_date', 'bst_book', 'new_value_id', 'old_value_id')
depth = 3
The key to my solution is SerializerMethodField. With this a new field can be added which is calculated with a method. This method signature contains the object to be serialized. After that a regular method serializer is used to return the serialized object.
From my serializers.py
class BstUserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = BstUserProfile
fields = ('is_avatar_uploaded', 'is_cover_uploaded')
class BstUserActionLogSerializer(serializers.ModelSerializer):
user = UserSerializer()
user_profile = serializers.SerializerMethodField()
def get_user_profile(self, obj):
try:
user_profile = BstUserProfile.objects.get(user_id=obj.user_id)
return BstUserProfileSerializer(user_profile).data
except Exception as e:
return {}
class Meta:
model = BstUserActionLog
fields = ('id', 'user', 'user_profile', 'bst_action_type', 'action_date', 'bst_book', 'new_value_id', 'old_value_id')
depth = 3