Django REST_Framework序列化程序抛出错误

isr3a4wc  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(106)

在serializers.py

from rest_framework import serializers
from .models import Book ,MenuItem

class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = ['id','title','author','price']

class MenuItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = MenuItem
        fields = ['id','title','price','inventory']
        extra_kwargs = {
            'price': {'min_value': 2},
            'inventory':{'min_value':0}
        }

字符串
我的模型

class MenuItem(models.Model):
    title = models.CharField(max_length=255)
    price = models.DecimalField(max_digits=6,decimal_places=2)
    inventory = models.SmallIntegerField


试图发送获取请求到我的视图,我面临的问题。

Applying littlelemonapi.0002_menuitem... OK
(API) rustemaisariyev@MacBook-Pro-Rustem littlelemon % python3 manage.py runserver     
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
July 28, 2023 - 16:20:24
Django version 4.2.3, using settings 'littlelemon.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Internal Server Error: /api/menu-items
Traceback (most recent call last):
  File "/Users/rustemaisariyev/.local/share/virtualenvs/API-BdtIIPdy/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rustemaisariyev/.local/share/virtualenvs/API-BdtIIPdy/lib/python3.11/site-packages/django/core/handlers/base.py", line 220, in _get_response
    response = response.render()
               ^^^^^^^^^^^^^^^^^
  File "/Users/rustemaisariyev/.local/share/virtualenvs/API-BdtIIPdy/lib/python3.11/site-packages/django/template/response.py", line 114, in render
    self.content = self.rendered_content
                   ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rustemaisariyev/.local/share/virtualenvs/API-BdtIIPdy/lib/python3.11/site-packages/rest_framework/response.py", line 70, in rendered_content
    ret = renderer.render(self.data, accepted_media_type, context)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rustemaisariyev/.local/share/virtualenvs/API-BdtIIPdy/lib/python3.11/site-packages/rest_framework/renderers.py", line 723, in render
    context = self.get_context(data, accepted_media_type, renderer_context)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rustemaisariyev/.local/share/virtualenvs/API-BdtIIPdy/lib/python3.11/site-packages/rest_framework/renderers.py", line 654, in get_context
    raw_data_post_form = self.get_raw_data_form(data, view, 'POST', request)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rustemaisariyev/.local/share/virtualenvs/API-BdtIIPdy/lib/python3.11/site-packages/rest_framework/renderers.py", line 562, in get_raw_data_form
    data = serializer.data.copy()
           ^^^^^^^^^^^^^^^
  File "/Users/rustemaisariyev/.local/share/virtualenvs/API-BdtIIPdy/lib/python3.11/site-packages/rest_framework/serializers.py", line 555, in data
    ret = super().data
          ^^^^^^^^^^^^
  File "/Users/rustemaisariyev/.local/share/virtualenvs/API-BdtIIPdy/lib/python3.11/site-packages/rest_framework/serializers.py", line 257, in data
    self._data = self.get_initial()
                 ^^^^^^^^^^^^^^^^^^
  File "/Users/rustemaisariyev/.local/share/virtualenvs/API-BdtIIPdy/lib/python3.11/site-packages/rest_framework/serializers.py", line 405, in get_initial
    for field in self.fields.values()
                 ^^^^^^^^^^^
  File "/Users/rustemaisariyev/.local/share/virtualenvs/API-BdtIIPdy/lib/python3.11/site-packages/django/utils/functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
                                         ^^^^^^^^^^^^^^^^^^^
  File "/Users/rustemaisariyev/.local/share/virtualenvs/API-BdtIIPdy/lib/python3.11/site-packages/rest_framework/serializers.py", line 356, in fields
    for key, value in self.get_fields().items():
                      ^^^^^^^^^^^^^^^^^
  File "/Users/rustemaisariyev/.local/share/virtualenvs/API-BdtIIPdy/lib/python3.11/site-packages/rest_framework/serializers.py", line 1086, in get_fields
    fields[field_name] = field_class(**field_kwargs)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rustemaisariyev/.local/share/virtualenvs/API-BdtIIPdy/lib/python3.11/site-packages/rest_framework/fields.py", line 1780, in __init__
    super().__init__(**kwargs)
TypeError: Field.__init__() got an unexpected keyword argument 'min_value'
[28/Jul/2023 16:20:27] "GET /api/menu-items HTTP/1.1" 500 124773

ztmd8pv5

ztmd8pv51#

要为price字段设置一个最小值,可以使用Django的验证器来强制执行该字段的验证规则

class MenuItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = MenuItem
        fields = ['id', 'title', 'price', 'inventory']

    price = serializers.DecimalField(max_digits=6, decimal_places=2, validators=[MinValueValidator(2)])
    inventory = serializers.IntegerField(validators=[MinValueValidator(0)])

字符串

相关问题