Django中模型字段的命名约定

时间:2018-12-21 12:13:58

标签: django django-models

在Django中,模型字段的命名约定是什么?我的问题是出现错误

django.core.exceptions.ImproperlyConfigured: Field name `tutor1` is not valid for model `ClientEntry`

在字段名称中使用整数是错误的吗?我可以用什么名字来命名它,例如tutor1,tutor2,tutor3?我也尝试了tutor_1,但遇到了同样的错误。

注意:tutor1 postgres ArrayField

编辑:这是回溯

Traceback (most recent call last):
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/rest_framework/viewsets.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/rest_framework/views.py", line 483, in dispatch
    response = self.handle_exception(exc)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/rest_framework/views.py", line 443, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/rest_framework/views.py", line 480, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/rest_framework/mixins.py", line 20, in create
    serializer.is_valid(raise_exception=True)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/rest_framework/serializers.py", line 236, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/rest_framework/serializers.py", line 434, in run_validation
    value = self.to_internal_value(data)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/rest_framework/serializers.py", line 482, in to_internal_value
    fields = self._writable_fields
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/rest_framework/serializers.py", line 370, in _writable_fields
    field for field in self.fields.values() if not field.read_only
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/rest_framework/serializers.py", line 363, in fields
    for key, value in self.get_fields().items():
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/rest_framework/serializers.py", line 1045, in get_fields
    source, info, model, depth
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/rest_framework/serializers.py", line 1190, in build_field
    return self.build_unknown_field(field_name, model_class)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/rest_framework/serializers.py", line 1302, in build_unknown_field
    (field_name, model_class.__name__)
django.core.exceptions.ImproperlyConfigured: Field name `amount_paid_tutor1` is not valid for model `ClientEntry`.

这是相关字段

amount_paid_to_tutor1 = ArrayField(
  models.IntegerField(
    blank=True
  ),
  blank=True,
  null=True
)

Serializer基本上是所有字段和视图,

class ClientEntryViewSet(viewsets.ModelViewSet):
  queryset = ClientEntry.objects.all()
  serializer_class = ClientEntrySerializer

1 个答案:

答案 0 :(得分:0)

似乎您的模型上有amount_paid_to_tutor1字段,但是由于amount_paid_tutor1而发生了错误。找到amount_paid_tutor1,然后更改为amount_paid_to_tutor1