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.CharField(max_length=256)
    region = models.ForeignKey('Region', db_index=True)

class Staff(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=256)
    company = models.ForeignKey('Company', db_index=True)

I know I don't need to include the id in these models, but I did it to make it clearer.

In this example, sometimes you just want to simply return a list of regions. Other times, you want to return a list of regions and a list of companies in each region under each region.

You also need more details, I guess, where you can find a list of regions, their company subsidiaries, and each company's employee subsidiaries.

As far as the rest of the frame views are concerned, what is the best way to handle these different levels of depth/detail. How do people usually deal with it?

I mean, what naming convention would you use when you say three views return the same content at the top level, the only difference is how many levels of nesting they contain?

NehaG

return list area only

-> use a simple serializer with many=True when calling it (this will return a list of dictionaries)

class SimpleRegionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Region
        fields = ('id', 'name')

Get a list of regions and a list of companies in each region under each region

-> use nested serializers

 class CompanySerializer(serializers.ModelSerializer):
     class Meta:
         model = Company
         fields = ('id', 'name')

 class NestedRegionSerializer(serializers.ModelSerializer):
     company = CompanySerializer(many=True, read_only=True)

     class Meta:
         model = Region
         fields = ('id', 'name')

Also changed the company model to

 class Company(models.Model):
     id = models.AutoField(primary_key=True)
     name = models.CharField(max_length=256)
     region = models.ForeignKey('Region', db_index=True, related_name='company')

related_name is the same as the serializer field in NestedRegionSerializer.

Don't forget to run migrations or syncdb

See http://www.django-rest-framework.org/api-guide/relations/#nested-relationships for more reference for more information.

You can use any level of nesting, and if desired, you can also specify "depth" as a meta option to provide information about the depth of nesting.

Related


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 rest framework logging different levels on different files

Thanos I am working on Django REST framework and I would like to have separate files for logging data. I want a file for simple transactions like GET, PUT, POST, etc, and a file with an error that I will collect if an error occurs. I've been reading the Django

Django rest framework logging different levels on different files

Thanos I am working on Django REST framework and I would like to have separate files for logging data. I want a file for simple transactions like GET, PUT, POST, etc, and a file with an error that I will collect if an error occurs. I've been reading the Django

Django rest framework logging different levels on different files

Thanos I am working on Django REST framework and I would like to have separate files for logging data. I want a file for simple transactions like GET, PUT, POST, etc, and a file with an error that I will collect if an error occurs. I've been reading the Django

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

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 Rest Framework foreign key nesting

Nader Besada I am trying to nest my Users table inside the Relationships table. So instead of this: [ { "user": 1, "related_user": 2, "relationship": "followed_by" } ] I am trying to get this: [ { "user": {

Automatic reference nesting with Django Rest Framework

André Carvalho I have this model in Django (based on legacy database): class NationalTest(models.Model): unityId = models.IntegerField(primary_key=True) parentId = models.IntegerField(blank=True) grade = models.FloatField(blank=True) This model s

Django Rest Framework foreign key nesting

Nader Besada I am trying to nest my Users table inside the Relationships table. So instead of this: [ { "user": 1, "related_user": 2, "relationship": "followed_by" } ] I am trying to get this: [ { "user": {

Need help with nesting Django Rest Framework

SRC I am using DRF for the first time and am stuck in one place. This is just a hypothetical example related to the problem I'm facing: models.py class Manufacturer(models.Model): manufacturer_id = models.CharField(max_length=25, primary_key=True) manu

Need help with nesting Django Rest Framework

SRC I am using DRF for the first time and am stuck in one place. This is just a hypothetical example related to the problem I'm facing: models.py class Manufacturer(models.Model): manufacturer_id = models.CharField(max_length=25, primary_key=True) manu

Automatic reference nesting with Django Rest Framework

André Carvalho I have this model in Django (based on legacy database): class NationalTest(models.Model): unityId = models.IntegerField(primary_key=True) parentId = models.IntegerField(blank=True) grade = models.FloatField(blank=True) This model s

Django Rest Framework foreign key nesting

Nader Besada I am trying to nest my Users table inside the Relationships table. So instead of this: [ { "user": 1, "related_user": 2, "relationship": "followed_by" } ] I am trying to get this: [ { "user": {

Automatic reference nesting with Django Rest Framework

André Carvalho I have this model in Django (based on legacy database): class NationalTest(models.Model): unityId = models.IntegerField(primary_key=True) parentId = models.IntegerField(blank=True) grade = models.FloatField(blank=True) This model s

Django Rest Framework foreign key nesting

Nader Besada I am trying to nest my Users table inside the Relationships table. So instead of this: [ { "user": 1, "related_user": 2, "relationship": "followed_by" } ] I am trying to get this: [ { "user": {

Need help with nesting Django Rest Framework

SRC I am using DRF for the first time and am stuck in one place. This is just a hypothetical example related to the problem I'm facing: models.py class Manufacturer(models.Model): manufacturer_id = models.CharField(max_length=25, primary_key=True) manu

Automatic reference nesting with Django Rest Framework

André Carvalho I have this model in Django (based on legacy database): class NationalTest(models.Model): unityId = models.IntegerField(primary_key=True) parentId = models.IntegerField(blank=True) grade = models.FloatField(blank=True) This model s

Automatic reference nesting with Django Rest Framework

André Carvalho I have this model in Django (based on legacy database): class NationalTest(models.Model): unityId = models.IntegerField(primary_key=True) parentId = models.IntegerField(blank=True) grade = models.FloatField(blank=True) This model s

Align buttons at different nesting levels

Octavian I have an HTML form with two buttons. Only show one of the buttons if the variable is true (I'm using React). <form> {this.state.classificationSelected == true ? <div> <ListAddition classification=

Align buttons at different nesting levels

Octavian I have an HTML form with two buttons. Only show one of the buttons if the variable is true (I'm using React). <form> {this.state.classificationSelected == true ? <div> <ListAddition classification=

Align buttons at different nesting levels

Octavian I have an HTML form with two buttons. Only show one of the buttons if the variable is true (I'm using React). <form> {this.state.classificationSelected == true ? <div> <ListAddition classification=

Django Rest Framework database error exception handling

Aru Is there any way to make Django Rest Framework automatically respond HTTP_400_STATUSwhen there is a database exception ? ( IntegrityErrorand so on) Example: I have a model with a unique username field and I'm trying to use a generic model rest_framework.Li