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
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.
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.
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.
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.
Deals.objects.filter(starting__gte=mydate, ending__lte=mydate)
Doc is here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With