у меня проблема токая Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\орозбек\Desktop\django\tutorial\pythonProject\snippets\serializers.py", line 5, in <module>
class SnippetSerializer(serializers.Serializer):
File "C:\Users\орозбек\Desktop\django\tutorial\pythonProject\snippets\serializers.py", line 11, in SnippetSerializer
style = serializers.CharField(choices=STYLE_CHOICES, default='friendly', max_length=100)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\орозбек\Desktop\django\tutorial\pythonProject\venv\Lib\site-packages\rest_framework\fields.py", line 730, in __init__
super().__init__(**kwargs)
TypeError: Field.__init__() got an unexpected keyword argument 'choices'
>>>
а это serializers.py
from rest_framework import serializers
from snippets.models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES
class SnippetSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
title = serializers.CharField(required=False, allow_blank=True, max_length=100)
code = serializers.CharField(style={'base_template': 'textarea.html'})
linenos = serializers.BooleanField(required=False)
language = serializers.ChoiceField(choices=LANGUAGE_CHOICES, default='python')
style = serializers.CharField(choices=STYLE_CHOICES, default='friendly', max_length=100)
def create(self, **validated_data):
return Snippet.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.title = validated_data.get('title', instance.title)
instance.code = validated_data.get('code', instance.code)
instance.linenos = validated_data.get('linenos', instance.linenos)
instance.language = validated_data.get('language', instance.language)
instance.style = validated_data.get('style', instance.style)
instance.save()
return instance
I don’t see anything in the docs at Serializer fields - Django REST framework showing that you can use choices
with a CharField
. I only see where you can use choices
with a ChoiceField
or a MultipleChoiceField
.
Side note: When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of modifying your original post to fix this.)
ошибка File “”, line 1, in
File “C:\Users\орозбек\Desktop\django\tutorial\pythonProject\snippets\serializers.py”, line 5, in
class SnippetSerializer(serializers.Serializer):
File “C:\Users\орозбек\Desktop\django\tutorial\pythonProject\snippets\serializers.py”, line 11, in SnippetSerializer
style = serializers.CharField(choices=STYLE_CHOICES, default=‘friendly’, max_length=100)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\орозбек\Desktop\django\tutorial\pythonProject\venv\Lib\site-packages\rest_framework\fields.py”, line 730, in init
super().init(**kwargs)
TypeError: Field.init() got an unexpected keyword argument ‘choices’
происходит кагда я пишу python manage.py shell
from snippets.serializers import SnippetSerializer
That’s because your definition for the style
field is not correct, as mentioned above.
на я же вить всё делаю по документам что я должен изменить
— from Google translate —
Пожалуйста, покажите мне, какая у вас есть документация, в которой говорится, что вы можете использовать атрибут choices
с CharField
. Я не могу найти ничего, что говорит о том, что это вариант.
— The original English —
Please show me what documentation you have that says that you can use the choices
attribute with a CharField
. I can’t find anything saying that that is an option.
Nope, that reference is not saying that.
Edited excerpts from that page.
The model:
class Snippet(models.Model):
^^^^^^^^^^^^^
...
style = models.CharField(choices=STYLE_CHOICES, default='friendly', max_length=100)
^^^^^^^^^^^^^^^^^^
...
The serializer:
class SnippetSerializer(serializers.Serializer):
^^^^^^^^^^^^^^^^^^^^^^
...
style = serializers.ChoiceField(choices=STYLE_CHOICES, default='friendly')
^^^^^^^^^^^^^^^^^^^
...
извенаюсь ошибка новичка я не увидел -->senky ← спасибо