We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I have these model:
class Produto(models.Model): slug = models.SlugField(_('Slug'), max_length=255, unique=True, blank=True) usuario = models.ForeignKey(SoOfertasUser, blank=True, null=True) status = models.CharField(u'Status', max_length=1, default='A', choices=Ativo_Inativo_CHOICE) dthr_cadastro = models.DateTimeField(_('Dt/Hr Cadastro'), auto_now_add=True) categoria = models.ForeignKey(Categoria) subcategoria = models.ForeignKey(SubCategoria, blank=True, null=True) descricao = models.CharField(_('Descrição'), max_length=100) info_complementares = RichTextField(_('Informações Complementares do Produto'), blank=True, null=True) foto = models.ImageField(_('Foto'), upload_to=upload_to, blank=True, null=True) foto_thumb = ImageSpecField(source='foto', processors=[SmartResize(300, 300)], format='PNG', options={'quality': 60})
and here is my Serializer and ViewSet:
class ProdutoSerializer(serializers.ModelSerializer): class Meta: model = Produto fields = ( 'slug', 'status', 'categoria', 'subcategoria', 'descricao', 'info_complementares', 'foto', 'foto_thumb' ) def create(self, validated_data): return Produto.objects.create(**validated_data) class ProdutoViewSet(viewsets.ModelViewSet): lookup_field = 'descricao' queryset = Produto.objects.filter(status='A') serializer_class = ProdutoSerializer def get_permissions(self): if self.request.method in permissions.SAFE_METHODS: return (permissions.AllowAny(),) if self.request.method == 'POST': return (permissions.AllowAny(),) # return (permissions.IsAuthenticated(), IsAccountOwner(),) return (permissions.IsAuthenticated(),) def list(self, request): queryset = Produto.objects.filter(status='A') serializer = ProdutoSerializer(queryset, many=True) return Response(serializer.data) def create(self, request): serializer = self.serializer_class(data=request.data) if serializer.is_valid(): Produto.objects.create(**serializer.validated_data) return Response(serializer.validated_data, status=status.HTTP_201_CREATED) return Response({ 'status': 'Bad request', 'message': 'Object could not be created with received data.' }, status=status.HTTP_400_BAD_REQUEST)
But, when I put foto_thumb my ImageKit field, show me these error:
foto_thumb
TypeError at /api/v1/produto/ b'\x89PNG\r\n' is not JSON serializable
Here is my traceback:
Environment: Request Method: GET Request URL: http://127.0.0.1:8000/api/v1/produto/ Django Version: 1.8.5 Python Version: 3.5.0 Installed Applications: ('django_extensions', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sites', 'allauth', 'allauth.account', 'allauth.socialaccount', 'allauth.socialaccount.providers.facebook', 'rest_framework', 'django_filters', 'bootstrap3', 'crispy_forms', 'floppyforms', 'sendgrid', 'imagekit', 'storages', 'ckeditor', 'accounts', 'core', 'pointlib', 'cadastro', 'assinatura', 'desktop', 'debug_toolbar') Installed Middleware: ('django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.security.SecurityMiddleware', 'debug_toolbar.middleware.DebugToolbarMiddleware') Traceback: File "/Users/fellipeh/.virtualenvs/soofertas/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response 164. response = response.render() File "/Users/fellipeh/.virtualenvs/soofertas/lib/python3.5/site-packages/django/template/response.py" in render 158. self.content = self.rendered_content File "/Users/fellipeh/.virtualenvs/soofertas/lib/python3.5/site-packages/rest_framework/response.py" in rendered_content 71. ret = renderer.render(self.data, media_type, context) File "/Users/fellipeh/.virtualenvs/soofertas/lib/python3.5/site-packages/rest_framework/renderers.py" in render 669. context = self.get_context(data, accepted_media_type, renderer_context) File "/Users/fellipeh/.virtualenvs/soofertas/lib/python3.5/site-packages/rest_framework/renderers.py" in get_context 632. 'content': self.get_content(renderer, data, accepted_media_type, renderer_context), File "/Users/fellipeh/.virtualenvs/soofertas/lib/python3.5/site-packages/rest_framework/renderers.py" in get_content 406. content = renderer.render(data, accepted_media_type, renderer_context) File "/Users/fellipeh/.virtualenvs/soofertas/lib/python3.5/site-packages/rest_framework/renderers.py" in render 103. separators=separators File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py" in dumps 237. **kw).encode(obj) File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py" in encode 201. chunks = list(chunks) File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py" in _iterencode 427. yield from _iterencode_list(o, _current_indent_level) File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py" in _iterencode_list 324. yield from chunks File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py" in _iterencode_dict 403. yield from chunks File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py" in _iterencode 437. yield from _iterencode(o, _current_indent_level) File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py" in _iterencode 427. yield from _iterencode_list(o, _current_indent_level) File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py" in _iterencode_list 324. yield from chunks File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py" in _iterencode 436. o = _default(o) File "/Users/fellipeh/.virtualenvs/soofertas/lib/python3.5/site-packages/rest_framework/utils/encoders.py" in default 64. return super(JSONEncoder, self).default(obj) File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py" in default 180. raise TypeError(repr(o) + " is not JSON serializable") Exception Type: TypeError at /api/v1/produto/ Exception Value: b'\x89PNG\r\n' is not JSON serializable
How can I provide these field using restframework?
Regards,
The text was updated successfully, but these errors were encountered:
Hi fellipeh. Did you manage to resolve this? I'm getting the same issue?
Sorry, something went wrong.
@Rosco77 I also have a similar issue. Specifying a field directly in the serializer as an ImageField works fine for me:
ImageField
thumbnail = serializers.ImageField(read_only=True)
The read_only=True is important.
read_only=True
No branches or pull requests
I have these model:
and here is my Serializer and ViewSet:
But, when I put
foto_thumb
my ImageKit field, show me these error:Here is my traceback:
How can I provide these field using restframework?
Regards,
The text was updated successfully, but these errors were encountered: