MarkK
MarkK

Reputation: 1088

How serialize a DRF model class name?

I've been given an example JSON output which I must reproduce uisng Django Rest Framework.

Ths is the output:

"results": [
    {
      "Brand": [
        {
          "BrandName": "20/20",
          "Store": [
            {
              "ID": "4",
              "User": [
                "1"
              ],
         etc

How do I Model this in DRF?

Can this be done or have a missed something with models?

Upvotes: 0

Views: 353

Answers (1)

You should pass many=True into Storeserializer

class BrandSerializer(serializers.ModelSerializer):
    stores = Storeserializer(many=True)
    class Meta:
        """ Serializer configuration. """
        model = Brand
        fields = ("BrandName", "stores")

Also, if you get some troubles with field naming, you can use source parameter:

class SomeModel(...):
    some_field = models.CharField(...)  # pythonic snake_case


class SomeClassSerializer(...):
    class Meta:
        model = SomeModel
        fields = (“SomeField”,)
    SomeField = serializers.CharField(source=“some_field”)  # CamelCase if it needed

Upvotes: 1

Related Questions