Django REST Framework: NestedSerializer in OneToMany relation - python

I have 2 related models:
class Systems(models.Model):
name = models.CharField(max_length=200)
description = models.CharField(max_length=2000, null=True, blank=True)
class Teams(models.Model):
system = models.ForeignKey(Systems, on_delete=models.CASCADE)
name = models.CharField(max_length=200)
description = models.CharField(max_length=2000, null=True, blank=True)
And 2 serializers:
class System_serializer(serializers.HyperlinkedModelSerializer):
id = serializers.ReadOnlyField
class Meta:
model = Systems
fields = ('id', 'name', 'description')
def create(self, validated_data):
return Systems.objects.create(**validated_data)
def update(self, system, validated_data):
system.name = validated_data.get('name', system.name)
system.description = validated_data.get('description', system.description)
system.save()
return system
second:
class Team_serializer(serializers.ModelSerializer):
id = serializers.ReadOnlyField
system_id = System_serializer()
class Meta:
model = Teams
fields = ('id', 'system_id', 'name', 'description')
def create(self, validated_data):
return Teams.objects.create(**validated_data)
def update(self, team, validated_data):
team.name = validated_data.get('name', team.name)
team.description = validated_data.get('description', team.description)
team.system_id = validated_data.get('system_id', team.system_id)
team.save()
return team
In 'team' entity I just want to have only 'System' ID, not the whole 'System'
How can I use the System_serializer, to serialize 'system_id' in 'team' object?
Thanks!
ADD
I tried to use SlugRelatedField:
class Team_serializer(serializers.ModelSerializer):
id = serializers.ReadOnlyField
system_id = serializers.SlugRelatedField(queryset=Systems.objects.all(), slug_field='id')
class Meta:
model = Teams
fields = ('id', 'system_id', 'name', 'description')
But got
'int' object has no attribute 'id'
ADD2
Tried to use PrimatyKeyRelatedField:
class Team_serializer(serializers.ModelSerializer):
id = serializers.ReadOnlyField
system_id = serializers.PrimaryKeyRelatedField(queryset=Systems.objects.all())
class Meta:
model = Teams
fields = ('id', 'system_id', 'name', 'description')
def create(self, validated_data):
return Teams.objects.create(**validated_data)
but got on create
Field 'id' expected a number but got <Systems: Systems object (2)>.

Related

Django rest framework - using SerializerMethodField with ModelSerializer

