__init__() missing 1 required positional argument: 'model_field'

I am trying to use Nested Serializer, if I remove the "product=ProductSerializer()" from serializer A, the code runs sucessfully, else it gives the below error.

How come the SizeSerializer is working but Productserializer don't.

model A

class SKU(models.Model):
    size = models.ForeignKey(Size,on_delete=models.CASCADE)
    product = models.ForeignKey(Product,on_delete=models.CASCADE)

    number = models.CharField(max_length=100,null=True,blank=True)
    price = models.IntegerField(null=True,blank=True)
    parent_sku = models.CharField(max_length=100,null=True,blank=True)
    active = models.BooleanField(default=True)

model B

class Product(models.Model):      
    name = models.CharField(max_length=200,null=True,blank=True)
    active = models.BooleanField(default=True)

serializer A

class SKU_Serializer(serializers.ModelSerializer):
    size    = SizeSerializer()
    product = ProductSerializer()

    class Meta:
        model = SKU
        exclude = ('created_at','updated_at')

serializer B

class ProductSerializer(serializers.ModelField):

    class Meta:
        model = Product
        exclude = ('created_at','updated_at')

json

{
        "id": 1,
        "size": {
            "id": 1,
            "name": "XL",
            "active": true
        },
        "number": "Muriel Treppas",
        "price": 899,
        "parent_sku": "Muriel",
        "active": true,
        "product": 1
    },

output(error)

  File "D:\1___DEVELOPMENT\Clothing\hoplet\sku\views.py", line 6, in <module>
    from sku.serializers import SKU_Serializer
  File "D:\1___DEVELOPMENT\Clothing\hoplet\sku\serializers.py", line 8, in <module>
    class SKU_Serializer(serializers.ModelSerializer):
  File "D:\1___DEVELOPMENT\Clothing\hoplet\sku\serializers.py", line 10, in SKU_Serializer
    product = ProductSerializer()
TypeError: __init__() missing 1 required positional argument: 'model_field'

Upvotes: 4

Views: 2235

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599630

ProductSerializer is inheriting from serializers.ModelField instead of serializers.ModelSerializer.

Upvotes: 20

Related Questions