Home > Net >  Django Store date only or query with date only
Django Store date only or query with date only

Time:09-17

My requirement is, every year the post should be found by user with a specific date range. in end_date and state_date

So i want to store only date or i have to query by date only instead of year

But i found no way to do that, coz, it is storing year too

class Post(models.Model):
    title = models.CharField(max_length=255, null=True)
    start_date = models.DateField(null=True, blank=True)
    end_date = models.DateField(null=True, blank=True)

I have tried to query like this:

post = Post.objects.filter(
    start_date__gte='09-16',
    end_date__lte='10-01'
)

but above solution doesnt work well, any have ideas how can i do it? storing date only except year or query by date only?, so that it works every year without any hassle

CodePudding user response:

You can try this:

post = Post.objects.filter(
    start_date__day__gte=16, start_date__month__gte=9,
    end_date__day__lte=10, end_date__month__lte=1
)

Basically, you use day, month and year with DateField and DateTimeField the same way you use eq, lt etx.

  • Related