Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show depth of a single field in Django Rest Framework?

I am using depth = 1 on my serializer to show details of a foreign key field. However, it is also showing details of another foreign key field which I don't really need. How do I show the details of one field but not the other one?

like image 959
MiniGunnR Avatar asked Nov 11 '16 05:11

MiniGunnR


People also ask

What is depth in Django REST framework?

DRF allows you to expose a REST framework based on your django data models with very little code needed. To get a basic REST API that supports Create-Read-Update-Delete operations on your data you have to do two things: You specify a serializer that tells drf how to go from your model to a JSON representation.

What is the difference between ModelSerializer and HyperlinkedModelSerializer?

The HyperlinkedModelSerializer class is similar to the ModelSerializer class except that it uses hyperlinks to represent relationships, rather than primary keys. By default the serializer will include a url field instead of a primary key field.

What is renderers in Django REST framework?

The rendering process takes the intermediate representation of template and context, and turns it into the final byte stream that can be served to the client. REST framework includes a number of built in Renderer classes, that allow you to return responses with various media types.

What is Restapi in Django?

Django REST framework is a powerful and flexible toolkit for building Web APIs. Some reasons you might want to use REST framework: The Web browsable API is a huge usability win for your developers. Authentication policies including packages for OAuth1a and OAuth2.


1 Answers

Just for your reference

Suppose you have three models:

class User(model.Model):
    username = model.CharField('username', max_length=10)

class Question(model.Model):
    title = models.CharField('title', max_length=10)

class Answer(model.Model):
    user = model.ForeignKey(User)
    question = model.ForeignKey(Question)
    body = model.TextField('the answer body')

And you need to serialise Answer, with showing the detail of Question, but not showing the detail of User, then you could define your serialisers like that:

class QuestionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Question
        exclude = []

class AnswerSerializer(serializers.ModelSerializer):
    question = QuestionSerializer(many=False, read_only=True)
    class Meta:
        model = Answer
        exclude = []

when you serialise Answer with AnswerSerializer, you will notice that question field is serialise at the same time, however user field is still an integer without serialising.

If you need to serialise a foreign key, you can define a field in the serializer explicitly, and the field name equal to the field name in model, and the value is equal to Foreign key model serializer. When the model is serialise, Answer in this case, the foreign key field, question for this case, will be "expanded" with QuestionSerializer, and other foreign key fields still keep the origin foreign key value, user in this case, if you haven't explicitly defined a serializer field in the serializer.

Hope it would help.

like image 133
Enix Avatar answered Oct 09 '22 09:10

Enix