Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django select between DB dates

Tags:

django

with _range you can do a between query

start_date = datetime.date(2005, 1, 1)
end_date = datetime.date(2005, 3, 31)
Entry.objects.filter(pub_date__range=(start_date, end_date))

You can use this if you have 2 dates and want to check if the date in het DB is between those 2 dates

In my case I have 1 date and want to check if that date is between my 2 date fields in db

class Deals(models.Model):
    #stuff
    starting = models.DateField()
    ending = models.Datefield()

How can I do a between query to check if month = '2010-01-01' is between starting and ending

Edit

I have deals in de Deals table. I want to know if there is a deal in January 2010(2010-01-01), February 2010(2010-02-01), etc

Like this

SELECT * FROM deals WHERE '2010-01-01' BETWEEN starting AND ending 
like image 695
nelsonvarela Avatar asked May 21 '12 09:05

nelsonvarela


People also ask

How do you implement a date picker in Django?

Introduction The implementation of a date picker is mostly done on the front-end. The key part of the implementation is to assure Django will receive the date input value in the correct format, and also that Django will be able to reproduce the format when rendering a form with initial data.

What is the use of ID_date in Django?

The id_date is the default ID Django generates for the form fields ( id_ + name ). To have a more generic implementation, this time we are selecting the field to initialize the component using its name instead of its id, should the user change the id prefix. This is a very beautiful and minimalist date picker.

How do I retrieve objects from the database in Django?

Django will complain if you try to assign or add an object of the wrong type. To retrieve objects from your database, construct a QuerySet via a Manager on your model class. A QuerySet represents a collection of objects from your database. It can have zero, one or many filters. Filters narrow down the query results based on the given parameters.

How do I write a SQL query in Django?

If you find yourself needing to write an SQL query that is too complex for Django’s database-mapper to handle, you can fall back on writing SQL by hand. Django has a couple of options for writing raw SQL queries; see Performing raw SQL queries. Finally, it’s important to note that the Django database layer is merely an interface to your database.


1 Answers

Deals.objects.filter(starting__gte=mydate, ending__lte=mydate)

Doc is here

like image 188
FallenAngel Avatar answered Sep 21 '22 05:09

FallenAngel