尝试执行Django Rest Framework外键序列化,但是我收到了这个错误,
models.py
class ProductsTbl(models.Model):
model_number = models.CharField(
max_length=255,
blank=True,
unique=True,
error_messages={
'unique': "這 model number 已經被註冊了 ."
}
)
name = models.CharField(max_length=255, blank=True, null=True)
material = models.CharField(max_length=255, blank=True, null=True)
color = models.CharField(max_length=255, blank=True, null=True)
feature = models.TextField(blank=True, null=True)
created = models.DateTimeField(editable=False)
modified = models.DateTimeField(auto_now=True)
release = models.DateTimeField(blank=True, null=True)
twtime = models.DateTimeField(blank=True, null=True)
hktime = models.DateTimeField(blank=True, null=True)
shtime = models.DateTimeField(blank=True, null=True)
jptime = models.DateTimeField(blank=True, null=True)
suggest = models.TextField(blank=True, null=True)
description = models.TextField(blank=True, null=True)
user = models.ForeignKey(User, blank=True, null=True)
useredit = models.CharField(max_length=32, blank=True, null=True)
def __unicode__(self):
return self.name
def save(self, *args, **kwargs):
''' On save, update timestamps '''
if not self.id:
self.created = timezone.now()
return super(ProductsTbl, self).save(*args, **kwargs)
def get_image_path(instance, filename):
return '/'.join(['thing_images', instance.thing.slug, filename])
class Upload(models.Model):
thing = models.ForeignKey(ProductsTbl, related_name="uploads")
image = models.ImageField(upload_to=get_image_path, verbose_name='Image')
def save(self, *args, **kwargs):
super(Upload, self).save(*args, **kwargs)
if self.image:
image = Image.open(self.image)
i_width, i_height = image.size
max_size = (640, 480)
if i_width > 1000:
image.thumbnail(max_size, Image.ANTIALIAS)
image.save(self.image.path)
API / serializers.py
from rest_framework import serializers
from ..models import *
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
UserModel = get_user_model()
class UploadSerializers(serializers.ModelSerializer):
class Meta:
model = Upload
fields = ('image',)
class ProductsTblSerializer(serializers.HyperlinkedModelSerializer):
uploads = UploadSerializers.HyperlinkedModelSerializer(many=True,read_only=True)
class Meta:
model = ProductsTbl
fields = ('model_number',
'created',
'name',
'release',
'twtime',
'hktime',
'shtime',
'jptime',
'feature',
'material',
'suggest',
'description',
'cataloggroup',
'place',
'scale',
'slug',
'user',
'uploads')
API / urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^productsTbls/$', views.ProductsTblListView.as_view(), name='productsTbls_list'),
url(r'^productsTbls/(?P<pk>\d+)/$', views.ProductsTblDetailView.as_view(), name='productsTbls_detail'),
url(r'^productsTbls/pdelete/(?P<id>[-\w]+)/$',views.api_delete_product,name='api_delete_p'),
url(r'^productsTbls/register/$', views.CreateUserView.as_view(), name='productsTbls_register'),
]
API / views.py
from rest_framework.parsers import JSONParser
from django.views.decorators.csrf import csrf_exempt
from django.forms import modelformset_factory
from django.template.defaultfilters import slugify
from rest_framework import permissions
from rest_framework.generics import CreateAPIView
from django.contrib.auth import get_user_model
from rest_framework.permissions import AllowAny
class ProductsTblListView(generics.ListCreateAPIView):
queryset = ProductsTbl.objects.order_by('-created')
serializer_class = ProductsTblSerializer
class ProductsTblDetailView(generics.RetrieveUpdateDestroyAPIView):
queryset = ProductsTbl.objects.all()
serializer_class = ProductsTblSerializer
然而,如果我改变了api/serializers.py
,如下所示
......
class ProductsTblSerializer(serializers.ModelSerializer):
uploads = UploadSerializers(many=True,read_only=True)
......
我可以看到http://127.0.0.1:8000/api/productsTbls/我的页面出来了,但没有图片链接,如何在"uploads":[]
中显示图片链接?谢谢(django rest framework 3.4)
答案 0 :(得分:1)
您已从serializers.ModelSerializer继承了您的UploadSerializer
类,而不是serializers.HyperLinkedModelSerializer
class UploadSerializers(serializers.ModelSerializer):
class Meta:
model = Upload
fields = ('image',)
因此,当您尝试将其用作
时 uploads = UploadSerializers.HyperlinkedModelSerializer(many=True,read_only=True)
它没有该属性。
如果您从超链接类继承UploadSerializers
的初始声明,它应该可以工作。
class UploadSerializers(serializers.HyperlinkedModelSerializer):
class Meta:
model = Upload
fields = ('image',)
然后你应该可以使用:
uploads = UploadSerializers(many=True,read_only=True)