Open Designer023 opened 8 years ago
I'm sorry you can't get it to work, could you give a little example code of what you're trying to do and what you're stuck on?
Thank you for taking a look. I am trying to get the image field on my PortfolioItem model. When I add the stdimage_serializer.fields there is no change in the output of my api. It just outputs the original. I tried a few variations and different names for the field in the serializer but no luck. Any ideas would be great. Thanks
#models.py
from stdimage.models import StdImageField
from stdimage.utils import UploadToUUID, UploadToClassNameDir, UploadToAutoSlug, UploadToAutoSlugClassNameDir
from django.db import models
from django.contrib.sitemaps import Sitemap
from django.core.urlresolvers import reverse
from datetime import datetime
from django.utils import timezone
IMAGE_SIZES = { 'fit1600': (1600, 1600), 'fit800': (800, 800), '1600': (1600, 900, True),'320': (320, 320, True),'800': (800, 600, True), '800sq': (800, 800, True), }
TYPE_CHOICES = (
...
)
CATEGORY_CHOICES = (
...
)
...
Other models
...
class PortfolioItem(models.Model):
title = models.CharField(max_length=1000)
slug = models.CharField(max_length=1000)
subtitle = models.CharField(max_length=1000, blank=True)
active = models.BooleanField(default=None)
client = models.CharField(max_length=1000)
#Image field
image = StdImageField(upload_to=UploadToUUID(path='uploads/portfolio/item'), blank=True, default=None, variations=IMAGE_SIZES)
client = models.CharField(max_length=1000, blank=True)
client_link_text = models.CharField(max_length=1000, blank=True)
client_link = models.CharField(max_length=1000, blank=True)
width = models.IntegerField(default=0, blank=True)
height = models.IntegerField(default=0, blank=True)
#popup page for the vid
content_page = models.CharField(max_length=1000, blank=True)
short_description = models.TextField(blank=True)
description = models.TextField(blank=True)
date_of_project = models.DateTimeField('date published')
category = models.ForeignKey(Category)
item_type = models.CharField(max_length=100, choices=TYPE_CHOICES)
contact_email = models.CharField(max_length=1000, blank=True)
app_store_link = models.CharField(max_length=1000, blank=True)
class Meta:
ordering = ['-date_of_project']
def __unicode__(self):
return self.title
def get_absolute_url(self):
return reverse('portfolio.views.project', args=[str(self.slug)])
#serializers.py
from rest_framework import serializers
from rest_framework import viewsets
from stdimage_serializer.fields import StdImageField
from .models import PortfolioItem, Category
...
Other serializers and viewsets
...
class PortfolioSerializer(serializers.ModelSerializer):
category_name = PortfolioCategorySerializer(source='category', read_only=True)
image = StdImageField()
class Meta:
model = PortfolioItem
fields = ('id', 'url', 'title', 'slug', 'subtitle', 'content_page',
'client', 'image', 'client_link_text',
'client_link', 'short_description', 'description',
'date_of_project', 'contact_email', 'app_store_link',
'category_name', 'item_type',)
lookup_field = 'slug'
extra_kwargs = {
'url': {'lookup_field': 'slug'}
}
def get_url(self, obj):
return reverse('portfolio.views.index', args=[str(self.slug)])
class PortfolioViewSet(viewsets.ModelViewSet):
queryset = PortfolioItem.objects.filter(active=True).order_by('-date_of_project')
serializer_class = PortfolioSerializer
lookup_field = 'slug'
#output for image field
"image": "http://localhost:8000/media/uploads/portfolio/item/3bf8c99687c645788f924bb349784adc.jpg",
Same issue here. The serialized field just contains the default, original image URL. Looks like you need to implement def to_representation(self, obj)
instead of to_native
per http://www.django-rest-framework.org/api-guide/fields/#examples
@Designer023 did you try to put StdImageField
to the serializer?
https://github.com/glemmaPaul/django-stdimage-serializer#usage
Example:
from stdimage_serializer.fields import StdImageField
class Serializer(BaseSerializer):
image = StdImageField()
Could you expand your example to show a full example model using stdimage and the full serialiser please? It might be obvious to most people but I just can't seem to get it to work at all so not sure if I'm putting things in the wrong place.
Thanks