View / Query Set not showing with filter

26 views
Skip to first unread message

Manuel Buri

unread,
Mar 19, 2021, 1:00:46 PM3/19/21
to Django users
Hi,

I am having this query set in my view:
context['bookings']=Booking.objects.filter( Q(organization_id=request.user.organization_id), Q(booking_time__range= (start_date, end_date)))

It produces a non-empty query set!
However, I am NOT able to display it in my template.

If I get rid of Q(booking_time__range= (start_date, end_date) and only do:
context['bookings']=Booking.objects.filter( Q(organization_id=request.user.organization_id))
then it is also non-empty AND it is displaying it in my template.

What am I missing here?

Thank you for your help.

Manuel

Nagaraju Singothu

unread,
Mar 19, 2021, 6:09:59 PM3/19/21
to django...@googlegroups.com
Dear connectors,

    Please let me know, what is double tap in python?. Any tutorials available in YouTube. 

--
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/76f85c72-19f9-4ad3-a39b-01dc60aa6da5n%40googlegroups.com.

Manuel Buri

unread,
Mar 19, 2021, 6:28:56 PM3/19/21
to django...@googlegroups.com
Hi Nagaraju

May I ask if your question is related to my post?

Thanks a lot,
Manuel

sent from my phone.
sorry for typos.

You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/2DC9EY75yuM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMyGuAYeFYOEnpk-MkHE51enE%2BEgGwtBrOtnTTtbAHCwC9wL0w%40mail.gmail.com.

Thomas Lockhart

unread,
Mar 19, 2021, 7:21:38 PM3/19/21
to django...@googlegroups.com
Almost certainly start_date and end_date are not what you expect.

Do the types match? In any case print them out and see what you are actually getting.

hth

- Tom

Manuel Buri

unread,
Mar 19, 2021, 8:24:59 PM3/19/21
to django...@googlegroups.com, tlockh...@gmail.com
Hi Tom, thanks for getting back to me.

As said, it is super weird:
both start_date and end_date are both datetime.datetime.

furthermore the queryset is as said non-empty:
<QuerySet [{'id': 9, 'account_id': 4, 'organization_id': 11, 'office_id': 1, 'booking_time': datetime.datetime(2021, 3, 20, 0, 0, tzinfo=<UTC>), 'location': 'H'}, {'id': 10, 'account_id': 4, 'organization_id': 11, 'office_id': 1, 'booking_time': datetime.datetime(2021, 3, 21, 0, 0, tzinfo=<UTC>), 'location': 'H'}]>

However, it is not working, and then if I remove the __range (see initial question): I have this queryset (while the above queryset is obviously included in the below one):
<QuerySet [{'id': 4, 'account_id': 12, 'organization_id': 11, 'office_id': 1, 'booking_time': datetime.datetime(2021, 3, 24, 23, 0, tzinfo=<UTC>), 'location': 'H'}, {'id': 2, 'account_id': 12, 'organization_id': 11, 'office_id': 1, 'booking_time': datetime.datetime(2021, 3, 18, 23, 0, tzinfo=<UTC>), 'location': 'H'}, {'id': 3, 'account_id': 2, 'organization_id': 11, 'office_id': 1, 'booking_time': datetime.datetime(2021, 3, 18, 0, 0, tzinfo=<UTC>), 'location': 'H'}, {'id': 8, 'account_id': 3, 'organization_id': 11, 'office_id': 1, 'booking_time': datetime.datetime(2021, 3, 18, 0, 0, tzinfo=<UTC>), 'location': 'H'}, {'id': 9, 'account_id': 4, 'organization_id': 11, 'office_id': 1, 'booking_time': datetime.datetime(2021, 3, 20, 0, 0, tzinfo=<UTC>), 'location': 'H'}, {'id': 10, 'account_id': 4, 'organization_id': 11, 'office_id': 1, 'booking_time': datetime.datetime(2021, 3, 21, 0, 0, tzinfo=<UTC>), 'location': 'H'}]>

I am really clueless why this is happening.
Do I need to think about something specific in the template when filtering for dates?


sent from my phone.
sorry for typos.

You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/2DC9EY75yuM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/83B24C06-8DA4-4F38-9384-FF4C19DBE262%40gmail.com.

Anornymous u

unread,
Mar 19, 2021, 8:28:07 PM3/19/21
to django...@googlegroups.com
What is the name of the field, this part that reads range

--

Manuel Buri

unread,
Mar 19, 2021, 8:32:52 PM3/19/21
to django...@googlegroups.com
the field is this (copied from my Model):
booking_time        = models.DateTimeField(default=timezone.now)

sent from my phone.
sorry for typos.
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/2DC9EY75yuM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMXTB%3DcX1xFLcEhHS_abDCvmr7npG8-TKLH29WYyFxhqfKOfWQ%40mail.gmail.com.

Anornymous u

unread,
Mar 19, 2021, 9:05:58 PM3/19/21
to django...@googlegroups.com
Tell us exactly what you are looking for from your query

On Fri, Mar 19, 2021, 16:01 Manuel Buri <manue...@gmail.com> wrote:
--

Anornymous u

unread,
Mar 19, 2021, 9:07:00 PM3/19/21
to django...@googlegroups.com
I mean what you want to fetch and the conditions

On Fri, Mar 19, 2021, 16:01 Manuel Buri <manue...@gmail.com> wrote:
--

Manuel Buri

unread,
Mar 19, 2021, 9:12:45 PM3/19/21
to django...@googlegroups.com
I want to fetch all bookings for the users organization_id between start_date and end_date.
I am achieving this via this queryset, however, as soon as I add the second filter (bold), it is not rendered in the template anymore and I, therefore, do not see it in my HTML file.
context['bookings']=Booking.objects.filter( Q(organization_id=request.user.organization_id), Q(booking_time__range= (start_date, end_date)))

Thank you so much for your help.
Best wishes,

Manuel





You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/2DC9EY75yuM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMXTB%3DcsfYRqGPbhXncaHNTjKqVhgskST8BDEAY-2fYonemfdg%40mail.gmail.com.

Héctor Alonso Lozada Echezuría

unread,
Mar 19, 2021, 9:14:55 PM3/19/21
to django...@googlegroups.com

Héctor Alonso Lozada Echezuría

unread,
Mar 19, 2021, 9:17:38 PM3/19/21
to django...@googlegroups.com
I think you should reconsider the query

context['bookings']=Booking.objects.filter(Q(organization_id=request.user.organization_id), Q(booking_time__date__gte=start_date), Q(booking_time__date__lte=end_date)))

