Alex Fox
Alex Fox

Reputation: 50

How to serialize file for creating models which connected by foreignkey. Django Rest Framework

I have several files "ABC" and each has several parts with different data. Also, I have a parser that parse this file to dict in format:

{part1: {"name":"test name", 
         "dataKey1":"i have a model for this key", 
         "dataKey2":{"a":123, "b":222}},
 part2: {"name":"test name 2", 
         "dataKey1":"not empty string", 
         "dataKey2":{"a":080, "b":999}}
}

I have a model for some keys in nested dicts. I have a model for some keys in the files. I have a model for file. And I have a model for keeping list of all files.

How serializer might be structured for handling many files(which I get with request) each of which will be parsed with my function and serialized to create models not only for each file but for each required nested dict???

Upvotes: 1

Views: 150

Answers (2)

anowlinorbit
anowlinorbit

Reputation: 357

Hei Alex,

Let's take a look at a question and answer model.

First the question with a single attribute which is the text of the question itself. We could have a question with the text, "Is the moon a sphere?"

class Question(models.Model):
    question = models.TextField()

Second, we have an answer. An answer is related to a question. For example, we could have two answers, "True" and "False" and both have the above question as their Foreign Key.

class Answer(models.Model):
    answer = models.TextField()
    question = models.ForeignKey(Question, related_name='answer', on_delete=models.CASCADE)

One of the key things to note above is the related_name='answer' on the ForeignKey. This, amongst other things, allows us to fetch related Answers when querying Question.

N.B. If you add a related_name to your model, you will have to migrate your changes.

Let's look at how we can do that with DRF's serializers.

class AnswerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Answer
        fields = ('id', 'answer', 'question')


class QuestionSerializer(serializers.ModelSerializer):
    answer = AnswerSerializer(many=True, read_only=True)

    class Meta:
        model = Question
        fields = ['id', 'question', 'answers']

The AnswerSerializer is fairly simple as we serializing the model Answer exactly as it appears within its own model class.

The QuestionSerializer makes us of the related_name we setup in our Answer mode. Here we can define an additional attribute we want to serialize: Answers.

Because we have a ForeignKey from Answer to Question with a defined related_name, we can use this related name as a field in the ModelSerializer for the related Model.

You will have probably noted that the related_name is answer and is the same name we use in the QuestionSerializer.

Using this logic as above, you will be able to build nested serializers as I've done here and by using the related_name property on your relationships, you'll be able to add related fields in your serializers.

Good luck!

Upvotes: 1

Related Questions