2

[SOLVED]

I just solved like this

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)

And request with password parameter, it was deleted successfully.


[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)

I want to add some condition when DELETE data.

Here is my models.py and 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)

For example, below data is in database.

| articleNo | content           | date                       | userkey |password|
---------------------------------------------------------------------------------
|        33 | test article      | 2018-03-11 05:00:15.428661 | a1b2c3d4 | 1234  |

When "DELETE" method request, I want to compare request's password and article's password. If it is same, data will be deleted.

Refer to document, I think I have to override def delete().

But I don't know exactly what should I do.

How can I solve this issue?

Thanks.

4
  • 1
    Add your VIEW code as well. Commented Mar 13, 2018 at 9:51
  • what view are you using? ModelViewset? APIView? Commented Mar 13, 2018 at 9:57
  • can you add your views.py as well please? Commented Mar 13, 2018 at 10:10
  • 1
    I added my views.py Commented Mar 13, 2018 at 10:36

3 Answers 3

2

Assuming you have a delete payload as below,

request_payload = {
    "password": "request's password",
    "article": some_integer_value_article_instance,
    # other data
}


and your views be like

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)
Sign up to request clarification or add additional context in comments.

9 Comments

I modified views.py as your comment, and construct payload like you. But when I request payload except password, It deleted well. All I want is request.password == article.password, then deleted article. I added python request on my post
What you mean by request.password ? I couldn't understand
request.password is Request by user(HTTP Request) and article.password is password that stored in database. So, Requested password and stored password is same, article will be deleted. That is my scenario.
So, whats the output/error you have got after applying my answer ?
There is no error. Just deleted. (without password parameter, only article number parameter)
|
0

I just solved like this

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)

And request with password parameter, it was deleted successfully.

Comments

-1

In your views.py file you can put up a condition which acts if the method is delete.

if request.method == 'DELETE':
    #your code here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.