Get fields of foreign key in serializer django
NickName:Arian Hassani Ask DateTime:2020-10-31T21:53:51

Get fields of foreign key in serializer django

I'm 2 weeks into django so this might be dumb! In my django project, I have 3 models, Teacher, Student and a CustomUser. CustomUser has 2 fields first_name and last_name. Student has 3 fields grade, field_of_study and user which is a foreign key to CustomUser. Now inside my StudentSerializer I want to serialize first_name, last_name, grade and field of study. But because the last 2 field are not direct fields of Student, I cannot do that. Anyone know how this should be handled?

I also came across this Retrieving a Foreign Key value with django-rest-framework serializers but the answer didn't help really.

Copyright Notice:Content Author:「Arian Hassani」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/64622432/get-fields-of-foreign-key-in-serializer-django

More about “Get fields of foreign key in serializer django” related questions

Get fields of foreign key in serializer django

I'm 2 weeks into django so this might be dumb! In my django project, I have 3 models, Teacher, Student and a CustomUser. CustomUser has 2 fields first_name and last_name. Student has 3 fields grade,

Show Detail

Validating a foreign key field in a serializer django rest framework

I am using django rest framework and have a html form which sends data to the rest api. I am doing a serializer.is_valid check and save() on the request data. In the the front end form I have an ID...

Show Detail

django - key error in serializer with foreign key models

I checked out all related questions but still curious to know if there exist an updated answer . django serialize foreign key objects So , I have a model which has three foreign keys , next when...

Show Detail

Django REST Framework serializer - access existing foreign key

I am using Django Rest Framework in my app, and I need to create new model instances which contain foreign keys. These refer to existing objects in another table, so I don't want new instances of t...

Show Detail

foreign key called in another serializer

I was looking at the serialzier part in django rest framework. I saw the following models in an example class Album(models.Model): album_name = models.CharField(max_length=100) artist = m...

Show Detail

How do I get my Django serializer to save my foreign key objects?

I'm using Python 3.8 and Django 3. I have the following models. Notice the second has foreign keys to the first ... class ContactMethod(models.Model): class ContactTypes(models.TextChoices): ...

Show Detail

Django foreign key field read and write

I am using Django rest framework for a project. In the project, there is a Student model, which has a foreign key referring to a user object. class Student(models.Model): user = models.OneToOne...

Show Detail

Django fails when saving a serializer with foreign key references

So I had a model with one foreign key reference that was working really well for a few weeks. I ended up having to add another foreign key reference to the model and now I'm not able to save any re...

Show Detail

In Django serializer, how do I serialize both sides of a foreign key relationship?

In my Django code, I have this model: class Swipe(models.Model): user = models.ForeignKey( Profile, related_name='swipes', on_delete=models.CASCADE, null=False,...

Show Detail

django rest framework - foreign key fields are read only

The model: class Item(models.Model): company = models.ForeignKey(Company, on_delete=models.CASCADE) item_num = models.IntegerField() # other fields... class Meta: unique_to...

Show Detail