Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

restframework 'tuple' object has no attribute '_meta'

Django throws the next exception:

restframework 'tuple' object has no attribute '_meta'

Model

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'

View

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

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

urls

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

serializers

from .models import BDetail
from rest_framework import serializers

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

Environment:

Request Method: GET Request URL: http://apiix.verinmuebles.dev/v1/bdetail/

Traceback:

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'

like image 426
cristian camilo cedeño gallego Avatar asked Aug 23 '16 23:08

cristian camilo cedeño gallego


2 Answers

You are having the , after the name of BDetail model in BDetailSerializer serializer. Remove that and your code will work.

Suggestion: Inherit serializers.ModelSerializer in your BDetailSerializer serializer instead of serializers.HyperlinkedModelSerializer i.e. :

class BDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = BDetail
        fields = ('lat', 'lng')
like image 121
Moinuddin Quadri Avatar answered Sep 28 '22 06:09

Moinuddin Quadri


Just wanted to add a potential case where such a thing might happen. In case you are using get_or_create, keep in mind that this method returns a tuple, and not just the object.

As an example:

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 

The problem is fixed by unpacking the tuple:

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)
like image 30
TGO Avatar answered Sep 28 '22 04:09

TGO