Django - Handling multiple nested models at different levels


CH Andre Meloa

I have some questions about Django nested models.

As far as I know, to handle nested data structures, I have to deserialize each data and then create objects that will be concatenated with the ForeignKeyField.

I can handle this by overriding the method overriding the method for .create()each nested data and .pop()creating each object model separately . Here is an example of how I handle 2 level nested data (based on documentation: DRF documentation )

def create(self, validated_data):
    child_data = validated_data.pop('child_data')
    parent_obj = ParentModel.objects.create(**validated_data)
    for data in child_data:
        ChildModel.objects.create(parent_id=parent_obj, **data)
    return parent_obj

I'm having some trouble here. I have multiple multi-level data structures, maybe data like the following:

    "data_a": [
        {
            "data_a_1": [
                {
                    "data_a_1_1": [
                        {
                            "data_a_1_1_1": "example_a_1_1_1",
                            "data_a_1_1_2": "example_a_1_1_2"
                        }
                    ]
                },
                {
                    "data_a_1_2_1": "Example a-1-2-1",
                    "data_a_1_2_2": "Example a-1-2-2"
                }
            ]
            "data_a_2": [
                {
                    "data_a_2_1": "Example a-2-1"
                },
                {
                    "data_a_2_2": "Example a-2-2"
                }
            ]
        }
    ]

my question is:

  1. What's the best way to handle this dynamic data structure? .create()As mentioned above , rewriting would require long hard-coded scripts to handle multiple multi-level data structures .
  2. Are there any other best practices for dealing with the above data structures?
CH Andre Meloa

I found a solution for this structure.

I read in the DRF documentation that there is a 3rd party package that can easily handle drf-writable-nested parent-child relationships

Not having to hard code .create()and .NET to deal with such data structure issues helps me a lot .update(). As mentioned above, this package provides special methods for handling parent-child CRUD problems.

Here is an example:

models.py:

class GrandChildModel(models.Model):
    name = models.CharField(max_length=32, blank=True, null=True)
    parent_id = models.ForeignKeyField(ChildModel, on_delete=CASCADE, related_name='grandchild')

class ChildModel(models.Model):
    name = models.CharField(max_length=32, blank=True, null=True)
    parent_id = models.ForeignKeyField(ParentModel, on_delete=CASCADE, related_name='child')

class ParentModel(models.Model):
    parent_name = model.CharField(max_length=32, blank=True, null=True)

serializer

from drf_writable_nested.serializers import WritableNestedModelSerializer

class GrandChildSerializer(serializers.ModelSerializer):
    class Meta:
        model=GrandChildModel
        fields = ["name"]

class ChildSerializer(WritableModelSerializer):
    grandchild = GrandChildSerializer(many=True)
    class Meta:
        model=ChildModel
        fields = ["name", "grandchild"]

class ParentSerializer(WritableModelSerializer):
    child = ChildSerializer(many=True)
    class Meta:
        model=ChildModelimport 
        fields = ["name", "child"]

This way you can handle all types of nested data :)

Related


Django - Handling multiple nested models at different levels

CH Andre Meloa I have some questions about Django nested models. As far as I know, to handle nested data structures, I have to deserialize each data and then create objects that will be concatenated with the ForeignKeyField. I can handle this by overriding the

Django - Handling multiple nested models at different levels

CH Andre Meloa I have some questions about Django nested models. As far as I know, to handle nested data structures, I have to deserialize each data and then create objects that will be concatenated with the ForeignKeyField. I can handle this by overriding the

Django - Handling multiple nested models at different levels

CH Andre Meloa I have some questions about Django nested models. As far as I know, to handle nested data structures, I have to deserialize each data and then create objects that will be concatenated with the ForeignKeyField. I can handle this by overriding the

Handling different levels of nesting in Django REST Framework

Smills For example, if you take the following model: class Region(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=256) class Company(models.Model): id = models.AutoField(primary_key=True) name = models

Handling different levels of nesting in Django REST Framework

Smills For example, if you take the following model: class Region(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=256) class Company(models.Model): id = models.AutoField(primary_key=True) name = models

