logo
down
shadow

Django queryset: filter DateTimeField if datetime.now() is greater than field - 24 hours


Django queryset: filter DateTimeField if datetime.now() is greater than field - 24 hours

By : Sandra Smith Larson
Date : November 20 2020, 11:01 PM
seems to work fine I can't understand how do this. , If you rearrange
code :
datetime.now() >= start_datetime - 24 hours
start_datetime <= datetime.now() + 1 day
from datetime import timedelta

Event.objects.filter(start_datetime__lte=datetime.now() + timedelta(days=1))


Share : facebook icon twitter icon
PHP DateTime(): Display a length of time greater than 24 hours but not as days if greater than 24 hours

PHP DateTime(): Display a length of time greater than 24 hours but not as days if greater than 24 hours


By : Christopher
Date : March 29 2020, 07:55 AM
Hope this helps Since you already have the length in seconds, you can just calculate it:
code :
function timeLength($sec)
{
    $s=$sec % 60;
    $m=(($sec-$s) / 60) % 60;
    $h=floor($sec / 3600);
    return $h.":".substr("0".$m,-2).":".substr("0".$s,-2);
}
echo timeLength(6534293); //outputs "1815:04:53"
function dtLength($sec)
{
    $t=new DateTime("@".$sec);
    $r=new DateTime("@0");
    $i=$t->diff($r);
    $h=intval($i->format("%a"))*24+intval($i->format("%H"));
    return $h.":".$i->format("%I:%S");
}
echo dtLength(6534293); //outputs "1815:04:53" too
class DTInterval
{
    private $sec=0;
    function __construct($s){$this->sec=$sec;}
    function formet($format)
    {
        /*$h=...,$m=...,$s=...*/
        $rst=str_replace("H",$h,$format);/*etc.etc.*/
        return $rst;
    }
}
Django objects queryset filter by matching datetime

Django objects queryset filter by matching datetime


By : Alessandro Privitera
Date : March 29 2020, 07:55 AM
like below fixes the issue I have a model with a field name 'timestamp' that is a datetime field, and when I print them in python manage.py shell I get:
code :
MyModelName.objects.filter(timestamp__gte=(datetime.datetime.now() - datetime.timedelta(seconds=30)))
django queryset filter datetimefield

django queryset filter datetimefield


By : Velmurugan
Date : March 29 2020, 07:55 AM
it fixes the issue Since you are using a datetime field you want to make your query’s using datetime objects. so first order the queryset from earliest to latest then filter out the events tat arn't occurring today.
code :
from datetime import datetime, timedelta, time

today = datetime.now().date()
tomorrow = today + timedelta(1)
today_start = datetime.combine(today, time())
today_end = datetime.combine(tomorrow, time())

queryset = KleedkamerIndeling.objects.order_by('-gametimedate').filter(gametimedate__gte=today_start).filter(gametimedate__lt=today_end)
if queryset.count() > 10:
  queryset = KleedkamerIndeling.objects.filter(gametimedate__gte=datetime.now())[:10]
Using MongoEngine for Django, how do I filter the QuerySet by the size of a list and greater than a value?

Using MongoEngine for Django, how do I filter the QuerySet by the size of a list and greater than a value?


By : user3857425
Date : March 29 2020, 07:55 AM
around this issue Well I think that you need to find a work around for this as mongoengine doesn't support your query. What you can do is add another field say 'options_length' and store the length of options field in this. Then you can query using '__gt > 2'. The additional cost is that you need to override your model save function to update length on every save. Also you need to update the existing records for this.
You can also read this question
isinstance(datetime.datetime, DateTimeField.value) always returns false for Django DateTimeField

isinstance(datetime.datetime, DateTimeField.value) always returns false for Django DateTimeField


By : smochi
Date : March 29 2020, 07:55 AM
I wish did fix the issue. It seems you have missed assigning the naive datetime object back to the value datetime object.
It should be
code :
value = value.replace(tzinfo=None) # assign 'naive' datetime object back to 'value'
value.replace(tzinfo=None) # will just return 'naive' datetime object
import pytz
import datetime
....
for i, row in enumerate(list(Record.objects.all())):

    for j,field in enumerate(list(Record._meta.get_fields())):
            value = getattr(row,field.name)
            if isinstance(value, datetime.datetime):
                value = value.replace(tzinfo=None) # assign value as 'naive' datetime object 
                worksheet.write(i, j, value) # will now use 'naive' datetime object to write

            else:
                worksheet.write(i, j, value)
Related Posts Related Posts :
  • django translation with format() does not work
  • how to import models to oscar.partner app's strategy module correctly?
  • Passing group name of a user in context and accessing in template in django
  • Django : size of floatfields
  • Django - Return a response and then update the DB
  • Django: loop over query and return newest related object
  • Programming a fall-back for in-memory python object retrieval
  • Django Rest Framework: how to set a custom name form non_field_errors?
  • Is it better to combine model subclasses and have un-used fields, or create concrete subclasses?
  • I cannot connect django with postgresql
  • Django filter custom look up
  • Django, bundle together count queries?
  • How to index just those models\rows, where some_field=some_value?
  • after login redirect to another page
  • Empty CharField with null=True returns NoneType Django 1.8
  • Django Database Settings : Can we keep default database as empty dict? [Django 1.8]
  • Accepting input in Django ListView
  • How to handle Django connection loss with database gracefully?
  • Trying to filter based upon a value in another table
  • Django url template with a filter
  • How I can create a PointField using an api?
  • Django Rest Framework 'RelatedManager' object has no attribute
  • Django PostgreSQL JSONField db_index error
  • Problems deploying GeoDjango application on Heroku
  • Redirecting to an external website from django view
  • Adding a Django site breaks my virtual hosts on CentOS 7
  • Django-Allauth, Multiple login redirect url
  • django field is required validators
  • How to render templates dynamically for a static user choice
  • Django ORM: Get verbose_name of field via filter lookup
  • How to count number of entries, annotate it and select latest with Django ORM
  • How to generate list of response messages in Django REST Swagger?
  • 'str' object is not callable - SuccessMessageMixin
  • Repeater objects appearing on all detail views - django
  • django admin why my ForeignKey do'nt display?
  • Django Sort by backward foreign key
  • Changing default field validator in Django ModelForm
  • Django Wagtail adding classname to structblock
  • Django CMS - Aldryn Bootstrap broken grid system
  • Extend the list of validators in a subclass model
  • allauth with crispyform : signup not working
  • Having an edit form and a detail view on the same page
  • Django - Remove object from database after day
  • Django & Postgresql annotate datetime field to localtime
  • Update object value from function based view
  • How to easily add Django urls?
  • Django: get related objects of related objects and pass to template
  • Django Query same week last year
  • how to remove reloading the page?
  • Django custom user error
  • Change default GeoModelAdmin to OSMGeoAdmin
  • How to actualize Django Template with get_context_data
  • FileUploadParser doesn't get the file name
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com