an api for adding label to device - python

I am trying to add gmail feature of moving the mail to custom created label or already made label. It is a move to label feature. When you select the mail and then click on move to label actions, you will see lots of pre-defined label name such as important, sms, personal, work etc and also a create a new label in the dropdown. I want to have get, post and delete. Am i on the right track?
Here is my model
class Device(BaseDevice):
"""
This stores Device
"""
# Description is optional (can be blank, can be null)
description = models.TextField(blank=True, null=True)
created_on = models.DateTimeField(auto_now_add=True)
updated_on= models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class Label(models.Model):
"""
This creates a label
"""
# Name is required
name = models.CharField(max_length=250, blank=False, null=False)
device = models.ForeignKey(Device, related_name='label')
Serializers
class IOSerializer(serializers.ModelSerializer):
id = serializers.UUIDField(source='token', format='hex')
class Meta:
model = IO
fields = ('id', 'name', 'description', 'type')
class LabelSerializer(serializers.ModelSerializer):
id = serializers.UUIDField(source='token', format='hex')
class Meta:
model = Label
fields = ('id', 'name')
class DeviceSerializer(serializers.ModelSerializer):
id = serializers.UUIDField(source='token', format='hex', read_only=True)
label = LabelSerializer(read_only=False, many=True, required=False)
io = IOSerializer(read_only=False, many=True, required=False)
class Meta:
model = Device
fields = ('id', 'name', 'description', 'io', 'label')
def save_io(self, device, io):
for io in io:
token = io.pop('token', None)
if token is None:
IO.objects.create(device=device, **io)
else:
IO.objects.filter(token=token, device=device).update(**io)
def update(self, instance, validated_data):
io = validated_data.pop('io',[])
instance = super(DeviceSerializer, self).update(instance, validated_data)
if len(io):
self.save_io(instance, io)
return instance
def create(self, validated_data):
io = validated_data.pop('io',[])
instance = super(DeviceSerializer, self).create(validated_data)
if len(io):
self.save_io(instance, io)
return instance

Related

Displaying Foreign Key Options in django restframework view

I'm having trouble displaying all the jobs as options in the apply url view see image below.
I am getting the error which says
Lists are not currently supported in HTML input
The main function I am looking for is for a list of jobs that were posted to be available for selection when applying for the job.
models.py
class Job(models.Model):
"""A Job used to create a job posting"""
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)
description = models.TextField()
job_type = models.CharField(max_length=12, choices=JOB_TYPE_CHOICES, default='Full-Time')
city = models.CharField(max_length=255)
def __str__(self):
return self.description[:50]
class Applicant(models.Model):
"""A applicant that can apply to a job"""
job = models.ForeignKey(Job, related_name='applicants', on_delete=models.CASCADE)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
email = models.EmailField(max_length=254)
phone_number = PhoneNumberField()
resume = models.FileField(upload_to=resume_file_path, validators=[validate_file_extension])
def __str__(self):
return self.first_name
I've removed some of the attributes in Job so that the code is not so long.
serializers.py
from rest_framework import serializers
from django.utils.translation import ugettext_lazy as _
from core.models import Job, Applicant
class JobSerializer(serializers.ModelSerializer):
"""Serializer for tag objects"""
applicants = serializers.StringRelatedField(many=True)
class Meta:
model = Job
fields = ('id', 'description', 'job_type', 'city', 'state', 'salary', 'position', 'employer', 'created_date', 'is_active', 'applicants')
read_only_fields = ('id',)
def create(self, validated_data):
"""Create a job posting with user and return it"""
return Job.objects.create(**validated_data)
class ApplyJobSerializer(serializers.ModelSerializer):
"""Serializer for applying to jobs"""
jobs = JobSerializer(many=True, queryset=Job.objects.all())
class Meta:
model = Applicant
fields = ('id','jobs', 'first_name', 'last_name', 'email', 'phone_number', 'resume')
read_only_fields = ('id',)
views.py
class ApplyJobView(generics.CreateAPIView):
"""Allows applicants to apply for jobs"""
serializer_class = serializers.ApplyJobSerializer
I've tried adding a queryset=Job.objects.all() as an argument to the JobSerializer() in the ApplyJobSerializer class in my serializers.py field. However I get an error that says
TypeError: __init__() got an unexpected keyword argument 'queryset'
You can select an existing job in the form.
class JobSerializer(serializers.PrimaryKeyRelatedField, serializers.ModelSerializer):
"""Serializer for tag objects"""
applicants = serializers.StringRelatedField(many=True)
class Meta:
model = Job
fields = ('__all__')
read_only_fields = ('id',)
def create(self, validated_data):
"""Create a job posting with user and return it"""
return Job.objects.create(**validated_data)
class ApplyJobSerializer(serializers.ModelSerializer):
"""Serializer for applying to jobs"""
jobs = JobSerializer(many=True, queryset=Job.objects.all())
class Meta:
model = Applicant
fields = ('__all__')
read_only_fields = ('id',)
This result as I have created a new job with description is desc