Handling different levels of nesting in Django REST Framework

Smills For example, if you take the following model: class Region(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=256) class Company(models.Model): id = models.AutoField(primary_key=True) name = models

Handling different levels of nesting in Django REST Framework

Smills For example, if you take the following model: class Region(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=256) class Company(models.Model): id = models.AutoField(primary_key=True) name = models

Handling different levels of nesting in Django REST Framework

Smills For example, if you take the following model: class Region(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=256) class Company(models.Model): id = models.AutoField(primary_key=True) name = models

Django multiple forms for different models

Simon I'm new to Django, but I've been stuck with it because I haven't been able to solve my problem for a day. I have to create a form with a lot of fields. The fields are from different models and I want to save them all at once. I want to display it in a sl

Django multiple forms for different models

Simon I'm new to Django, but I've been stuck with it because I haven't been able to solve my problem for a day. I have to create a form with a lot of fields. The fields are from different models and I want to save them all at once. I want to display it in a sl

Django multiple forms for different models

Simon I'm new to Django, but I've been stuck with it because I haven't been able to solve my problem for a day. I have to create a form with a lot of fields. The fields are from different models and I want to save them all at once. I want to display it in a sl

Django multiple forms for different models

Simon I'm new to Django, but I've been stuck with it because I haven't been able to solve my problem for a day. I have to create a form with a lot of fields. The fields are from different models and I want to save them all at once. I want to display it in a sl

Django multiple forms for different models

Simon I'm new to Django, but I've been stuck with it because I haven't been able to solve my problem for a day. I have to create a form with a lot of fields. The fields are from different models and I want to save them all at once. I want to display it in a sl

django rest framework nested fields with multiple models

Momokjaaaaa This is django and django rest framework. I have 2 models: User and Phone. first question: I want to be able to update user data (email) as well as phone data (phone number) in 1 api update response. Phone numbers can be 0 or many. Well, it's actua

Django insert performance with multiple nested models

Thanh Nguyen I have the following model definition: class Workflow(models.Model): name = models.CharField(max_length=255) class Step(models.Model): workflow = models.ForeignKey(Section, on_delete=models.CASCADE, related_name='steps') title = model

django rest framework nested fields with multiple models

Momokjaaaaa This is django and django rest framework. I have 2 models: User and Phone. first question: I want to be able to update user data (email) as well as phone data (phone number) in 1 api update response. Phone numbers can be 0 or many. Well, it's actua

django rest framework nested fields with multiple models

Momokjaaaaa This is django and django rest framework. I have 2 models: User and Phone. first question: I want to be able to update user data (email) as well as phone data (phone number) in 1 api update response. Phone numbers can be 0 or many. Well, it's actua

Django insert performance with multiple nested models

Thanh Nguyen I have the following model definition: class Workflow(models.Model): name = models.CharField(max_length=255) class Step(models.Model): workflow = models.ForeignKey(Section, on_delete=models.CASCADE, related_name='steps') title = model

Nested Json for Django multiple foreign key models

Mehmet Ince I have 4 models with relationships via FK. class Journal(models.Model): name = models.CharField(max_length=255) class Volume(models.Model): journal = models.ForeignKey(Journal, related_name='volumes') number = models.IntegerField() cl

django rest framework nested fields with multiple models

Momokjaaaaa This is django and django rest framework. I have 2 models: User and Phone. first question: I want to be able to update user data (email) as well as phone data (phone number) in 1 api update response. Phone numbers can be 0 or many. Well, it's actua

Django insert performance with multiple nested models

Thanh Nguyen I have the following model definition: class Workflow(models.Model): name = models.CharField(max_length=255) class Step(models.Model): workflow = models.ForeignKey(Section, on_delete=models.CASCADE, related_name='steps') title = model

Nested Json for Django multiple foreign key models

Mehmet Ince I have 4 models with relationships via FK. class Journal(models.Model): name = models.CharField(max_length=255) class Volume(models.Model): journal = models.ForeignKey(Journal, related_name='volumes') number = models.IntegerField() cl