0
モデルのデフォルト機能を正しく書き込む方法は?モデルのデフォルト機能を正しく書き込む方法は?
私はTestDefalt
モデル、およびデフォルトの機能があります。
def genefunc(instance, detail):
str = instance.name + "/abc/"
return str
class TestDefalt(models.Model):
name = models.CharField(max_length=11)
detail = models.CharField(default=genefunc, max_length=11)
そして、私のシリアライザとビューで:
:私はTestDCreateAPIView
にアクセスすると
# serializer
class TestDefaultSerializer(ModelSerializer):
class Meta:
model = TestDefalt
fields = "__all__"
# view
class TestDCreateAPIView(CreateAPIView):
serializer_class = TestDefaultSerializer
permission_classes = []
queryset = TestDefalt.objects.all()
、私は怒鳴るエラーを取得します
TypeError at /test04/
Got a `TypeError` when calling `TestDefalt.objects.create()`. This may be because you have a writable field on the serializer class that is not a valid argument to `TestDefalt.objects.create()`. You may need to make the field read-only, or override the TestDefaultSerializer.create() method to handle this correctly.
...
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 782, in get_default
return self._get_default()
TypeError: genefunc() missing 2 required positional arguments: 'instance' and 'detail'
デフォルトパラメータ関数は引数をとりません。あなたのアプローチを再考してください。 –
FileFieldのデフォルト関数をupload_to関数と混同していますか? –