Anornymous u

unread,
Mar 19, 2021, 9:19:49 PM3/19/21
to django...@googlegroups.com

Thomas Lockhart

unread,
Mar 19, 2021, 9:51:08 PM3/19/21
to django...@googlegroups.com
Why use Q? afaict simple filter syntax should get you what you want.

Booking.objects.filter(organization_id=request.user.organization_id, booking_time__range= (start_date, end_date))

Not sure if this will get you closer to the query you expect.

You might also find it useful to use "./manage.py shell" to test the queries by hand. Import a few models, put in “11” for the organization_id, and see what happens.

- Tom
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACx7KOQEEWWNu6hQwMXdEP_nZ6%2Bex9HZ9KEiVubrvd89%3DTP%3DuQ%40mail.gmail.com.

Manuel Buri

unread,
Mar 19, 2021, 10:17:33 PM3/19/21
to django...@googlegroups.com
@anorny...@gmail.com and ima...@gmail.com
your suggestion is unfortunately not working too

@All:
So as said my queryset is not empty, meaning that the query works.
However, the results is not in the rendered HTML file.
This would imply that with my template is something wrong, don't you think?

{% for office in office.all %}
<small>{{ office.office_name }}</small>
{% for booking in bookings %}
<div class="client-item">
<div class="client-text">
<p class="name">{{ booking.account.first_name }} {{ booking.account.last_name }}</p>
{% if booking.get_location_display == 'House' %}
<p class="sub-title">🏢 {{ booking.get_location_display }}</p>
{% elif booking.get_location_display == 'Home' %}
<p class="sub-title">🏠 {{ booking.get_location_display }}</p>
{% else %}
<p class="sub-title">🤔 {{ booking.get_location_display }}</p>
{% endif %}
</div>
</div>
{% endfor %}
{% endfor %}

