[解决]
我刚刚解决了这个问题
class articleViewSet(viewsets.ModelViewSet):
queryset = article.objects.all()
serializer_class = articleSerializer
def destroy(self, request, *args, **kwargs):
password = request.data.get('password')
article_id = request.data.get('article')
instance = self.get_object()
if password == instance.password:
self.perform_destroy(instance)
return Response(status=status.HTTP_204_NO_CONTENT)
return Response(status=status.HTTP_304_NOT_MODIFIED)
使用密码参数请求,已成功删除。
[delete.py]
import requests
r = requests.delete("http://mysite/article/45/")
[views.py]
class articleViewSet(viewsets.ModelViewSet):
queryset = article.objects.all()
serializer_class = articleSerializer
def delete(self, pk, request):
method = self.request.method
article = article.objects.get(pk=pk)
if method == 'DELETE':
if request.password == article.password:
article.delete()
return Response(status=status.HTTP_200_OK)
return Response(status=status.HTTP_304_NOT_MODIFIED)
我想在删除数据时添加一些条件。
这是我的models.py和serializers.py
[serializers.py]
class articleSerializer(serializers.ModelSerializer):
userkey = serializers.CharField(write_only=True)
password = serializers.CharField(write_only=True)
class Meta:
model = article
fields = ('articleNo', 'content', 'date', 'userkey', 'password')
[models.py]
class article(models.Model):
articleNo = models.AutoField(primary_key=True)
userkey = models.CharField(max_length=50, null=False)
content = models.CharField(max_length=500, null=False)
password = models.CharField(max_length=20, null=False, default='1234')
date = models.DateTimeField(auto_now_add=True)
例如,下面的数据在数据库中。
| articleNo | content | date | userkey |password|
---------------------------------------------------------------------------------
| 33 | test article | 2018-03-11 05:00:15.428661 | a1b2c3d4 | 1234 |
何时"删除"方法请求,我想比较请求的密码和文章的密码。如果相同,则会删除数据。
参考文件,我想我必须覆盖def delete()
。
但我不知道该怎么做。
我该如何解决这个问题?
感谢。
答案 0 :(得分:1)
假设您有一个删除有效负载,如下所示,
request_payload = {
"password": "request's password",
"article": some_integer_value_article_instance,
# other data
}
你的意见就像是
class articleViewSet(viewsets.ModelViewSet):
queryset = article.objects.all()
serializer_class = articleSerializer
def delete(self, pk, request):
password = request.data.get('password')
article_id = request.data.get('article')
article_instance = article.objects.get(id=article_id)
if password == article_instance.password:
article_instance.delete()
return Response(status=status.HTTP_200_OK)
return Response(status=status.HTTP_304_NOT_MODIFIED)
答案 1 :(得分:0)
我刚刚解决了这个问题
class articleViewSet(viewsets.ModelViewSet):
queryset = article.objects.all()
serializer_class = articleSerializer
def destroy(self, request, *args, **kwargs):
password = request.data.get('password')
article_id = request.data.get('article')
instance = self.get_object()
if password == instance.password:
self.perform_destroy(instance)
return Response(status=status.HTTP_204_NO_CONTENT)
return Response(status=status.HTTP_304_NOT_MODIFIED)
使用密码参数请求,已成功删除。
答案 2 :(得分:-1)
在views.py文件中,您可以设置一个条件,该方法在删除方法时起作用。
if request.method == 'DELETE':
#your code here