Mehady
Mehady

Reputation: 180

I am trying to implement soft delete in django

I am trying to implement soft delete in django but getting the following error:

NoReverseMatch at /admin/trip-issue-report/

Reverse for 'soft-delete-trip-issue' with arguments '(3,)' not found. 1 pattern(s) tried: ['admin/soft\-delete\-trip\-issue/$']

My code:

models.py

class TripIssue(models.Model):
    trip = models.ForeignKey(Trip, on_delete=models.CASCADE)
    issue = models.ForeignKey(Issue, on_delete=models.CASCADE)
    isSolved = models.BooleanField(blank=False, default=False)
    is_deleted = models.BooleanField(default=False)
    deleted_at = models.DateTimeField(blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def soft_delete(self):
        self.is_deleted = True
        self.deleted_at = timezone.now()
        self.save()

views.py

class TripIssueSoftDelete(DeleteView):
    model = TripIssue
    success_url = reverse_lazy('trip-issue-report')
    template_name = 'trip_issue.html'

    def delete(self, request, *args, **kwargs):
        self.object = self.get_object()
        self.object.soft_delete()
        return HttpResponseRedirect(self.get_success_url()) 

urls.py

path('soft-delete-trip-issue/', views.TripIssueSoftDelete, name="soft-delete-trip-issue"),

trip_issue.html template

          {% for trip_issue in trip_issues %}
                <tr>
                  <td>{{trip_issue.trip}}</td>
                  <td>{{trip_issue.issue}}</td>
                  <td>{{trip_issue.isSolved}}</td>
                  <td>{{trip_issue.updated_at}}</td>
                    <td><a href="{% url 'dashboard:soft-delete-trip-issue' trip_issue.id %}"><i class="fas fa-minus-circle"></i></a></td>
                </tr>
                {% endfor %}
              </tbody>

So I need your help to fix the issue and implement soft delete successfully.

Thanks in advance.

Happy Coding :)

Upvotes: 1

Views: 642

Answers (3)

Sangram Deshpande
Sangram Deshpande

Reputation: 1

  • You don't even need to write to save method in the models, just use soft_delete as a boolean field, and write filter query to get the deleted and non deleted items seperately from views.
  • You can also change the value of the soft_delete to restore it, write a seperate function to implement the same.
  • I also used the same approach as I mentioned, and it's pretty nicely working for me. You can also try this.

Upvotes: 0

Mehady
Mehady

Reputation: 180

Here is the solution I've done:

urls.py

updated the url < str:pk > and viewset [as this is class based view] with .as_view()

path('soft-delete-trip-issue/<str:pk>/', views.TripIssueSoftDelete.as_view(), name="soft-delete-trip-issue"),

Extra Tips: if you get this error __init__() takes 1 positional argument but 2 were given make sure your decorator in view is working or try to fix that.

Happy Coding :)

Upvotes: 0

SaeX
SaeX

Reputation: 18691

The error shows Reverse for 'soft-delete-trip-issue' with arguments '(3,)' not found. 1 pattern(s) tried: ['admin/soft\-delete\-trip\-issue/$']. Read closely: this points out exactly what the issue is about.

Your path() entry in urls.py doesn't allow passing an argument:

path('soft-delete-trip-issue/', views.TripIssueSoftDelete, name="soft-delete-trip-issue"),

Change to e.g.:

path('soft-delete-trip-issue/<int:pk>', views.TripIssueSoftDelete, name="soft-delete-trip-issue"),

See https://docs.djangoproject.com/en/3.1/topics/http/urls/.

Upvotes: 1

Related Questions