2 Almost equal Context with non-empty query set: 1 works, 1 returns nothing

14 views
Skip to first unread message

Manuel Buri

unread,
Mar 18, 2021, 10:59:14 PM3/18/21
to Django users
Hi folks,
can you please help me with this.

I am trying to get the context working where I filter for the organization_id as well as the booking_time. Both queries are not empty, however, only the context without the booking_time filter works while rending in html.

Code:
@login_required
def overview_view(request):

context = {}
booking_date = None

if request.method == 'GET':
if request.is_ajax():
booking_time = request.GET.get('day')
booking_time = datetime.strptime(booking_time, "%Y-%m-%d")
booking_date = booking_time.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=pytz.utc)

THIS ONE DOES NOT WORK
context['bookings'] = Booking.objects.filter(organization_id=request.user.organization_id).filter(
booking_time=booking_date)
THESE DO WORK:
context['bookings'] = Booking.objects.filter(organization_id=request.user.organization_id)
return render(request, 'overview/overview.html', context)

return render(request, 'overview/overview.html', context)


Thank you so much for your help.

Manuel

Arisophy

unread,
Mar 19, 2021, 12:37:49 AM3/19/21
to django...@googlegroups.com
Hi, Manuei

I think ,
At first, you shoukd check the value of "request.user.organization_id" .
Next, try to use Q if the value is valid.

context['bookings'] = Booking.objects.filters(Q((organization_id=request.user.organization_id) & Q(booking_time=booking_date))

2021年3月19日(金) 7:59 Manuel Buri <manue...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8afb07f4-10b2-4145-ab23-6fe9bb41c599n%40googlegroups.com.

Arisophy

unread,
Mar 19, 2021, 2:59:41 AM3/19/21
to django...@googlegroups.com
Hi Manuel

Oops!
.filter(booking_time=booking_date) doesn't work

Did you check the query and DB data?
Are booking_time's time of  DB data just 00:00:00?

If another time, you have to use
 booking_time >= target_date AND  booking_time < next_date_of_target

Arisophy

2021年3月19日(金) 9:36 Arisophy <aris...@is-jpn.com>:

Arisophy

unread,
Mar 19, 2021, 3:09:54 AM3/19/21
to django...@googlegroups.com
use this

https://docs.djangoproject.com/ja/3.1/ref/models/querysets/#date

.filter(booking_time__date=booking_date) 


2021年3月19日(金) 11:58 Arisophy <aris...@is-jpn.com>:

Manuel Buri

unread,
Mar 19, 2021, 9:46:02 AM3/19/21
to Django users
thank you so much Arisophy.

I am not able to fix the problem with your suggestions.
Please be aware that I am not having any empty query sets while filtering etc.
So it must be a problem with getting the context to HTML, however, it is super weird, because as long as I am not filtering for a date my HTML renders the context perfectly.

Does that help to provide further help?

Thank you so much.
Manuel
Reply all
Reply to author
Forward
0 new messages