Date difference in minutes in Python

Date difference in minutes in Python or Django?

How do I calculate the difference in days for the following datetime in Python?

2018-01-01 15:00:00
2018-01-05 15:00:00

from datetime import datetime

datetime_fmt = '%Y-%m-%d %H:%M:%S'
date1 = datetime.strptime('2010-01-01 17:31:22', datetime_fmt )
date2 = datetime.strptime('2010-01-03 17:31:22', datetime_fmt )

days_diff = (d2-d1).days
print (days_diff)
> 4

 

source:
https://docs.python.org/3/library/datetime.html#

How to Combining a Parsed time with today’s date in Python?

How to Combining a Parsed time with today’s date in Python?

datetime.combine(date, time)

Here, have to pass a datetime.date object as the first argument, and a datetime.time object as the second argument.

>>> import datetime
>>> today = datetime.date.today()
>>> time = datetime.strptime('09:30', '%H:%M').time()
>>> datetime.combine(today, time)

datetime.datetime(2018, 8, 2, 9, 30)

 

source:
https://docs.python.org/3/library/datetime.html#datetime.datetime.combine

 

SQL “LIKE” equivalent in django Filter Query

sql “LIKE” equivalent in django query:

What is the equivalent of this SQL statement in django? Now we will check this.

SELECT * FROM table_name WHERE string LIKE pattern;

For the Like Statement in Django,

** For case sensitive search

result = table.objects.filter( fieldname__contains = 'pattern' )

fieldname = Table Matching Field Name
pattern = Matching Pattern, Like “Fruct”

by using “__contains” we check if that fieldname contains the pattern.

** For case insensitive search

result = table.objects.filter( fieldname__icontains = 'pattern' )

__icontains = For case insensitive

__contains = For case sensitive

 

** Along with them, you might need these ones with similar behavior:

__startswith = For matching the start string (case sensitive)

__istartswith = For matching the start string (case insensitive)

__endswith = For matching the end string (case sensitive)

__iendswith = For matching the end string (case insensitive)

__contains = For matching the full string (case sensitive)

__icontains = For matching the full string (case insensitive)

 

How to automatically update the date field through Django Class model

How to automatically update the date field through Django Class model::

 

For the update of table field automatically from the django model we need to use

the auto_now=False and auto_now_add=False field options

date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)

From Django ::

DateField

class DateField([auto_now=Falseauto_now_add=False**options])

A date, represented in Python by a datetime.date instance. Has a few extra, optional arguments:

DateF
ield.auto_now
Automatically set the field to now every time the object is saved. Useful for “last-modified” timestamps. Note that the current date is always used; it’s not just a default value that you can override.
DateField.auto_now_add
Automatically set the field to now when the object is first created. Useful for creation of timestamps. Note that the current date is always used; it’s not just a default value that you can override.

The default form widget for this field is a TextInput. The admin adds a JavaScript calendar, and a shortcut for “Today”. Includes an additional invalid_date error message key.

Example::

from django.db import models

class Task(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

sources:
https://docs.djangoproject.com/en/1.7/ref/models/fields/#datefield

https://timonweb.com/tutorials/automatic-created-and-updated-datetime-fields-for-django-models/

 

What is Elastic search Output if running in python or django

For OUTPUT: type: http://localhost:9200/
Result:
{

  "status" : 200,

  "name" : "Skin",

  "cluster_name" : "elasticsearch",

  "version" : {

    "number" : "1.7.2",

    "build_hash" : "e43676b1385b8125d647f593f7202acbd816e8ec",

    "build_timestamp" : "2015-09-14T09:49:53Z",

    "build_snapshot" : false,

    "lucene_version" : "4.10.4"

  },  
  "tagline" : "You Know, for Search"
}