关于python:restframework’tuple’对象没有属性’_meta’

restframework 'tuple' object has no attribute '_meta'

Django抛出下一个异常:

restframework"tuple"对象没有属性"_meta"

模型

1
2
3
4
5
6
7
class BDetail(models.Model):
   lat = models.FloatField(blank=True, null=True)
   lng = models.FloatField(blank=True, null=True)

   class Meta:
       # managed = False
       db_table = 'b_detail'

视图

1
2
3
4
5
6
7
from .models import BDetail
from .serializers import BDetailSerializer
from rest_framework import viewsets

class BDetailList(viewsets.ModelViewSet):
        queryset = BDetail.objects.all()
        serializer_class = BDetailSerializer

网址

1
2
3
4
5
6
7
8
9
10
from django.conf.urls import url, include
from bdetail import views
from rest_framework import routers

router = routers.DefaultRouter()
router.register(r'bdetail', views.BDetailList)

urlpatterns = [
    url(r'^', include(router.urls), name='bdetail')
]

串行器

1
2
3
4
5
6
7
from .models import BDetail
from rest_framework import serializers

class BDetailSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = BDetail,
        fields = ('lat', 'lng')

环境:

请求方法:GET请求URL:http://apix.verinmuebles.dev/v1/bdetail/

回溯:

File
"/var/www/verinmuebles/current/Env/api/local/lib/python2.7/site-packages/django/core/handlers/exception.py"
in inner
39. response = get_response(request)

File
"/var/www/verinmuebles/current/Env/api/local/lib/python2.7/site-packages/django/core/handlers/base.py"
in _get_response
187. response = self.process_exception_by_middleware(e, request)

File
"/var/www/verinmuebles/current/Env/api/local/lib/python2.7/site-packages/django/core/handlers/base.py"
in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File
"/var/www/verinmuebles/current/Env/api/local/lib/python2.7/site-packages/django/views/decorators/csrf.py"
in wrapped_view
58. return view_func(*args, **kwargs)

File
"/var/www/verinmuebles/current/Env/api/local/lib/python2.7/site-packages/rest_framework/viewsets.py"
in view
87. return self.dispatch(request, *args, **kwargs)

File
"/var/www/verinmuebles/current/Env/api/local/lib/python2.7/site-packages/rest_framework/views.py"
in dispatch
474. response = self.handle_exception(exc)

File
"/var/www/verinmuebles/current/Env/api/local/lib/python2.7/site-packages/rest_framework/views.py"
in handle_exception
434. self.raise_uncaught_exception(exc)

File
"/var/www/verinmuebles/current/Env/api/local/lib/python2.7/site-packages/rest_framework/views.py"
in dispatch
471. response = handler(request, *args, **kwargs)

File
"/var/www/verinmuebles/current/Env/api/local/lib/python2.7/site-packages/rest_framework/mixins.py"
in list
45. return self.get_paginated_response(serializer.data)

File
"/var/www/verinmuebles/current/Env/api/local/lib/python2.7/site-packages/rest_framework/serializers.py"
in data
701. ret = super(ListSerializer, self).data

File
"/var/www/verinmuebles/current/Env/api/local/lib/python2.7/site-packages/rest_framework/serializers.py"
in data
240. self._data = self.to_representation(self.instance)

File
"/var/www/verinmuebles/current/Env/api/local/lib/python2.7/site-packages/rest_framework/serializers.py"
in to_representation
619. self.child.to_representation(item) for item in iterable

File
"/var/www/verinmuebles/current/Env/api/local/lib/python2.7/site-packages/rest_framework/serializers.py"
in to_representation
460. fields = self._readable_fields

File
"/var/www/verinmuebles/current/Env/api/local/lib/python2.7/site-packages/django/utils/functional.py"
in get
35. res = instance.dict[self.name] = self.func(instance)

File
"/var/www/verinmuebles/current/Env/api/local/lib/python2.7/site-packages/rest_framework/serializers.py"
in _readable_fields
354. field for field in self.fields.values()

File
"/var/www/verinmuebles/current/Env/api/local/lib/python2.7/site-packages/rest_framework/serializers.py"
in fields
340. for key, value in self.get_fields().items():

File
"/var/www/verinmuebles/current/Env/api/local/lib/python2.7/site-packages/rest_framework/serializers.py"
in get_fields
946. info = model_meta.get_field_info(model)

File
"/var/www/verinmuebles/current/Env/api/local/lib/python2.7/site-packages/rest_framework/utils/model_meta.py"
in get_field_info
36. opts = model._meta.concrete_model._meta

Exception Type: AttributeError at /v1/bdetail/ Exception Value:
'tuple' object has no attribute '_meta'


您在BDetailSerializer序列化程序中的BDetail模型的名称后面有,。删除它,代码就可以工作了。

建议:在你的BDetailSerializer系列中继承serializers.ModelSerializer而不是serializers.HyperlinkedModelSerializer即:

1
2
3
4
class BDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = BDetail
        fields = ('lat', 'lng')


只是想增加一个可能发生这种事情的潜在案例。在使用get_or_create的情况下,请记住,此方法返回的是元组,而不仅仅是对象。

例如:

1
2
3
4
book = Book.objects.get_or_create(pk=123)
serializer = BookSerializer(book, request.data)
if serializer.is_valid():
    serializer.save() # <-- Right here you would get the same error

通过解包tuple解决了此问题:

1
2
3
4
book, created = Book.objects.get_or_create(pk=123)

# or this way if you do not need to know if it was created or not
book, _ = Book.objects.get_or_create(pk=123)