Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7527253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:04:06+00:00 2026-05-30T04:04:06+00:00

class SomeModel(models.Model): end = models.DateTimeField() def delete(self, *args, **kwargs): now = datetime.datetime.now() if self.end

  • 0
class SomeModel(models.Model):
    end = models.DateTimeField()

    def delete(self, *args, **kwargs):
        now = datetime.datetime.now()
        if self.end < now:
            return  # past events cannot be deleted

        super(SomeModel, self).delete(self, *args, **kwargs)

I’ve wrote above code in one of my models.
It’s working beautifully but having one single problem:

I’m getting a message saying, object is successfully deleted even if that model is not deleted because if the condition I put in.

Is there a way I can send a message that object is not deleted in this case?

NB: This model is for django-admin only.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:04:06+00:00Added an answer on May 30, 2026 at 4:04 am

    The delete view in the django admin does not check to see if the delete() call was successful, so if you want to override the delete method as in your question, you’ll need to override the entire ModelAdmin.delete_view method.

    If SomeModel is only used in the Django admin, another possible approach is to override the has_delete_permission method. This would remove the delete links from the change view, and disable the delete page for events in the past.

    class SomeModelAdmin(admin.ModelAdmin):
        ...
        def has_delete_permission(self, request, obj=None):
            """
            Return False for events in the past
            """
            if obj is None:
                # obj is None in the model admin changelist view
                return False
            now = datetime.datetime.now()
            if obj.end < now:
                return False # past events cannot be deleted
            else:
                return super(SomeModelAdmin, self).has_delete_permission(request, obj)
    

    The implementation above would disable the “delete selected objects” admin action, as we return False when obj is None. You should consider doing this anyway, as it calls the queryset delete method and not your overridden delete method.

    With this approach, superadmins would still be able to delete events as they have all permissions. I don’t think this approach would work if SomeModel appears in model inlines — although I see that has_delete_permission is an InlineModelAdmin option in Django 1.4.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to write some code like this: class SomeModel(models.Model): field = models.ForeignKey(SomeOtherModel) def
I have a model with a m2m relation to users: class SomeModel(models.Model): user=models.ManyToManyField(User,related_name='linked',blank=True,null=True) to
Suppose I have a model: class SomeModel(models.Model): id = models.AutoField(primary_key=True) a = models.CharField(max_length=10) b
I have the following model and TabularInline subclasses: class SomeModel(models.Model): name = models.CharField(max_length=50) class
class Tag(models.Model): name = models.CharField(maxlength=100) class Blog(models.Model): name = models.CharField(maxlength=100) tags = models.ManyToManyField(Tag) Simple
class Foo(models.Model): title = models.CharField(max_length=20) slug = models.SlugField() Is there a built-in way to
I tried doing the following class SomeModel(db.Model): prev = db.ReferenceProperty(SomeModel) next = db.ReferenceProperty(SomeModel) but
Consider this simple Model and ViewModel scenario: public class SomeModel { public virtual Company
I have a class that contains data from some model. This class has metadata
class SomeModel < ActiveRecord::Base named_scope :recent, lambda { { :conditions => ['created_at > ?',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.