10

Using the Django REST framework I have the following Serializer below. I would like to add in (nested) related objects (ProductCatSerializer) to ProductSerializer. I have tried the following....

class ProductCatSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProductCat
        fields = ('id', 'title')

class ProductSerializer(serializers.ModelSerializer):
    """
    Serializing the Product instances into representations.
    """
    ProductCat = ProductCatSerializer()

    class Meta:
        model = Product
        fields = ('id', 'title', 'description', 'price',)

So what I want to happen is Products to show its related category nested in the results.

Thank you.

Update:

Using the depth = 2 option (thanks Nandeep Mali ) I now get the nested values, but they only show using ID's and not keyparis like the rest of the json request (see category below). Its almost right.

"results": [
        {
            "id": 1, 
            "title": "test ", 
            "description": "test", 
            "price": "2.99", 
            "product_url": "222", 
            "product_ref": "222", 
            "active": true, 
            "created": "2013-02-15T15:49:28Z", 
            "modified": "2013-02-17T13:05:28Z", 
            "category": [
                1, 
                2
            ], 
jason
  • 2,895
  • 8
  • 26
  • 38
  • Shouldn't the model in `ProductCatSerializer` be something else? As an aside, your name is really in sync with the question. – Nandeep Mali Feb 18 '13 at 14:50
  • Have you tried this? http://stackoverflow.com/questions/3753359/serializing-foreign-key-objects-in-django – Nandeep Mali Feb 18 '13 at 14:52
  • sorry just a mistake when typing the example, dow! corrected. Name yes lol :) – jason Feb 18 '13 at 14:52
  • @Nandeep Mali i'm trying to so this within http://django-rest-framework.org/api-guide/serializers.html – jason Feb 18 '13 at 14:53
  • 1
    Oh yes. Not very familiar with that framework. They do have nested support though: http://django-rest-framework.org/api-guide/serializers.html#specifiying-nested-serialization What is the issue that you had? Have you used the `depth = 1` setting? – Nandeep Mali Feb 18 '13 at 14:55
  • yes just stumbling to understand how I relate this with above. – jason Feb 18 '13 at 14:57
  • well I'll be how did i miss the option depth = 1!!!!!!!!!! only gives me the ID pks tho but seems to work on the models. now if I can just workout how to show the name with keypairs – jason Feb 18 '13 at 15:00
  • If you have figured out the answer it would be helpful to write it as an answer :) – dmg Feb 18 '13 at 15:02
  • yep, i now have a different question. you solved this one:) – jason Feb 18 '13 at 15:15

1 Answers1

9

Your example was almost right, except that you should call the field 'productcat' (or whatever the model relationshipt is called, but without the CamelCase), and add it to your fields.

class ProductCatSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProductCat
        fields = ('id', 'title')

class ProductSerializer(serializers.ModelSerializer):
    """
    Serializing the Product instances into representations.
    """
    productcat = ProductCatSerializer()

    class Meta:
        model = Product
        fields = ('id', 'title', 'description', 'price', 'productcat')
Tom Christie
  • 33,394
  • 7
  • 101
  • 86