我有
picture = models.ImageFIEld(null=True,blank=True,upload_to="items")
所以我创建了一个新模型:
class ItemImages(models.Model): itemImage = models.ForeignKey(Item,related_name='pictures') image = models.ImageFIEld(null=True,upload_to="items")
之后,我创建了视图集:(请注意,我不知道我在做什么)
class ItemImagesVIEwSet(vIEwsets.GenericVIEwSet): for_model = Item queryset = Item.objects.all() serializer_class = ItemImagesSerializer permission_classes = (permissions.IsAuthenticated,IsOwnerOrReadonly)
然后网址转:
router = ExtendedSimpleRouter()...( router.register(r'items',ItemVIEwSet) .register(r'itemimages',ItemImagesVIEwSet,'items-images',parents_query_lookups=['items']) # I added this line .register(r'revIEws',ItemRevIEwVIEwSet,'items-revIEw',parents_query_lookups=['content_ID']))
我的序列化器:
from rest_framework import serializersfrom django.utils.translation import ugettext_lazy as _from inventory.models import Item,ItemImagesfrom users.API.serializers import ObjectRevIEwsSerializerclass ItemSerializer(ObjectRevIEwsSerializer): class Meta: model = Item fIElds = ( 'category','ID','pictures','created','Title','url','value','user','revIEws_url','rating','description' ) read_only_fIElds = ('user','modifIEd','') def create(self,valIDated_data): valIDated_data.update({ 'user': self.context['request'].user,}) return super(ItemSerializer,self).create(valIDated_data) def valIDate(self,attrs): more_fee = attrs.get('more_fee',None) less_fee = attrs.get('less_fee',None) if not more_fee and not less_time_fee and not self.instance: msg = _('You should provIDe either a more fee or a less fee') raise serializers.ValIDationError(msg) return attrsclass ItemImagesSerializer(ObjectRevIEwsSerializer): class Meta: model = ItemImages
它在我请求/ items / 23时给出了标题中声明的错误
我知道我做错了很多但是我不知道为什么和为什么.
我是Django Rest和Django的新手,我正在玩一个现有的项目(尝试将一张图片更改为多张)
完整的错误消息:
解决方法 您使用“items-images”作为URLS的基本名称而不是默认的queryset属性名称注册“itemimages”,这意味着名称为“itemimages-detail”的网址不再存在,而您的视图集的详细网址将为“ items-images-detail“(见 Usage section of the DRF docs).ImproperlyConfigured at /API/v1/items/23/ Could not resolve URL for
hyperlinked relationship using vIEw name “itemimages-detail”. You may
have Failed to include the related model in your API,or incorrectly
configured thelookup_fIEld
attribute on this fIEld.
由于您也没有为序列化程序指定“图片”字段的字段,因此它将尝试使用默认的VIEwlink =“itemimages-detail”的默认HyperlinkRelatedFIEld序列化程序.在ItemSerializer中,您需要为具有正确的非默认vIEw_name的图片指定字段:
pictures = serializers.HyperlinkRelatedFIEld( many=True,read_only=True,vIEw_name='items-images-detail',)总结
以上是内存溢出为你收集整理的django – 无法使用视图名称“itemimages-detail”解析超链接关系的URL全部内容,希望文章能够帮你解决django – 无法使用视图名称“itemimages-detail”解析超链接关系的URL所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)