I have the following models.
models.py
class Language(models.Model):
name = models.CharField(max_length=255, unique=True)
class Subject(models.Model):
name = models.CharField(max_length=255, unique=True)
class Term(models.Model):
subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
language = models.ForeignKey(Language, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
definition = models.TextField()
image = models.ImageField(default='', blank=True)
I want to write a Serializer that will return a list of subjects. Each subject has a term field, which contains None or a single object, depending on the condition.
serializers.py
class TermSerializer(serializers.ModelSerializer):
language = serializers.CharField(source='language.name')
def to_representation(self, data):
data = data.get(language=self.context['language'])
return super(TermSerializer, self).to_representation(data)
class Meta:
model = Term
fields = ('id', 'language', 'name', 'definition', 'image')
class FilteredSubjectSerializer(serializers.ListSerializer):
def to_representation(self, data):
if self.context['condition']:
terms = Term.objects.filter(language=self.context['language'])
data = data.filter(term__in=terms)
return super(FilteredSubjectSerializer, self).to_representation(data)
class SubjectSerializer(serializers.ModelSerializer):
term = serializers.SerializerMethodField()
def get_term(self, term):
if self.context['condition']:
return TermSerializer(many=False, source='term_set').data
return None
class Meta:
model = Term
list_serializer_class = FilteredSubjectSerializer
fields = ('id', 'name', 'definition', 'image', 'term')
The problem is that when condition == True, the ViewSet returns incorrect data. All fields inside term have a default value.
It works fine if I write SubjectSerializer like this:
class SubjectSerializer(serializers.ModelSerializer):
term = serializers.TermSerializer(many=False, source='term_set')
class Meta:
model = Term
list_serializer_class = FilteredSubjectSerializer
fields = ('id', 'name', 'definition', 'image', 'term')
But the case when condition == False doesn't work.
Try passing the serializer the instance and the context.
Like this:
def get_term(self, instance):
if self.context['condition']:
return TermSerializer(many=False, source='term_set', instance=instance, context=self.context).data
return None

Contacts matching query does not exist in django rest framework

I am trying to update Contact model fields while creating the new fields of UpdateInfo model and add them to the existing model.
But I am getting this error
contacts.models.Contacts.DoesNotExist: Contacts matching query does not exist.
I know the contact object with the id 19 exists because I can see it when I try the get contacts API.
I am sending data like this.
My models:
class Contacts(models.Model):
full_name = models.CharField(max_length=100, blank=True)
.........
def __str__(self):
return self.full_name
class Meta:
verbose_name_plural = 'Potential Clients'
class UpdateInfo(models.Model):
contacts = models.ForeignKey(Contacts,on_delete=models.CASCADE, related_name='update_info')
updated_at = models.DateTimeField(auto_now=True)
modified_by = models.CharField(max_length=100, blank=True)
def __str__(self):
return f"Last modified by {self.modified_by} at {self.updated_at}"
My views:
class EditContactView(RetrieveUpdateDestroyAPIView):
permission_classes = [IsAuthenticated]
queryset = Contacts.objects.all()
serializer_class = ContactsUpdateSeializer
My serializers:
class UpdateInfoSerializer(serializers.ModelSerializer):
contacts= serializers.PrimaryKeyRelatedField(read_only=True)
class Meta:
model = UpdateInfo
fields = ['id','contacts','updated_at','modified_by']
class ContactsUpdateSeializer(serializers.ModelSerializer):
update_info = UpdateInfoSerializer(many=True)
id = serializers.PrimaryKeyRelatedField(read_only=True)
class Meta:
model = Contacts
fields = ['id', 'full_name', 'lead_source', 'email', 'phone', 'contact_owner',
'contact_status', 'company_name', 'job_position', 'tasks',
'notes', 'email_list', 'created_by', 'created_at', 'update_info']
def update(self, instance, validated_data):
update_data = validated_data.pop('update_info')
id = validated_data.get('id')
contacts = Contacts.objects.get(id=id)
#contacts.save()
#contact_only_update_logic
instance.full_name = validated_data.get('full_name')
instance.lead_source = validated_data.get('lead_source')
instance.email = validated_data.get('email')
instance.phone = validated_data.get('phone')
instance.contact_owner = validated_data.get('contact_owner')
instance.contact_status = validated_data.get('contact_status')
instance.company_name = validated_data.get('company_name')
instance.job_position = validated_data.get('job_position')
instance.tasks = validated_data.get('tasks')
instance.notes = validated_data.get('notes')
instance.email_list = validated_data.get('email_list')
instance.save()
#add_update_info_logic
for update_data in update_data:
abc = UpdateInfo.objects.create(contacts=contacts,**update_data)
instance.update_info.add(abc)
instance.save()
return instance
You have to change your serializer
class ContactsUpdateSeializer(serializers.ModelSerializer):
update_info = UpdateInfoSerializer(many=True)
class Meta:
model = Contacts
fields = ['id', 'full_name', 'lead_source', 'email', 'phone', 'contact_owner',
'contact_status', 'company_name', 'job_position', 'tasks',
'notes', 'email_list', 'created_by', 'created_at', 'update_info']
def update(self, instance, validated_data):
update_data = validated_data.pop('update_info')
instance = super(ContactsUpdateSeializer, self).update(instance, validated_data)
for update_datum in update_data:
abc = UpdateInfo.objects.create(contacts=instance,**update_datum)
return instance
PrimaryKeyRelatedField is used for foreign key purpose and there we have to define queryset also.
Don't need to add the updateinfo in contact, it is already done throgh django.
Moreover, it would be better if you use bulk_create instead of running save each time if you there is no signals exists for that.
You can do it as:-
UpdateInfo.objects.bulk_create([UpdateInfo(contacts=instance,**update_datum) for update_datum in update_data])

Creating view with foreign key model django restframework

I'm fairly new with the django restframework.
I am trying to create a serializer and a view for a model that has a foreignKey.
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)
state = models.CharField(max_length=255)
created_date = models.DateField(auto_now=False, auto_now_add=True)
salary = models.CharField(max_length=255)
position = models.CharField(max_length=255)
employer = models.CharField(max_length=255)
is_active = models.BooleanField(default=True)
def __str__(self):
return self.description[:50]
class Applicant(models.Model):
"""A applicant that can apply to a job"""
job = models.ForeignKey(Job, 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
The idea is that Applicant will be able to apply to Job.
I can't seem to figure out how to get the Job id when a Applicant is applying to job.
serializers.py
class JobSerializer(serializers.ModelSerializer):
"""Serializer for tag objects"""
class Meta:
model = Job
fields = ('id', 'description', 'job_type', 'city', 'state', 'salary', 'position', 'employer', 'created_date', 'is_active')
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"""
class Meta:
model = Applicant
fields = ('id', '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
Below is an image of my http://localhost:8000/api/jobPosting/apply/ url
I don't know how i can bring in the Jobs as in option to my view so I can obtain the id field.
I get the following error when POST
null value in column "job_id" violates not-null constraint
You are not passing job field in serializer fields. Try this:
class ApplyJobSerializer(serializers.ModelSerializer):
"""Serializer for applying to jobs"""
class Meta:
model = Applicant
fields = ('id', 'first_name', 'last_name', 'email', 'phone_number', 'resume', 'job')
read_only_fields = ('id',)

ValueError when trying to create through Django REST Framework

I get a ValueError Cannot assign "[]": "Match.stats" must be a "Stats" instance. when I try and create a match through the browsable API but can create one through the shell just fine.
If I remove the HyperlinkedRelatedField from the MatchSerializer it can create just fine.
models.py
class Player(models.Model):
name = models.CharField(max_length=30)
account = models.IntegerField()
place = models.CharField(max_length=30)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='players')
def __str__(self):
return self.name
class Match(models.Model):
game = models.IntegerField()
length = models.IntegerField()
win = models.BooleanField()
player = models.ForeignKey(Player, on_delete=models.CASCADE, related_name='matches')
def __str__(self):
return str(self.game)
class Stats(models.Model):
goals = models.IntegerField()
assists = models.IntegerField()
time = models.IntegerField()
match = models.OneToOneField(Match, on_delete=models.CASCADE, related_name='stats')
def __str__(self):
return '{} {}'.format(str(self.goals), str(self.match))
class Team(models.Model):
possession = models.IntegerField()
goals = models.IntegerField()
assists = models.IntegerField()
extra = models.BooleanField(default=False)
match = models.OneToOneField(Match, on_delete=models.CASCADE, related_name='teams')
def __str__(self):
return '{} - {}'.format(str(self.possession), str(self.match))
serializer.py
class UserSerializer(serializers.ModelSerializer):
players = serializers.HyperlinkedRelatedField(many=True, view_name='players-detail', queryset=Player.objects.all())
class Meta:
model = User
fields = ('id', 'username', 'email', 'first_name', 'last_name', 'players')
class PlayerSerializer(serializers.ModelSerializer):
user = serializers.ReadOnlyField(source='user.username')
matches = serializers.HyperlinkedRelatedField(many=True, view_name='matches-detail', queryset=Match.objects.all())
class Meta:
model = Player
fields = ('id', 'name', 'account', 'place', 'user', 'matches')
class MatchSerializer(serializers.ModelSerializer):
player = serializers.ReadOnlyField(source='player.name')
stats = serializers.HyperlinkedRelatedField(many=True, view_name='stats-detail', queryset=Stats.objects.all())
teams = serializers.HyperlinkedRelatedField(many=True, view_name='teams-detail', queryset=Team.objects.all())
class Meta:
model = Match
fields = ('id', 'game', 'length', 'win', 'player', 'stats', 'teams')
class StatsSerializer(serializers.ModelSerializer):
match = serializers.ReadOnlyField(source='match.game')
class Meta:
model = Stats
fields = ('id', 'goals', 'assists', 'time', 'match')
class TeamSerializer(serializers.ModelSerializer):
match = serializers.ReadOnlyField(source='match.game')
class Meta:
model = Team
fields = ('id', 'possession', 'goals', 'assists', 'extra', 'match')
I can go into python manage.py shell and create a match just fine.
>>>m = Match(game=12345, length=5674, win=True, player=player1) # a previously queried player
>>>m.save()
I'm just a little confused what is going
By default, HyperlineRelatedField allow both read and write operation, so I think you need make it read-only true:
class MatchSerializer(serializers.ModelSerializer):
player = serializers.ReadOnlyField(source='player.name')
stats = serializers.HyperlinkedRelatedField(view_name='stats-detail', read_only=True)
teams = serializers.HyperlinkedRelatedField(view_name='teams-detail', read_only=True)
class Meta:
model = Match
fields = ('id', 'game', 'length', 'win', 'player', 'stats', 'teams')
Also, you don't need to add many=True, because both teams and stats are OneToOne relations. So one entry for both tables will be created for each match.

Django Rest Framework update don't save changes

I have two models related through a one to one field, and I want to create a REST services that manages them both as they where one. The post works perfectly so far, and new instances are created in both models as they where one, but the put method just does nothing. It raises no error or anything, it just leaves the data unchanged.
These are my models:
class Company(models.Model):
name = models.CharField(max_length=40)
legal_name = models.CharField(max_length=100)
url = models.URLField()
address = models.TextField(max_length=400)
def __str__(self):
return "[{}]{}".format(self.id, self.name)
class Hotel(models.Model):
company = models.OneToOneField('Company', on_delete=models.CASCADE, primary_key=True)
code = models.CharField(max_length=10)
city = models.ForeignKey('City', on_delete=models.PROTECT, related_name='city_hotels')
category = models.ForeignKey('HotelCategory', on_delete=models.PROTECT, related_name='category_hotels')
capacity = models.IntegerField()
position = models.DecimalField(max_digits=11, decimal_places=2, default=0.00)
in_pickup = models.BooleanField(default=False)
def __str__(self):
return self.company.name
This is my ViewSet:
class HotelViewSet(viewsets.ModelViewSet):
queryset = models.Hotel.objects.all()
serializer_class = serializers.HotelSerializer
These are my serializers:
class CompanySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.Company
fields = ('id', 'name', 'legal_name', 'url', 'address')
class HotelSerializer(serializers.HyperlinkedModelSerializer):
company = CompanySerializer()
class Meta:
model = models.Hotel
fields = ('company', 'code', 'city', 'category', 'capacity', 'position', 'in_pickup')
def create(self, validated_data):
company_data = validated_data.pop('company')
new_company=models.Company.objects.create(**company_data)
hotel = models.Hotel.objects.create(company=new_company, **validated_data)
return hotel
def update(self, instance, validated_data):
company = models.Company(
id=instance.company.id,
name=instance.company.name,
legal_name=instance.company.legal_name,
url=instance.company.url,
address=instance.company.address
)
company.save()
instance.save()
return instance
I found that instance carries the original data, and validated_data carries the new data. I was saving the original data back.
I had to replace instance data with validated_data data and then save instance:
def update(self, instance, validated_data):
company_data = validated_data.pop('company')
company = models.Company.objects.get(id=instance.company.id)
company.name = company_data.get('name')
company.legal_name = company_data.get('legal_name')
company.tax_id = company_data.get('tax_id')
company.url = company_data.get('url')
company.address = company_data.get('address')
instance.company = company
instance.code = validated_data.get('code', instance.code)
instance.city = validated_data.get('city', instance.city)
instance.category = validated_data.get('category', instance.category)
instance.capacity = validated_data.get('capacity', instance.capacity)
instance.position = validated_data.get('position', instance.position)
instance.in_pickup = validated_data.get('in_pickup', instance.in_pickup)
instance.is_active = validated_data.get('is_active', instance.is_active)
company.save()
instance.save()
return instance
put method works handsomely now.
You are creating a new instance of company instead of updating the one that belongs to the Hotel instance.
company = Company.objects.get(id=instance.company.id)
company.name = instance.company.name
company.legal_name = instance.company.name
company.url = instance.company.url
company.address = instance.company.address
company.save()
instance.save()

Categories