我正在使用DjangoRestFrameowork构建RESTful API,并且在我的模型中尝试使用choices
时遇到错误。访问api端点时,我收到错误KeyError at /jobs/ u'job_type'
。我的模特有
JOB_CHOICES = (
('p', 'Paint'),
('pw', 'Pressure Wash'),
('h', 'Hourly'),
)
class Job(models.Model):
created = models.DateTimeField(auto_now_add=True)
budget = models.IntegerField()
job_type = models.CharField(choices=JOB_CHOICES,
default='p',
max_length=100)
我的序列化器看起来像这样:
class JobSerializer(serializers.ModelSerializer):
class Meta:
model = Job
fields = ('pk',
'budget',
'created',
'job_type',)
我无法弄清楚这出错的地方。他们在Django-Rest-Framework教程中做了类似的事情,我无法弄清楚我的不同之处。任何帮助将不胜感激!
这是完整的错误:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/jobs/
Django Version: 1.7.1
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main',
'rest_framework',
'corsheaders')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware')
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
57. return view_func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/generic/base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/rest_framework/views.py" in dispatch
452. response = self.handle_exception(exc)
File "/Library/Python/2.7/site-packages/rest_framework/views.py" in dispatch
449. response = handler(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/rest_framework/generics.py" in get
241. return self.list(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/rest_framework/mixins.py" in list
47. return Response(serializer.data)
File "/Library/Python/2.7/site-packages/rest_framework/serializers.py" in data
618. ret = super(ListSerializer, self).data
File "/Library/Python/2.7/site-packages/rest_framework/serializers.py" in data
213. self._data = self.to_representation(self.instance)
File "/Library/Python/2.7/site-packages/rest_framework/serializers.py" in to_representation
568. self.child.to_representation(item) for item in iterable
File "/Library/Python/2.7/site-packages/rest_framework/serializers.py" in to_representation
435. ret[field.field_name] = field.to_representation(attribute)
File "/Library/Python/2.7/site-packages/rest_framework/fields.py" in to_representation
1041. return self.choice_strings_to_values[six.text_type(value)]
Exception Type: KeyError at /jobs/
Exception Value: u'job_type'
答案 0 :(得分:0)
Meta类应该是序列化程序的内部类,而不是单独的类。试试这个:
class JobSerializer(serializers.ModelSerializer):
class Meta:
model = Job
fields = ('pk',
'budget',
'created',
'job_type',
)