I'm using django-mptt to create a tree-like structure for my Section model. Unfortunately, when I go to serialize it with drf-writable-nested, I get an error. This error only occurs when the url field is added to the serializer.
Also, when I remove uuid from the custom lookup field, the error is just replaced with pk.
I found this: https://stackoverflow.com/a/55173445/9137820, but I'm not accessing any of the data directly, so I'm not sure how that could be the issue.
Code:
# models.py
class Section(MPTTModel, TimeStampedModel):
uuid = models.UUIDField(default=uuid_lib.uuid4, editable=False)
name = models.CharField(max_length=255, unique=True)
objects = TreeManager()
parent = TreeForeignKey('self', related_name='section_children', on_delete=models.CASCADE, null=True, blank=True)
# serializers.py
class SectionSerializer(UniqueFieldsMixin, WritableNestedModelSerializer):
children = serializers.ListField(source='get_children', child=RecursiveField())
class Meta:
model = Section
fields = ['url', 'uuid', 'name', 'children']
extra_kwargs = {
'url': {'lookup_field': 'uuid'},
}
# views.py
class SectionDetail(generics.RetrieveUpdateDestroyAPIView, viewsets.GenericViewSet):
queryset = Section.objects.all()
serializer_class = SectionSerializer
lookup_field = 'uuid'
Traceback:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/usr/local/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.9/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/rest_framework/viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/rest_framework/mixins.py", line 75, in update
return Response(serializer.data)
File "/usr/local/lib/python3.9/site-packages/rest_framework/serializers.py", line 548, in data
ret = super().data
File "/usr/local/lib/python3.9/site-packages/rest_framework/serializers.py", line 246, in data
self._data = self.to_representation(self.instance)
File "/usr/local/lib/python3.9/site-packages/rest_framework/serializers.py", line 515, in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "/usr/local/lib/python3.9/site-packages/rest_framework/fields.py", line 1661, in to_representation
return [self.child.to_representation(item) if item is not None else None for item in data]
File "/usr/local/lib/python3.9/site-packages/rest_framework/fields.py", line 1661, in <listcomp>
return [self.child.to_representation(item) if item is not None else None for item in data]
File "/usr/local/lib/python3.9/site-packages/rest_framework/serializers.py", line 515, in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "/usr/local/lib/python3.9/site-packages/rest_framework/relations.py", line 399, in to_representation
url = self.get_url(value, self.view_name, request, format)
File "/usr/local/lib/python3.9/site-packages/rest_framework/relations.py", line 335, in get_url
lookup_value = getattr(obj, self.lookup_field)
Exception Type: AttributeError at /api/v1/sections/3b15cbda-f61e-4a00-89fb-817beed10b14/
Exception Value: 'collections.OrderedDict' object has no attribute 'uuid'
I did some digging and this is what I can find. Dictionaries and OrderedDicts don't save keys and values as class attributes. So you cant retrieve them using getattr(). You need to request a method or attribute like dict.keys() or dict.values() from the OrderedDict. You can't use a key name.
See this example:
from collections import OrderedDict
g = OrderedDict()
g['test'] = 1
g = getattr(g, 'values')
print(list(g())[0])
That works fine but if you changed
g = getattr(g, 'values')
to
g = getattr(g, 'test')
It produces the same error you're experiencing
Related
I am trying to get a user's list with an AJAX request and DRF. But get this error:
Field 'id' expected a number but got 'Henry'.
I'd be grateful for any assistance.
AJAX:
const showUserLists = function(map){
let userName = "Henry";
$.ajax({
type: 'GET',
url: '/api/userlist/',
data: {
'username': userName
},
success: function (data) {
data.forEach(item => {
console.log(item.list_name)
$("#userLists").append("<li class=userlist data-name=\"" + item.list_name + "\">" + item.list_name + "</li>")
})
}
});
};
urls.py:
router = DefaultRouter() #need help understanding router register
router.register('userlist', views.UserListViewSet, basename= 'userlist')
views.py
#this shows all lists for a user
class UserListViewSet(viewsets.ModelViewSet):
serializer_class = UserListSerializer
def get_queryset(self):
name = self.request.GET.get('username', None)
return UserList.objects.filter(user=name)
Serializer:
class UserListSerializer(serializers.ModelSerializer): #this is what we worked on on October 1
class Meta:
model = UserList
fields = ['id', 'user', 'list_name']
class VenueListSerializer(serializers.ModelSerializer):
created = serializers.ReadOnlyField()
class Meta:
model = VenueList
fields = ['id', 'title']
Relevant model:
class UserList(models.Model):
list_name = models.CharField(max_length=255)
user = models.ForeignKey(User, on_delete=models.CASCADE) #is this okay?
def __str__(self):
return self.list_name
Traceback
Traceback (most recent call last):
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 1774, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'Henry'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/core/handlers/base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/rest_framework/viewsets.py", line 114, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/rest_framework/views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/rest_framework/views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
raise exc
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/rest_framework/views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/rest_framework/mixins.py", line 38, in list
queryset = self.filter_queryset(self.get_queryset())
File "/Users/x/Desktop/Coding/anybody/anybody1/api/views.py", line 33, in get_queryset
return UserList.objects.filter(user=name)
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/query.py", line 942, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/query.py", line 962, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, *args, **kwargs)
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/query.py", line 969, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1358, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1380, in _add_q
split_subq=split_subq, check_filterable=check_filterable,
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1319, in build_filter
condition = self.build_lookup(lookups, col, value)
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1165, in build_lookup
lookup = lookup_class(lhs, rhs)
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/lookups.py", line 24, in __init__
self.rhs = self.get_prep_lookup()
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/fields/related_lookups.py", line 115, in get_prep_lookup
self.rhs = target_field.get_prep_value(self.rhs)
File "/Users/x/Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 1778, in get_prep_value
) from e
ValueError: Field 'id' expected a number but got 'Henry'.
Your user is a ForeignKey to the User model, so if you filter on this, it expects the primary key of the User, not the username.
You can however filter on the username of the related User with:
class UserListViewSet(viewsets.ModelViewSet):
serializer_class = UserListSerializer
def get_queryset(self):
name = self.request.GET.get('username', None)
return UserList.objects.filter(user__username=name)
Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.
i am trying to convert my existing project to the DRF . However im facing the error of :
: Object of type TransitionApproval is not JSON serializable
TransitionApproval object comees from a package called django-river. Here is my code:
class ProjectDetailSerializer(serializers.ModelSerializer):
requirements = CustomerRequirementSerializer(many=True)
transitionApproval = serializers.SerializerMethodField('get_transition_approval')
class Meta:
model = Project
fields = '__all__'
depth = 2
def get_transition_approval(self,project):
transitions = TransitionApproval.objects.filter(object_id=project.pk).filter(workflow__field_name='project_status')
print(transitions)
return transitions
My console prints :
<CTEQuerySet [<TransitionApproval: TransitionApproval object (1)>, <TransitionApproval: TransitionApproval object (2)>, <TransitionApproval: TransitionApproval object (3)>]>
Is there a good way to solve this? I have tried doing this :
class ProjectDetailSerializer(serializers.ModelSerializer):
requirements = CustomerRequirementSerializer(many=True)
transitionApproval = TransitionSerializer(many=True)
class Meta:
model = Project
fields = '__all__'
depth = 2
but it seems that 'transitionApproval' is not an attribute of Project. Im not sure how to resolve this.
Here is my detail page view
class SalesProjectDetailView(RetrieveAPIView):
queryset = SalesProject.objects.all()
serializer_class = SalesProjectDetailSerializer
edits:
I have taken the advise to pass the queryset through a serializer before returning it .
class TransitionApprovalSerializer(serializers.ModelSerializer):
class Meta:
model = TransitionApproval
fields = '__all__'
class ProjectDetailSerializer(serializers.ModelSerializer):
transitionApproval = serializers.SerializerMethodField('get_transition_approval')
class Meta:
model = Project
fields = '__all__'
depth = 2
def get_transition_approval(self,project):
transitions = TransitionApproval.objects.filter(object_id=project.pk).filter(workflow__field_name='project_status')
return TransitionApprovalSerializer(transitions).data
That however returns me an error of :
Traceback (most recent call last):
File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-
packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\views.py", line 476, in raise_uncaught_exception
raise exc
File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\generics.py", line 208, in get
return self.retrieve(request, *args, **kwargs)
File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\mixins.py", line 56, in retrieve
return Response(serializer.data)
File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\serializers.py", line 562, in data
ret = super().data
File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\serializers.py", line 260, in data
self._data = self.to_representation(self.instance)
File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\serializers.py", line 529, in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\fields.py", line 1905, in to_representation
return method(value)
File "C:\Users\dream\Desktop\crmReact\backend\backend\sales\api\serializers.py", line 205, in get_transition_approval
return TransitionApprovalSerializer(transitions).data
File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\serializers.py", line 562, in data
ret = super().data
File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\serializers.py", line 260, in data
self._data = self.to_representation(self.instance)
File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\serializers.py", line 516, in to_representation
attribute = field.get_attribute(instance)
File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\fields.py", line 487, in get_attribute
raise type(exc)(msg)
AttributeError: Got AttributeError when attempting to get a value for field `object_id` on serializer `TransitionApprovalSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `CTEQuerySet` instance.
Original exception text was: 'CTEQuerySet' object has no attribute 'object_id'.
object_id is a field of the TransitionApproval object , but i think the serializer is taking the queryset as the object itself
You can get this error, because your get_transition_approval method returns queryset TransitionApproval.Querysets couldn't serialize to json directly. You must return serialized data from this method. You can create another serializer and serialize your data with this serializer like that:
class TransitionApprovalSerializer(serializers.ModelSerializer):#your new serializer
class Meta:
model = TransitionApproval
fields = [...]
class ProjectDetailSerializer(serializers.ModelSerializer):
....
def get_transition_approval(self,project):
transitions = TransitionApproval.objects.filter(object_id=project.pk).filter(workflow__field_name='project_status')
# you can serialize your data here
return TransitionApprovalSerializer(transitions).data
I am trying to restify my backend using django-rest-gis, in such a way that POST request adds data to database and GET request lists down the data. However, I am getting following error:-
Internal Server Error: /api/
Traceback (most recent call last):
File "/home/vineet/env-tsl/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/vineet/env-tsl/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/vineet/env-tsl/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/vineet/env-tsl/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/vineet/env-tsl/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/generics.py", line 241, in get
return self.list(request, *args, **kwargs)
File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/mixins.py", line 48, in list
return Response(serializer.data)
File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework_gis/serializers.py", line 20, in data
return super(ListSerializer, self).data
File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/serializers.py", line 262, in data
self._data = self.to_representation(self.instance)
File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework_gis/serializers.py", line 28, in to_representation
("features", super(GeoFeatureModelListSerializer, self).to_representation(data))
File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/serializers.py", line 686, in to_representation
self.child.to_representation(item) for item in iterable
File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/serializers.py", line 686, in <listcomp>
self.child.to_representation(item) for item in iterable
File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework_gis/serializers.py", line 100, in to_representation
fields = list(self.fields.values())
File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/serializers.py", line 363, in fields
for key, value in self.get_fields().items():
File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/serializers.py", line 1031, in get_fields
field_names, declared_fields, extra_kwargs
File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/serializers.py", line 1378, in get_uniqueness_extra_kwargs
field_names, declared_fields, extra_kwargs
File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/serializers.py", line 1450, in _get_model_fields
if field_name in declared_fields:
TypeError: unhashable type: 'list'
[25/May/2019 04:48:29] "GET /api/ HTTP/1.1" 500 147185
I have followed many answers on stackoverflow but could only get that it comes when you have a mutable type as key in dictionary
models.py
from django.contrib.gis.db import models
# Create your models here.
class test1(models.Model):
date = models.DateTimeField(auto_now_add=True, null=True, blank=True)
location = models.PointField(blank=False)
plg = models.PolygonField(srid=4326, geography=True, null=True, blank=True)
city = models.CharField(max_length=50, blank=False)
state = models.CharField(max_length=50, blank=False)
def __str__(self):
return "%s" % (self.state)
serializers.py
from rest_framework_gis.serializers import GeoFeatureModelSerializer
from .models import test1
class test1Serializer(GeoFeatureModelSerializer):
class Meta:
model = test1
geo_field = ['location','plg']
auto_bbox = True
id_field = False
fields = ('city','state')
views.py
from .models import test1
from .serializers import test1Serializer
from rest_framework.generics import ListCreateAPIView
class test1SerializerCreate(ListCreateAPIView):
serializer_class = test1Serializer
queryset = test1.objects.all()
GeoFeatureModelSerializer only takes one field and it doesn't take the list, check with one field and it should work. Use Rest for multiple fields and you can parse in views. Or you can make a new GeometrySerializerMethodField field and return two geometry in that
geo_field cannot be list.
It can be this way:
class test1Serializer(GeoFeatureModelSerializer):
m_geo_field = GeometrySerializerMethodField()
def get_m_geo_field(self, obj):
return # do whatever you like
class Meta:
model = Location
geo_field = 'm_geo_field'
I have few django models:
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), unique=True)
phone = models.CharField(
_('phone number'), max_length=16,
unique=True, blank=True, null=True,
default=None, validators=[phone_validator]
)
...
class UserExtension(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
class Meta:
abstract = True
class Worker(UserExtension):
# some fields
class Administrator(UserExtension):
# some fields
View:
class AdministratorListView(ListAPIView):
permission_classes = (AllowAny,)
queryset = Administrator.objects.all()
serializer_class = serializers.AdministratorSerializer
And serializer:
class AdministratorSerializer(serializers.ModelSerializer):
class Meta:
model = Administrator
fields = ('network', 'email')
extra_kwargs = {'email': {'source': 'user.email'}}
But this is not working. Following traceback tell about field does not exist for my model:
Traceback (most recent call last):
File "/home/vagrant/v_env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/vagrant/v_env/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/vagrant/v_env/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/vagrant/v_env/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/vagrant/v_env/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/vagrant/v_env/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "/home/vagrant/v_env/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/vagrant/v_env/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
File "/home/vagrant/v_env/lib/python3.6/site-packages/rest_framework/generics.py", line 201, in get
return self.list(request, *args, **kwargs)
File "/home/vagrant/v_env/lib/python3.6/site-packages/rest_framework/mixins.py", line 48, in list
return Response(serializer.data)
File "/home/vagrant/v_env/lib/python3.6/site-packages/rest_framework/serializers.py", line 765, in data
ret = super(ListSerializer, self).data
File "/home/vagrant/v_env/lib/python3.6/site-packages/rest_framework/serializers.py", line 262, in data
self._data = self.to_representation(self.instance)
File "/home/vagrant/v_env/lib/python3.6/site-packages/rest_framework/serializers.py", line 683, in to_representation
self.child.to_representation(item) for item in iterable
File "/home/vagrant/v_env/lib/python3.6/site-packages/rest_framework/serializers.py", line 683, in <listcomp>
self.child.to_representation(item) for item in iterable
File "/home/vagrant/v_env/lib/python3.6/site-packages/rest_framework/serializers.py", line 510, in to_representation
fields = self._readable_fields
File "/home/vagrant/v_env/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/vagrant/v_env/lib/python3.6/site-packages/rest_framework/serializers.py", line 376, in _readable_fields
field for field in self.fields.values()
File "/home/vagrant/v_env/lib/python3.6/site-packages/rest_framework/serializers.py", line 363, in fields
for key, value in self.get_fields().items():
File "/home/vagrant/v_env/lib/python3.6/site-packages/rest_framework/serializers.py", line 1047, in get_fields
source, info, model, depth
File "/home/vagrant/v_env/lib/python3.6/site-packages/rest_framework/serializers.py", line 1192, in build_field
return self.build_unknown_field(field_name, model_class)
File "/home/vagrant/v_env/lib/python3.6/site-packages/rest_framework/serializers.py", line 1304, in build_unknown_field
(field_name, model_class.__name__)
django.core.exceptions.ImproperlyConfigured: Field name `user.email` is not valid for model `Administrator`.
I looked at the source code and realized that the value from 'source' is passed to build_field as field_name argument and rest-framework simply cannot find this field ('user.email') in my model (Administrator)
part of get_fields method
build_field method
Problem:
My serializer uses the wrong Model for serialization. I expected that the serializer uses my ProfileInfo model but it does not. It uses the User model. They have an OneToOneRelationship. I do not know why because I defined in the meta that the serializer should use the ProfileInfo model.
So when I try to serialize the username with -> username=serializers.Field(source='user.username') Django shows this error:
'User' object has no attribute 'user'.
Can you please explain to me why the wrong model is used ? Thank you for your help.
Serializers:
class ProfileInfoSerializer(serializers.ModelSerializer):
image = serializers.SerializerMethodField()
socialMediaLinks = serializers.SerializerMethodField("get_social_media_links")
username = serializers.Field(source='user.username')
class Meta:
model = ProfileInfo
fields = ['username', 'description', 'image', 'socialMediaLinks']
def get_image(self, obj):
request = self.context.get('request')
photo_url = obj.image.url
return request.build_absolute_uri(photo_url)
class UserInfoSerializer(serializers.ModelSerializer):
user = ProfileInfoSerializer()
wingman = ProfileInfoSerializer()
clan = ClanSerializer()
class Meta:
model = ProfileInfo
fields = ['user', 'clan', 'wingman']
Model:
class ProfileInfo(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
wingman = models.OneToOneField(User, on_delete=None, related_name="wingman", null=True)
clan = models.ForeignKey(Clan, on_delete=None)
image = models.ImageField(upload_to="", default="", null=True)
description = models.CharField(max_length=5000, null=True)
Api:
class ProfileInfoApi(APIView):
def get(self, request, id):
profileInfo = ProfileInfo.objects.get(pk=id)
serializer = UserInfoSerializer(profileInfo, context={'request': request})
return Response(serializer.data)
Traceback:
Internal Server Error: /api/profileInfo/1/
Traceback (most recent call last):
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\views.py", line 483, in dispatch
response = self.handle_exception(exc)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\views.py", line 443, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\views.py", line 480, in dispatch
response = handler(request, *args, **kwargs)
File "C:\_sources\Gamingplattform\Backend\gamingplattform\profileInfo\api.py", line 18, in get
return Response(serializer.data)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\serializers.py", line 560, in data
ret = super(Serializer, self).data
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\serializers.py", line 262, in data
self._data = self.to_representation(self.instance)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\serializers.py", line 527, in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\fields.py", line 570, in to_representation
field_name=self.field_name,
NotImplementedError: Field.to_representation() must be implemented for field username. If you do not need to support write operations you probably want to subclass `ReadOnlyField` instead.
Internal Server Error: /api/profileInfo/getFriends/1/
Traceback (most recent call last):
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\fields.py", line 441, in get_attribute
return get_attribute(instance, self.source_attrs)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\fields.py", line 100, in get_attribute
instance = getattr(instance, attr)
AttributeError: 'User' object has no attribute 'user'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\views.py", line 483, in dispatch
response = self.handle_exception(exc)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\views.py", line 443, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\views.py", line 480, in dispatch
response = handler(request, *args, **kwargs)
File "C:\_sources\Gamingplattform\Backend\gamingplattform\profileInfo\api.py", line 36, in get
return Response(serializer.data)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\serializers.py", line 765, in data
ret = super(ListSerializer, self).data
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\serializers.py", line 262, in data
self._data = self.to_representation(self.instance)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\serializers.py", line 683, in to_representation
self.child.to_representation(item) for item in iterable
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\serializers.py", line 683, in <listcomp>
self.child.to_representation(item) for item in iterable
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\serializers.py", line 527, in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\serializers.py", line 514, in to_representation
attribute = field.get_attribute(instance)
File "C:\Users\Aaron Visang\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\fields.py", line 462, in get_attribute
raise type(exc)(msg)
AttributeError: Got AttributeError when attempting to get a value for field `username` on serializer `ProfileInfoSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `User` instance.
Original exception text was: 'User' object has no attribute 'user'.
You are trying to serialize ProfileInfo instances with UserInfoSerializer serializer, which is not possible. So
Change
serializer = UserInfoSerializer(profileInfo, context={'request': request})
to
serializer = ProfileInfoSerializer(profileInfo, context={'request': request})
Hence your view be like,
class ProfileInfoApi(APIView):
def get(self, request, id):
profileInfo = ProfileInfo.objects.get(pk=id)
serializer = ProfileInfoSerializer(profileInfo, context={'request': request})
return Response(serializer.data)
UPDATE-1It's NotImplementedError,You shouldn't use serializer.Field class unless you need to inherrit. So change
username = serializers.Field(source='user.username')
to
username = serializers.CharField(source='user.username')<br>
Apart from this update, I would like to inform that, you are defined one line as , socialMediaLinks = serializers.SerializerMethodField("get_social_media_links") But, there is no method named get_social_media_links. It will raise some error, sureSo the ProfileInfoSerializer should be as below
class ProfileInfoSerializer(serializers.ModelSerializer):
image = serializers.SerializerMethodField()
socialMediaLinks = serializers.SerializerMethodField("get_social_media_links")
username = serializers.CharField(source='user.username')
class Meta:
model = ProfileInfo
fields = ['username', 'description', 'image', 'socialMediaLinks']
def get_image(self, obj):
request = self.context.get('request')
photo_url = obj.image.url
return request.build_absolute_uri(photo_url)
def get_social_media_links(self, model):
return "some data"