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 1045865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:03:10+00:00 2026-05-16T16:03:10+00:00

I have a model MyModel that has a field expiration_datetime. Every time a user

  • 0

I have a model MyModel that has a field expiration_datetime.

Every time a user retrieves an instance of MyModel I need to first
check if it has expired or not. If it has expired, than I need to
increment some counter, update others, and then extend the
expiration_datetime to some time in the future.

So the view would do something like:

if object.expiration_datetime < datetime.datetime.now(): 
    object.counter = F('counter') + 1 
    object.expiration_datetime = F('expiration_datetime') + datetime.timedelta(days=1) 
    object.save() 

There’s a race condition in the code above. Say thread 1 checks and
finds that the current instance has expired, it proceeds to increment
the counter and extend the expiration datetime. But before it could do
so, thread 2 is scheduled and does the same. By the time thread 1
finally finishes, counter has been incremented twice and
expration_datetime has been extended twice.

This looks like it should be a pretty common issue. What is the most efficient way to handle it? Ideally I’d like to be able to handle it within Django in a database-portable way.

  • 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-16T16:03:11+00:00Added an answer on May 16, 2026 at 4:03 pm

    This may be a good use case for optimistic locking. There’s a few ways to do it:

    • You can have a version number, and you run your UPDATE queries so that it always includes the version number in the WHERE clause, and then check if any rows were changed.
    • Include every value of the record (before the changes you made) in the WHERE clause, that way you can be sure that the record you are saving is exactly the same as when you read it.

    How to implement optimistic locking in Django? Check out this question: Django: How can I protect against concurrent modification of data base entries.

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

Sidebar

Related Questions

I have a Django form that uses an integer field to lookup a model
I'm looking to narrow a query set for a form field that has a
I'm doing this query: SomeObject.objects.annotate(something=Avg('something')).order_by(something).all() I normally have an aggregate field in my model
I have a Rails application that lists information about local services. My objectives for
I have a database table with a certain field which should be impossible to
I'm using the table class that auto-generates a table for me from an array
I have a Registaration page with the following fields Email, Confirm Email, Password and
I want to execute a random .py file, say foo.py on the myproject/myapp folder
I'm somewhat new to Python, Django, and I'd like some advice on how to
Is it possible to override the html naming of fields in TabularInline admin forms

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.