python flask dictionary database table - flask

I am working on an application in Python and Flask where I am creating a query from a database of 50 rows of content that I would like to use to build an HTML table on render. I will be dealing with 50 rows and 8 fields per row. What would be the best way to package this up and then render it our in the template. A sample of a row is below.
(49L, 1L, 1L, datetime.datetime(2013, 9, 25, 19, 32, 34), 996L, u'on', 78.0, 80.0, 48.0)
i'll be manipulating the datatime for a better display, but most of the other fields will come through basically unchanged.
Thanks!

Related

Django returns different results on same filters

I have a model that I run a daterange query over. For business reasons, I annotate a field in my results that represent CST timezone (data is stored in UTC by default).
The query below returns an empty queryset even though there are entries by the date range mentioned below.
MyModel.objects.annotate(cst_created_at=ExpressionWrapper(F('created_at')-timezone.timedelta(hours=6, minutes=0), output_field=DateTimeField())).filter(cst_created_at__date__range=["2022-09-01", "2022-09-01"])
If I get the values of cst_created_at (that I annotated) and created_at, which is a auto_now_add=True field, they're both the same.
result = MyModel.objects.annotate(cst_created_at=ExpressionWrapper(F(field)-timezone.timedelta(hours=6, minutes=0), output_field=DateTimeField())).first()
cst = result.cst_created_at
ca = result.created_at
print(cst)
>>>datetime.datetime(2022, 9, 1, 0, 51, 2, 310752, tzinfo=<UTC>)
print(ca)
>>>datetime.datetime(2022, 9, 1, 6, 51, 2, 310752, tzinfo=<UTC>)
cst.date() == ca.date()
>>>True
At the same time, if I query cst_created_at with yesterday's date (2022-08-31) it returns the objects created on 2022-09-01. Also, if I query created_at__date for 2022-09-01 it works as well.
I wanna know if both the dates are same why doesn't the query work properly?

How to do text based filtering on DateTimeFields using a specific template format?

I have a table that includes both CharFields and DateTimeFields displayed using django template language. The DateTimeFields are displayed in the format "Aug. 18, 2020, 6:33 p.m.". Is there an easy way to do a text based filter/query on the DateTimeFields based on that specific format? For example, searching "aug" or "Aug" or "Aug. 18" or "2020" should find that entry.
I've tried icontains, but that seems to work on the underlying format, rather than the display format. For example, searching "2020-08" finds that entry, but searching "Aug" does not.
Works:
Entry.objects.get(created__icontains='2020')
Entry.objects.get(created__icontains='2020-08')
Entry.objects.get(created__icontains='33')
Does not work:
Entry.objects.get(created__icontains='Aug')
Entry.objects.get(created__icontains='6:33')
Entry.objects.get(created__icontains='p.m')
Is there a way to search the using the specific dat time display format?
What format is the underlying DateTimeField? Is it DB dependent?
Partial answer for 2: the underlaying format seems to be UTC in the format
"2020-05-12 18:58:23.210862+00". Using icontains on any of that text works.
For clarification, the idea is to search all fields (datetime and string) with the same text. So, "Aug" should return a row that has a date field with "Aug 13" or a string field with "Augustine". Kind of like a general google search.
you have change string to datetime,
like this:date = datetime.date(2020, 8, 3) ,
here is you can use:
date:Entry.objects.filter(created__date=datetime.date(2005, 1, 1))
year:Entry.objects.filter(created__year= 2020)
month:Entry.objects.filter(created__month=8)
hour:Event.objects.filter(created__hour=6)
minute:Event.objects.filter(created__minute=29)
second:Event.objects.filter(created__second=31)
you can see more about date querysets on here:https://docs.djangoproject.com/en/3.1/ref/models/querysets/#year

Difference between range and lte/gte django filters

I am using filters for a model with following two ways:
Model.objects.filter(created_date__gte=datetime(2018, 1, 11, 00, 00, 00), created_date__lte=datetime(2018, 11, 11, 23, 59, 59), user_id=135)
And
Model.objects.filter(user_id=135, created_date__range=["2018-11-1 00:00:00", "2018-11-11 23:59:59"])
When i check the count of the both filters there is huge difference of count.
The main question is what is difference between range and gte/lte in filters ?
While using postgres SQL raw query
SELECT , count() OVER() AS full_count FROM table_name
WHERE user_id=135 AND created_date BETWEEN DATE('2018-11-01 00:00:00') AND DATE('2018-11-12 23:59:59') ORDER BY created_date;
counts are also different with django ORM

Bootstrap table pages by column values

I am using a bootstrap table where I can choose the page size by the
pageSize: 10,
pageList: [5, 10, 20, 50, 100, 200],
parameters.
What I would like to do is to get the pages not of a fixed size, but dividing all the row by the values of one column. So for example if I have a column for salaries and 13 people have a salary 3500€ and 8 people have a salary 4000€, then I'd like to have the 13 entries on 1 table-page and the other 8 entries on the other table-page. I would like to make this work particularly for dates or date ranges.
What I've understood from the documentation, is that this feature does not exist, but I hope to get some alternative solutions for my Django-run page.
I am handling a similar situation, but not with paging. I have select2 dropbox boxes where the user select the parameter(s) and then I populate the grid with those choice(s). https://select2.github.io/examples.html
Here is a sample of the code I am using the pass parameters to the server application.
$('#comptable1').bootstrapTable({
url: 'urlhere.php',
pagination: true,
sidePagination: 'server',
uniqueId: 'id',
selectItemName: 'chkID',
columns:[
{
...
}
],
queryParams: function(params){
//add paramaters to the params array as needed
params['field']=$('#findfield').val();
return params;
}
});

Format date time in Google Chart by timezone

I am using Google Charts for line graphs and tables in my application. I have my data in UTC format however while displaying I want to change the timezone to the user's choice of timezone.
For the tables, I tried using the timezone attribute for the date formatter. However it is not giving proper results. Hence I am planning to use moment.js to format the data in specific timezone. Can I use a custom formatter with data tables. The examples in the documentation do not have any details. Similarly can I format the data displayed on the x-axis of my line graph using a custom formatter. Any example would really help.
You can format the data in the DataTable by calling the setFormattedValue method of the DataTable: data.setFormattedValue(row, column, 'string formatted value');. You cannot use the format property of the axis option to change the timezone, but you can use the ticks property of the axis option to set which dates and associated labels to use:
hAxis: {
ticks: [{v: new Date(2014, 2, 1, 10), f: '03/01/2014 5AM EST'}, {v: new Date(2014, 2, 2, 17), f: '03/02/2014 12PM EST'}, {v: new Date(2014, 2, 3, 6), f: '03/03/2014 1AM EST'}]
}
It might be easier to adjust your data rather than to reformat things. As an example, to change dates in column 1 of data from GMT by offset hours, you could use this:
for (var i = 0; i < data.getNumberOfRows(); i++) {
var d = data.getValue(i, 0);
d.setHours(d.getHours + offset);
data.setValue(i, 0, d);
}