Django’s built‑in DeleteView is wired for one thing: “fetch a model instance with get_object(), call its .delete(), then redirect”.
If the thing you want to delete isn’t a Django model, you simply have to replace those two touch‑points:
- How the view finds the object.
- What the view does to remove it.
Those are encapsulated in two methods:
| Method | What the stock version does | What you should do |
|---|
| get_object() | Pulls a model instance from the ORM queryset | Fetch your external/NOSQL record (return any Python object you like) |
| delete(request, *args, **kwargs) | Calls obj.delete() then redirects | Perform the actual removal via your driver/API, then redirect |
Everything else (template rendering for the confirmation page, POST‑redirect, permission checks, etc.) you can keep exactly the same.
Minimal example
from django.urls import reverse_lazy
from django.views.generic import DeleteView
from bson import ObjectId # example: using MongoDB (pymongo)
from myproject.mongodb import db # your own helper that gives you a pymongo DB
class MongoDeleteView(DeleteView):
"""
Delete a document from the `articles` collection instead of a Django model.
"""
# model = None # not needed; just don’t set it
template_name = "articles/article_confirm_delete.html"
success_url = reverse_lazy("articles:list")
# 1) How to fetch the document shown on the confirm page
def get_object(self, queryset=None):
doc_id = self.kwargs["pk"] # <pk> captured in URL
article = db.articles.find_one({"_id": ObjectId(doc_id)})
if article is None:
raise Http404("Article not found")
return article # can be a dict, a custom class, etc.
# 2) How to actually delete it when the user presses “Yes, delete”
def delete(self, request, *args, **kwargs):
obj = self.get_object() # what you just fetched above
db.articles.delete_one({"_id": obj["_id"]})
return HttpResponseRedirect(self.get_success_url())
# (Optional) Provide context vars for the template
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx["article"] = self.object # whatever your template needs
return ctx
Why not just override .delete() on the object?
If you returned a custom Python object that does implement its own .delete() method, you could skip overriding the view’s delete(). But when you’re dealing with a plain dict or some third‑party library object, adding that method is often clunkier than overriding the view.
Quick checklist
☑️ Sub‑class DeleteView
☑️ Override get_object()
☑️ Either:
• return an object that already has .delete() implemented, or
• override delete() on the view and do the removal yourself
☑️ Keep success_url, template_name, permission mixins, etc., exactly as in a normal DeleteView
That’s all you need—no changes to Django’s internals, no hacking SingleObjectMixin, just two small overrides.