Issue
I am trying to create a serializer to show a list of products in api_view. this list will include 3 things from my product model( their title, id, price). i use CharField for title with max_lenght=255 but i get and innit unknown_kwarg error for it. any solotions?
class Product(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField()
description = models.TextField(null=True, blank=True)
unit_price = models.DecimalField(
max_digits=6,
decimal_places=2,
validators=[MinValueValidator(1)])
inventory = models.IntegerField(validators=[MinValueValidator(0)])
last_update = models.DateTimeField(auto_now=True)
collection = models.ForeignKey(Collection, on_delete=models.PROTECT)
promotions = models.ManyToManyField(Promotion, blank=True)
from rest_framework import serializers
class ProductSerializer(serializers.Serializer):
id = serializers.IntegerField()
title = serializers.CharField()
unit_price = serializers.DecimalField(max_digits=6, decimal_places=2)
File "E:\python learning\storefront2\store\views.py", line 6, in <module>
from .serializers import ProductSerializer
File "E:\python learning\storefront2\store\serializers.py", line 3, in <module>
class ProductSerializer(serializers.Serializer):
File "E:\python learning\storefront2\store\serializers.py", line 5, in ProductSerializer
title = serializers.CharField(max_lenght=254)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\.virtualenvs\storefront2-EzF46c-9\Lib\site-packages\rest_framework\fields.py", line 730, in __init__
super().__init__(**kwargs)
TypeError: Field.__init__() got an unexpected keyword argument 'max_lenght'
After removing max_lenght, everything will work just fine!
Solution
It is a typo : -> max_lenght
max_length
title = serializers.CharField(max_length=254)
^^
Answered By - ilyasbbu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.