3

I'm working with Django Framework. I have two models: Component and ComponentProperty.

class Component(models.Model):
    name = models.CharField(unique=True, max_length=255)
    component_properties = models.ManyToManyField(ComponentProperty)

class ComponentProperty(models.Model):
    label = models.CharField(unique=True, max_length=255)
    component_type = models.CharField(max_length=255)

And serializers:

class ComponentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Component
        fields = ('name', 'component_properties')
        depth = 1

class ComponentPropertySerializer(serializers.ModelSerializer):
    class Meta:
        model = ComponentProperty
        fields = ('label', 'component_type')

I'm trying to load data with fixtures. I wrote and fixture file that look like this:

[
   {
      "pk":1,
      "model":"api.componentproperty",
      "fields":{
         "label":"label",
         "component_type":"text"
      }
   },
   {
      "pk":2,
      "model":"api.componentproperty",
      "fields":{
         "label":"description",
         "component_type":"text",

      }
   },
   {
      "pk":1,
      "model":"api.component",
      "fields":{
         "name":"text",
         "component_properties":[
            1,
            2
         ]
      }
   }
]

That's work fine! But I have 20 fixtures to load. I want to have fixture (component.json for example) look like this bellow:

[
   {
      "pk":null,
      "model":"api.component",
      "fields":{
         "name":"text",
         "component_properties":[
            {
               "pk":null,
               "model":"api.componentproperty",
               "fields":{
                  "label":"label",
                  "component_type":"text"
               }
            },
            {
               "pk":null,
               "model":"api.componentproperty",
               "fields":{
                  "label":"description",
                  "component_type":"text",

               }
            }
         ]
      }
   }
]

The icing on the cake must be fixtures specifying a primary key like here. Please, can anyone help me to write these fixtures without pk and the relationships described above?

Aneesh R S
  • 3,807
  • 4
  • 23
  • 35
Junior Gantin
  • 2,102
  • 1
  • 18
  • 24
  • what happens if you omit `pk` or set it to `null`? – Jahongir Rahmonov Aug 16 '17 at 11:16
  • django.core.serializers.base.DeserializationError: Problem installing fixture component.json': ["'{'pk': None, 'model': 'api.componentproperty', 'fields': {'component_type': 'text', 'default_value': 'string', 'data': [], 'label': 'label'}}' value must be an integer."]: (api.component:pk=None) field_value was '{'pk': None, 'model': 'api.componentproperty', 'fields': {'component_type': 'text', 'default_value': 'string', 'data': [], 'label': 'label'}}' – Junior Gantin Aug 16 '17 at 11:35
  • I'm trying to have fixture file look like: [component.json](https://drive.google.com/file/d/0B30Ar7SFl-tXdkJJRTY2cnBhck0/view?usp=sharing) – Junior Gantin Aug 16 '17 at 11:40
  • Can you add your `serializers.py` to the question? – wencakisa Aug 16 '17 at 11:44
  • @wencakisa Done! – Junior Gantin Aug 16 '17 at 11:52

1 Answers1

2

As far as I understood, you want to perform nested serialization of Component properties in your ComponentSerializer.

It can be done if you use your ComponentPropertySerializer inside your ComponentSerializer:

class ComponentPropertySerializer(serializers.ModelSerializer):
    class Meta:
        model = ComponentProperty
        fields = ('label', 'component_type')


class ComponentSerializer(serializers.ModelSerializer):
    # Django REST Framework supports nested serialization
    # You serialize your *component_properties* with your ComponentPropertySerializer
    # As they are many properties, you use *many=True*

    component_properties = ComponentPropertySerializer(many=True, read_only=True)

    class Meta:
        model = Component
        fields = ('name', 'component_properties')
        depth = 1

I think that this should reproduce the desired output. Nested serialization is something very useful and well-documented in Django REST Framework's docs.

wencakisa
  • 5,850
  • 2
  • 15
  • 36
  • Thank you for your help. In fact, what I would like to do is load the initial data into the database. I chose the fixtures to do it. What I'm trying to do is create fixtures file like above (the second json file). – Junior Gantin Aug 16 '17 at 12:19