0

I created a form where an user can delete some preferences from the Database. Here is my view:

if 'button2' in request.POST:
    instance = Keys.objects.get(id=request.POST['id']) 
    form2 = DeleteKey(request.POST, instance=instance)
    if form2.is_valid():
        profile = form.save(commit=False)
        profile.key = ''
        profile.save()

        messages.success(request, f"Success")
        return HttpResponseRedirect(request.path_info)

And the form:

class DeleteKey(forms.ModelForm):

    class Meta:
        model = Keys
        fields = ()

    def save(self, commit=True):
        send = super(DeleteKey, self).save(commit=False)

        if commit:
            send.save()
        return send

This code works, the problem is that i'm not really deleting the row from my own database, i'm just setting that field to an empty string. Is there any way to completely delete the row, instead? Thanks in advance!

Jack022
  • 867
  • 6
  • 30
  • 91
  • Please fix the 3rd line in your view ```form2 = form=...``` this is clearly a typo – tomgalpin Feb 28 '20 at 13:28
  • 2
    Does this answer your question? [How to delete a record in Django models?](https://stackoverflow.com/questions/3805958/how-to-delete-a-record-in-django-models) – geckos Feb 28 '20 at 13:29
  • My bad for not making more research; yes the question was already answered, ineed. And yes, it was a typo @tomgalpin. Thank you! – Jack022 Feb 28 '20 at 13:32

0 Answers0