Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

python - Best way to delete a django model instance after a certain date

I am writing a little app where the user creates an event and specifies the date that event will occur. After the event date has past, I want to delete that event instance. My current attempt is throwing a function that checks if the event should expire in the event page view. I am not sure whether the expiration_check function is checking in a correct way, nor am I sure whether just having a function in the view will event work.

Here is my view and expire function:

def event_page(request, name):
    event = Event.objects.get(name=name)

    check_expiration(event)

    if request.method == "POST":
        form = GuestForm(request.POST)
        if form.is_valid():
            Guest = form.save(commit=False)
            Guest.event = event
            Guest.save()
            return redirect(event)
    else:
        form = GuestForm()
        return render(request, "event_page.html", {"form": form, "event": event, })


def check_expiration(event):
    now = datetime.datetime.now()

    if event.date < now: #if the event date has past
        event.delete()

I collect the date from the user and store it in a DateTime filed: date = models.DateField()

Let me know if any further details are needed. Any insight is appreciated, thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you're hosting your application on a UNIX platform (GNU/Linux, OSX, etc.), it's probably best to make use of cron, the generic system utility for running things periodically.

This requires implementing your expiry code as a custom management command:

  1. If you don't have any custom management commands already, create the following directory structure:

    yourapp/
      management/
         __init__.py (blank)
         commands/
           __init__.py (blank)
           expire_events.py
    
  2. In expire_events.py, create a new class along the lines of the following:

    from django.core.management.base import NoArgsCommand
    
    class Command(NoArgsCommand):
    
        help = 'Expires event objects which are out-of-date'
    
        def handle_noargs(self):
            print Event.objects.filter(date__lt=datetime.datetime.now()).delete()
    
  3. Now you should be able to run ./manage.py expire_events and have any events with expiry dates in the past deleted.

To run this at regular intervals using cron (these instructions are for GNU/Linux but may well work on other UNIX variants), run sudo crontab -e and add the following line:

*/5 * * * * /path/to/your/django/app/manage.py expire_events

(this would run the task every 5 minutes; see the crontab documentation for advice on specifying job run times)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...