How do I do a post request with related serializers Django rest framework

I have model Package with Supplier and PackageSize, foreign keys and a field to which is also a foreign key of a Shipping model which contains where the supplier wants to ship the package so to make sure I a user can submit the whole information in one request, I created my serializers and linked them like such.
serializers.py
from users.models import Supplier
from packages.models import Package , PackageSize , ShippingLocation , Shipping
class ShippingLocationSerializer(serializers.ModelSerializer):
class Meta:
model= ShippingLocation
fields = ['latitude','longitude']
def create(self, validated_data):
return ShippingLocation(**validated_data)
class PackageSizeSerializer(serializers.ModelSerializer):
class Meta:
model= PackageSize
fields = ['length', 'width' ,'height' ,'weight']
def create(self, validated_data):
"""
docstring
"""
pass
class ShippingSerializer(serializers.ModelSerializer):
location = ShippingLocationSerializer(source='location_set')
class Meta:
model = Shipping
fields = [
'first_name',
'last_name',
'phone_number',
'email',
'street_address',
'village',
'district',
'country',
'location'
]
def create(self, validated_data):
"""
docstring
"""
pass
class SupplierPackageSerializer(serializers.ModelSerializer):
size = PackageSizeSerializer(source='size_set')
shipping_location= ShippingSerializer(source='to_set')
class Meta:
model = Package
fields = ['supplier', 'name', 'size', 'shipping_location', ]
read_only_fields = ['supplier']
def create(self, validated_data):
user =Supplier.objects.get(username=self.context['request'].user)
return Package(supplier=user, **validated_data )
and created my views like such
view.py
from rest_framework import generics
from packages.models import Package
from .serializers import , SupplierPackageSerializer
from rest_framework.permissions import IsAuthenticated
class SupplierPackageViewSet(generics.ListCreateAPIView):
serializer_class = SupplierPackageSerializer
queryset = Package.objects.all().order_by('-name')
permission_classes = [IsAuthenticated]
models.py
from django.db import models
from django.conf import settings
from users.models import Supplier
class ShippingLocation(models.Model):
latitude = models.IntegerField()
longitude = models.IntegerField()
class Shipping (models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
phone_number = models.CharField(max_length=14)
email = models.EmailField()
street_address = models.CharField(max_length=30)
village = models.CharField(max_length=30)
district = models.CharField(max_length=30)
country = models.CharField(max_length=30)
location = models.OneToOneField(ShippingLocation , default=None, on_delete=models.CASCADE )
# Returns the string representation of the model.
def __str__(self):
return self.email
class PackageSize(models.Model):
length = models.IntegerField()
width = models.IntegerField()
height = models.IntegerField()
weight = models.IntegerField()
# Create your models here.
class Package(models.Model):
TYPE = (
('1', 'Envelope'),
('2', 'Parcel'),
('2', 'Soft'),
('2', 'Freezed'),
)
TYPE = (
('1', 'CREATED IN SYSTEM'),
('2', ''),
('2', 'Soft'),
('2', 'Freezed'),
)
name = models.CharField(max_length=30)
supplier = models.ForeignKey( Supplier , on_delete=models.DO_NOTHING)
to = models.ForeignKey(Shipping, default=None, on_delete=models.CASCADE )
size = models.ForeignKey( PackageSize, default=None, on_delete=models.CASCADE )
type = models.CharField(max_length=1, default="1", choices=TYPE)
def __str__(self):
return self.name
the challenge is when I submit the data it is validated very well but it cannot be saved and I get this error
TypeError at /supplier/package
Package() got an unexpected keyword argument 'size_set'
Model Package has foreign key to PackageSize ( only one size per package) so source is not size_set but just size
size = PackageSizeSerializer()
EDIT:
You will also have to override create method on serializer to save related object as documented in writable nested serializer
Something in a line of
def create(self, validated_data):
size_data= validated_data.pop('size', None)
if size_data:
package_size= PackageSize.objects.get_or_create(**size_data)[0]
validated_data['size'] = package_size
return Package.objects.create(**validated_data)
EDIT!
serializers.py
class StringSerializer(serializers.StringRelatedField):
def to_internal_value(self, value):
return value
class SupplierPackageSerializer(serializers.ModelSerializer):
item1 = StringSerializer()
item2 = StringSerializer()
size = serializers.SerializerMethodField()
shipping_location= serializers.SerializerMethodField()
class Meta:
model = Package
fields = ['supplier', 'name', 'size', 'shipping_location', ]
read_only_fields = ['supplier']
# add this
def get_size(self, obj):
return PackageSizeSerializer(obj.item1).data
def get_shipping_location(self,obj):
return ShippingSerializer(obj.item2).data
DOCS:
https://www.django-rest-framework.org/api-guide/fields/#serializermethodfield
https://www.django-rest-framework.org/api-guide/relations/#stringrelatedfield
So basically The StringSerializer() class is a way to return a string representation of the model data.
While SerializerMethodField This is a read-only field. It gets its value by calling a method on the serializer class it is attached to. It can be used to add any sort of data to the serialized representation of your object.

Add Extra fields in serializer from one to one relation models

In this project, I have two models Student and Parent related to each other through one to one field.
In Parent serializer, I want to add Students attributes like age. I am thinking of using SerializerMethodField for both cases is their any better way to do it?
I am not getting the queries on how to get the object attributes and little explanation would be great.
Here what I have done till now.
Models.py
class Student(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True, default=None)
batch = models.ForeignKey(Batch, on_delete=models.CASCADE, null=True, related_name='students')
email = models.EmailField(null=True)
phone_number = models.CharField(max_length=10, null=True)
dob = models.DateField(blank=True, null=True, help_text="Enter in the following format : YYYY-MM-DD")
address = models.TextField(max_length=150, null=True)
age = models.IntegerField(blank=True)
image = models.ImageField(upload_to='profile_pictures', default='student_image.png', blank=True)
#property
def remarks(self):
return self.remark_set.all()
#property
def marks(self):
return self.marks_set.all()
def __str__(self):
return self.user.firstName + ' ' + self.user.lastName
class Parent(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True, default=None)
child = models.ForeignKey(Student, on_delete=models.CASCADE)
email = models.EmailField(null=True)
phone_number = models.CharField(max_length=10, null=True)
address = models.TextField(max_length=150, null=True)
image = models.ImageField(upload_to='profile_pictures', default='student_image.png', blank=True)
def __str__(self):
return self.user.firstName + ' ' + self.user.lastName
Serilaizer.py
class ParentSerializer(serializers.HyperlinkedModelSerializer):
student_age = serializers.SerializerMethodField()
student_batch = serializers.SerializerMethodField()
parent_name = serializers.SerializerMethodField()
class Meta:
model = Parent
fields = "__all__"
def get_student_age(self, obj):
return Parent.objects.get(child__age = self.obj.user.child)
def get_student_batch(self, obj):
return Parent.objects.get(child__bacth = self.obj.user.child)
def get_parent_name(self, user):
return Parent.objects.get(user=self.request.user)
Views.py
class ParentView( mixins.ListModelMixin, mixins.RetrieveModelMixin,viewsets.GenericViewSet):
queryset = Parent.objects.all()
serializer_class = serializers.ParentSerializer
first way:
from apps.models import Student, parent
class BasicUserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = "__all__"
class BasicStudentSerializer(serializers.ModelSerializer):
class Meta:
model = Student
fields = "__all__"
class ParentSerializer(serializers.ModelSerializer):
user = BasicUserSerializer(read_only=True,many=False)
child = BasicStudentSerializer(read_only=True, many=True)
class Meta:
model = Parent
fields = '__all__'
you can do this way . its replace a serializer field that you want and if parent have several child then in child's field you have new all child's information as dictionary.
================================================================
second way is use HyperLinkModel .
class ParentSerializer(serializers.ModelSerializer):
user = serializers.HyperlinkedRelatedField(read_only=True,many=False)
child = serializers.HyperlinkedRelatedField(read_only=True, many=True)
class Meta:
model = Parent
fields = '__all__'
but notice that in first way you will have a independent serializer class that every time you need to serialize model class that related to User or Child you can use them simply.

Many to Many model with a dropdown in Django Rest Framework?

I am trying to create a Many to Many relation with a model in between, I have a Client model, and a Zone model, each client may have access to different zones, and each zone may have multiple clients.
Therefore I created a model called Access Permission, that stores said relation, and I want to show a dropdown selector in the post form that shows the existing clients and zones, or to ask for the Id of an existing object, instead of showing the form to create new ones.
These are my models:
class Zone(models.Model):
name = models.TextField()
created = models.DateTimeField(auto_now=True)
def __str__(self):
return '%s' % (self.name)
class Client(models.Model):
name = models.TextField()
birthDate = models.DateField()
created = models.DateTimeField(auto_now=True)
def __str__(self):
return '%s' % (self.name)
class AccessPermission(models.Model):
idClient = models.ForeignKey(Client, on_delete=models.CASCADE, null=False)
idZone = models.ForeignKey(Zone, on_delete=models.CASCADE, null=False)
And these my current serializers:
class ZoneSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Zone
fields = ('name',)
class ClientSerializer(serializers.HyperlinkedModelSerializer):
zones = ZonesSerializer(source='accesspermission_set', many=True, read_only=True)
class Meta:
model = Client
fields = ('name', 'birthDate', 'zones')
class AccessPermissionSerializer(serializers.ManyRelatedField):
idClient = ClientSerializer(many=False)
idZone = ZoneSerializer(many=False)
class Meta:
model = AccessPermission
fields = ('idClient', 'idZone')
Is there any way to ask for the Id of an existing object, or show the existing ones, instead of the fields to create new ones?
You can do it like:
models
class AccessPermission(models.Model):
client = models.ForeignKey(Client, on_delete=models.CASCADE, null=False)
zone = models.ForeignKey(Zone, on_delete=models.CASCADE, null=False)
serializers
class AccessPermissionSerializer(serializers.ManyRelatedField):
id = serializers.IntegerField(read_only=True)
client_id = serializers.PrimaryKeyRelatedField(
queryset=Client.objects.all(), source='client', allow_null=False, required=True
)
zone_id = serializers.PrimaryKeyRelatedField(
queryset=Zone.objects.all(), source='zone', allow_null=False, required=True
)
class Meta:
model = AccessPermission
fields = (
'id', 'client_id', 'zone_id'
)

Django Rest Framework writable nested field using create()

I'm trying to write a create method that will write my nested fields but am finding that the nested object isn't written.
This is the sample I was using:
class UserSerializer(serializers.ModelSerializer):
profile = ProfileSerializer()
class Meta:
model = User
fields = ('username', 'email', 'profile')
def create(self, validated_data):
profile_data = validated_data.pop('profile')
user = User.objects.create(**validated_data)
Profile.objects.create(user=user, **profile_data)
return user
But I'm failing to understand what the user=user refers to.
Here is my code:
class MessagesSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.IntegerField(source='pk', read_only=True)
suggested_songs = SongSerializer()
class Meta:
model = Messages
fields = ('id','owner','url','suggested_songs',)
#fields = ('id','url','suggested_songs',)
def create(self, validated_data):
song_data = validated_data.pop('suggested_songs')
message = Messages.objects.create(**validated_data)
Song.objects.create(**song_data)
return message
class SongSerializer(serializers.HyperlinkedModelSerializer):
#id = serializers.IntegerField(source='pk', read_only=True)
class Meta:
model = Song
fields = ('id','title','artist','album','albumId','num_votes','cleared')
read_only_fields = ('song_id')
class Messages(models.Model):
owner = models.OneToOneField(User, primary_key=True, related_name='user_messages', editable=False) #TODO, change owner to 'To'
#suggested_songs = models.ManyToManyField(Song, related_name='suggested_songs')
suggested_songs = models.ForeignKey(Song, null=True, blank=True)
# If a user is added, this runs.
#receiver(post_save, sender=User)
def create_friend_for_user(sender, instance=None, created=False, **kwargs):
if created:
Messages.objects.get_or_create(owner=instance)
# Same as above, but for deletion
#receiver(pre_delete, sender=User)
def delete_friend_for_user(sender, instance=None, **kwargs):
if instance:
Messages.objects.get(owner=instance).delete()
class Song(models.Model):
"""
A model which holds information about the songs.
"""
#song_id = models.IntegerField(primary_key=True)
title = models.CharField(max_length=150, blank=True, default='')
artist = models.CharField(max_length=150, blank=True, default='')
album = models.CharField(max_length=150, blank=True, default='')
albumId = models.CharField(max_length=150, blank=True, default='')
num_votes = models.IntegerField(default=0, blank=True)
cleared = models.BooleanField(default=False, blank=True)
class Meta:
ordering = ('title',)
#managed=True
I think that the issue might be in the MessageSerializer.create method:
def create(self, validated_data):
# here you are popping the suggested songs
song_data = validated_data.pop('suggested_songs')
# so when you create the message here the foreign key is set to NULL
message = Messages.objects.create(**validated_data)
# and here you create the Song instance correctly but it is not
# associated with the message
Song.objects.create(**song_data)
return message
You need to pass the foreign key to the Messages.create method like in the example you have.
def create(self, validated_data):
song_data = validated_data.pop('suggested_songs')
song = Song.objects.create(**song_data)
# song need to be created first because the foreign key is in
# the Messages model
message = Messages.objects.create(suggested_songs=song, **validated_data)
return message
I hope this helps!

Categories

Resources