restframework 'tuple' 对象没有属性“_meta”

restframework 'tuple' object has no attribute '_meta'

Django 抛出下一个异常:

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

型号

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'

查看

from .models import BDetail
from .serializers import BDetailSerializer
from rest_framework import viewsets

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

网址

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')
]

序列化器

from .models import BDetail
from rest_framework import serializers

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

环境:

请求方法:GET 请求 URL:http://apiix.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 即:

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

只是想添加一个可能发生这种事情的潜在案例。如果您使用 get_or_create,请记住此方法 returns 是一个元组,而不仅仅是对象。

举个例子:

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 

问题已通过解包元组解决:

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)

问题出在序列化程序上,删除model = BDetail,中的',',错误就会消失。您在那里所做的基本上是将 BDetail 转换为元组。

使用get_or_create()方法代替get_object_or_404() 当您创建更新视图并在对象后使用“,_”以避免"tuple object has no _meta"。

像这样 对象 = ModelClass.objects.get(pk=123) 如果此对象为 None,则会引发错误 反而: 用这个 : obj, _ = ModelClass.objects.get_or_create(pk=123) 此方法检查modelClass.pk中是否存在123获取,如果123不存在,则在table中创建一个123,保存后获取。