gpt4 book ai didi

python - 休息框架 'tuple' 对象没有属性 '_meta'

转载 作者:太空狗 更新时间:2023-10-30 01:45:50 26 4
gpt4 key购买 nike

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请求网址: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')

关于python - 休息框架 'tuple' 对象没有属性 '_meta',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39112252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com