python - Django view objects filter with timezone.now().date or timezone.now().time-> expected string or bytes-like object -


hi have django 11 project, model like

class event(models.model):     name = models.charfield(max_length=100, unique=true)     title = models.charfield(max_length=100)     info = models.charfield(max_length=100)     image = models.imagefield(upload_to='events/%y/%m/%d')     start_date = models.datefield(default=timezone.now)     start_time = models.timefield(default=timezone.now)     stop_date = models.datefield(default=timezone.now)     stop_time = models.timefield(default=timezone.now)      place = models.foreignkey('places.place', on_delete=models.cascade)     company = models.foreignkey('companies.company', on_delete=models.cascade) 

and view like

def place_website(request, place_id):      place_template = get_template('room.html')      place_obj = place.objects.filter(id=place_id)      # filter event obejts requested place, filtered , next events     place_event_now = event.objects.filter(place=place_id, start_date=timezone.now().date, stop_date__gte=timezone.now().date)      place_events_next = event.objects.filter(place=place_id, start_date=timezone.now(), stop_date__gte=timezone.now()).order_by('start_time')      place_context = {         'place_obj': place_obj,         'place_event_now': place_event_now,         'place_events_next': place_events_next,     }      return httpresponse(place_template.render(place_context)) 

the thing want manage pass template list of filtered event objects based on time.

lets pick line

place_event_now = event.objects.filter(place=place_id, start_date=timezone.now().date, stop_date__gte=timezone.now().date) 

it couse error "expected string or bytes-like object" when remove ".date" "timezone.now()" error disappear (then filter nothing) want compare date date , time time.

how ?

this approach filter objects in view rather in template proper?

###### update ########

its werid because after correction have no error queryset not filtered properly, 2 first parameter filtering ok , 2 causing empty queryset.

place_event_now = event.objects.filter(place=place_id, start_date=timezone.now().strftime('%y-%m-%d'), start_time__lte=timezone.now().strftime('%h:%m:%s'), stop_time__gte=timezone.now().strftime('%h:%m:%s')) 

i database time saved in format h:m:s , timezone.now().time() has different format modified filter .strftime didnt help, wont limit "place_event_now" queryset particular object/objects come true condition start_time < currenttime < stop_time.

another case place_event_next

place_events_next = event.objects.filter(place=place_id, start_date=timezone.now().strftime('%y-%m-%d'), stop_date__gte=timezone.now().strftime('%y-%m-%d'), start_time__gt=timezone.now().strftime('%h:%m:%s')).order_by('start_time') 

event when filter objects start_time greater timezone.now() still in queryset.

am doing wrong ?

may need call date date() replace

filter(place=place_id, start_date=timezone.now().date, stop_date__gte=timezone.now().date) #                                                  ^^^                                 ^^^ 

to

filter(place=place_id, start_date=timezone.now().date(), stop_date__gte=timezone.now().date()) #                                                  ^^^                                 ^^^ 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -