9

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'

0

4 Answers 4

23

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')
2
  • 1
    Updated the answer. It is not working because you are having the , after the name of BDetail model in your BDetailSerializer serializer. Aug 23, 2016 at 23:56
  • 1
    @MoinuddinQuadri, thank you so much for your sharp eye. the ',' was my problem, it was messing the tuple structure! Right on! Jan 23, 2017 at 14:09
19

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)
1
  • 1
    useful for those like me with enough forgetfulness Jan 18, 2021 at 7:20
2

The problem is on the serializer, remove the ',' in model = BDetail, and the error will be gone. What you did there was essentially cast BDetail as a tuple.

0

Use get_or_create() method instead of get_object_or_404 () When u r creating update view and use ", _" after the object for avoiding "tuple object has no _meta".

Like this Obj = ModelClass.objects.get(pk=123) This obj will raise error if it is None instead: Use this : obj, _ = ModelClass.objects.get_or_create(pk=123) This method check if 123 exists in modelClass.pk get it or if doesn't exist 123, create one 123 in the table, save it and then get it .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.