[英]how to get id of current object
how to get current id instead of static id (7) to add image to flat如何获取当前 id 而不是 static id (7) 以将图像添加到平面
''' '''
class CreateFlat(CreateAPIView):
serializer_class = CreateFlat
queryset = Flat.objects.all()
permission_classes = [AllowAny]
def post(self, request, *args, **kwargs):
print(request.data)
my_img = request.data['id_image']
ima = Images.objects.get(id=my_img)
print(self.id)
print(self)
flat = Flat.objects.get(id=7)
flat.images.add(ima);
serializer = FlatSerializer(flat, many=True)
return Response("done")
''' '''
There are so many errors but I think I can make you clear有很多错误,但我想我可以让你清楚
First of all I would like to tell you about your view in general:首先,我想大致谈谈您的看法:
CreateAPIView
it using CreateModelMixin
you should work with create
method, not post, but it does not really matter ok.CreateAPIView
它使用CreateModelMixin
你应该使用create
方法,而不是发布,但这并不重要。CreateAPIView
there is only post method so you can remove queryset
attributeCreateAPIView
中只有 post 方法,因此您可以删除queryset
属性create
method in serializer in this casecreate
方法And finally about your question.最后关于你的问题。 if your path url looks like this
flat/<int:pk>/add-images
then try this如果你的路径 url 看起来像这样
flat/<int:pk>/add-images
然后试试这个
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
id = self.kwargs[lookup_url_kwarg]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.