What do you think?

Thank you so much for your help !!!!!

Best wishes,

Manuel

Héctor Alonso Lozada Echezuría

unread,
Mar 19, 2021, 10:22:50 PM3/19/21
to django...@googlegroups.com
Can you share your "Booking" model and the view (function or class) that renders the template?, and I beg you, please use pastebin.com for share your code :D

Manuel Buri

unread,
Mar 19, 2021, 10:31:09 PM3/19/21
to django...@googlegroups.com
Booking Model
from django.db import models
from django.utils import timezone
from users.models import Account
from organization.models import Organization, Office


class Booking(models.Model):
    LOCATION_OPTIONS = (
            ('H', 'Home Office'),
            ('O', 'Office'),
            ('N', 'Not indicated'),
    )
    account             = models.ForeignKey(Account, on_delete=models.CASCADE)
    organization        = models.ForeignKey(Organization, on_delete=models.CASCADE)
    office              = models.ForeignKey(Office, on_delete=models.CASCADE)
    booking_time        = models.DateTimeField(default=timezone.now)
    location            = models.CharField(max_length=1, choices=LOCATION_OPTIONS)
    class Meta:
            unique_together = ('account', 'booking_time',)

View
def overview_view(request):

    context = {}

    if request.method == 'GET':
        start_date = None
        end_date = None
        if request.is_ajax():
            booking_times = request.GET.get('day')
            start_date = datetime.strptime(booking_times, "%Y-%m-%d")
            end_date = start_date + timedelta(days=4)
            start_date = start_date.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=pytz.utc)
            end_date = end_date.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=pytz.utc)

        context['bookings']=Booking.objects.filter( Q(organization_id=request.user.organization_id), Q(booking_time__range = (start_date, end_date))
        context['office'] = Office.objects.filter(organization_id__exact=request.user.organization_id)
       
        return render(request, 'overview/overview.html', context)
return render(request, 'overview/overview.html', context)

haha sorry for not using pastebin before :D

Thank you so much.

Best wishes,

Manuel




Arisophy

unread,
Mar 20, 2021, 12:21:28 AM3/20/21
to django...@googlegroups.com
Hi Manuelf

I understand what you say.
Now, I think It's not the problem of Querym , too.
The problem is that field data is not displayed in HTML.

You use the wrong variable name "office" in Template.

try this.

(View)
context['office'] = ....
context['offices']  = ...

(Template)

× {% for office in office.all %}
 ↓
{% for office in offices %}


regards

Arisophy

2021年3月20日(土) 7:31 Manuel Buri <manue...@gmail.com>:

Héctor Alonso Lozada Echezuría

unread,
Mar 20, 2021, 3:49:58 AM3/20/21
to django...@googlegroups.com
my friend,

I'm checking your code, and I can't continue without seeing the template and how you make the request.

However I will try to make a dissection of the code to evaluate what happens, so I will forward your code to you in the pastebin link with some notes: https://pastebin.com/QbxqcTrh

Note: if you are doing an ajax request you should not return a template,  https://docs.djangoproject.com/en/3.1/ref/request-response/#jsonresponse-objects

So I would return a template with the context: https://pastebin.com/BYre4Dig
and so I would return it in an ajax request: https://pastebin.com/CJ2YA8gz


Reply all
Reply to author
Forward
0 new messages