如何在用字典列表而不是查询集填充后过滤 django-tables2?
How to filter django-tables2 after populating it with list of dicts rather than a query set?
我有一个从 MongoDB 中获取的字典列表。然后我在 views.py 中填充 django-tables2 table 并在我的模板 index.html[= 中呈现它70=]
在views.py
from django.shortcuts import render
from .tables import generateTable
from django_tables2 import RequestConfig
import pymongo
import pandas as pd
import json
client = pymongo.MongoClient("<mongodb url>")
db = client["stocks"]
col = db["company_stocks"]
the_rows = col.find({}, {'_id': False})
pandas_dataframe = pd.DataFrame(list(the_rows), columns=['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Name'])
json_fomatted = pandas_dataframe.to_json(orient='records')
data = []
data = json.loads(json_fomatted)
def index(request):
table = generateTable(data)
RequestConfig(request).configure(table)
context = {'stocks_table': table }
return render(request, "home_app/index.html", context)
tables.py
import django_tables2 as tables
from .models import Stocks
class generateTable(tables.Table):
Date = tables.Column()
Open = tables.Column()
High = tables.Column()
Low = tables.Column()
Close = tables.Column()
Volume = tables.Column()
Name = tables.Column()
urls.py
urlpatterns = [
# Uncomment the next line to enable the admin:
path('admin/', admin.site.urls),
url(r'^$', home_app.views.index, name='index'),
url(r'^home$', home_app.views.index, name='home'),
]
index.html
{% load render_table from django_tables2 %}
{% render_table stocks_table 'django_tables2/bootstrap.html' %}
Result is this
现在我正在尝试使用 django-filter 过滤 table 但运气不好
我发现的是如何过滤使用查询集而不是字典列表填充的数据
我的过滤尝试:
views.py
...
from .models import Stocks
...
def index(request):
table = generateTable(data)
RequestConfig(request).configure(table)
#with this line
myFilter = stockFilter(request.GET, queryset=Stocks.objects.all())
context = {'stocks_table': table, 'myFilter': myFilter }
return render(request, "home_app/index.html", context)
filters.py
import django_filters
from .models import Stocks
class stockFilter(django_filters.FilterSet):
class Meta:
model = Stocks
fields = '__all__'
models.py
from django.db import models
class Stocks(models.Model):
open = models.IntegerField(
max_length = 128)
high = models.IntegerField(
max_length = 128)
low = models.IntegerField(
max_length = 128)
close = models.IntegerField(
max_length = 128)
volume = models.IntegerField(
max_length = 128)
name = models.CharField(
max_length = 128)
index.html
<form method="GET">
{{ myFilter.form }}
<input type="submit" />
</form>
{% load render_table from django_tables2 %}
{% render_table stocks_table 'django_tables2/bootstrap.html' %}
这是result
在filtering...
之后
我认为问题在于我的 table 填充的是列表而不是查询集。
有没有更好的过滤django-tables2的方法?
非常感谢任何帮助!
我找到了一个 ,但它的 tables
使用了多个模型
我最终做的是从 mongo 获取所需的文档后,我创建了一个模型对象 Stocks 和将其附加到列表中并简单地 bulk_create(created list)
views.py
the_rows = col.find({}, {'_id': False})
json_data = list(the_rows)
obj_list = []
for data in json_data:
_open = round(float(data['Open']), 2)
_high = round(float(data['High']), 2)
_low = round(float(data['Low']), 2)
_close = round(float(data['Close']), 2)
obj = Stocks(name=data['Name'], date=data['Date'], open=_open, high=_high, low=_low, close=_close, volume=data['Volume'])
obj_list.append(obj)
Stocks.objects.bulk_create(obj_list)
tables.py 基于 models.py
import django_tables2 as tables
from .models import Stocks
class generateTable(tables.Table):
class Meta:
model = Stocks;
template = 'django_tables2/bootstrap.html';
然后创建 table 和过滤器,我按照我找到的步骤 here
views.py
qs = Stocks.objects.all()
myFilter = stockFilter(request.GET, queryset=qs)
table = generateTable(myFilter.qs)
RequestConfig(request, paginate={"per_page": 30}).configure(table)
return render(request, "home_app/index.html", { 'stocks_table': table, 'myFilter': myFilter })
最后是模板 index.html
{% block content %}
{% load render_table from django_tables2 %}
<div class="row">
<div class="filter_column">
<form>
{{myFilter.form}}
<input type="submit" />
</form>
</div>
<div class="table_column">
{% render_table stocks_table %}
</div>
</div>
{% endblock %}
Result
我有一个从 MongoDB 中获取的字典列表。然后我在 views.py 中填充 django-tables2 table 并在我的模板 index.html[= 中呈现它70=]
在views.py
from django.shortcuts import render
from .tables import generateTable
from django_tables2 import RequestConfig
import pymongo
import pandas as pd
import json
client = pymongo.MongoClient("<mongodb url>")
db = client["stocks"]
col = db["company_stocks"]
the_rows = col.find({}, {'_id': False})
pandas_dataframe = pd.DataFrame(list(the_rows), columns=['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Name'])
json_fomatted = pandas_dataframe.to_json(orient='records')
data = []
data = json.loads(json_fomatted)
def index(request):
table = generateTable(data)
RequestConfig(request).configure(table)
context = {'stocks_table': table }
return render(request, "home_app/index.html", context)
tables.py
import django_tables2 as tables
from .models import Stocks
class generateTable(tables.Table):
Date = tables.Column()
Open = tables.Column()
High = tables.Column()
Low = tables.Column()
Close = tables.Column()
Volume = tables.Column()
Name = tables.Column()
urls.py
urlpatterns = [
# Uncomment the next line to enable the admin:
path('admin/', admin.site.urls),
url(r'^$', home_app.views.index, name='index'),
url(r'^home$', home_app.views.index, name='home'),
]
index.html
{% load render_table from django_tables2 %}
{% render_table stocks_table 'django_tables2/bootstrap.html' %}
Result is this
现在我正在尝试使用 django-filter 过滤 table 但运气不好
我发现的是如何过滤使用查询集而不是字典列表填充的数据
我的过滤尝试:
views.py
...
from .models import Stocks
...
def index(request):
table = generateTable(data)
RequestConfig(request).configure(table)
#with this line
myFilter = stockFilter(request.GET, queryset=Stocks.objects.all())
context = {'stocks_table': table, 'myFilter': myFilter }
return render(request, "home_app/index.html", context)
filters.py
import django_filters
from .models import Stocks
class stockFilter(django_filters.FilterSet):
class Meta:
model = Stocks
fields = '__all__'
models.py
from django.db import models
class Stocks(models.Model):
open = models.IntegerField(
max_length = 128)
high = models.IntegerField(
max_length = 128)
low = models.IntegerField(
max_length = 128)
close = models.IntegerField(
max_length = 128)
volume = models.IntegerField(
max_length = 128)
name = models.CharField(
max_length = 128)
index.html
<form method="GET">
{{ myFilter.form }}
<input type="submit" />
</form>
{% load render_table from django_tables2 %}
{% render_table stocks_table 'django_tables2/bootstrap.html' %}
这是result
在filtering...
之后我认为问题在于我的 table 填充的是列表而不是查询集。
有没有更好的过滤django-tables2的方法?
非常感谢任何帮助!
我找到了一个
我最终做的是从 mongo 获取所需的文档后,我创建了一个模型对象 Stocks 和将其附加到列表中并简单地 bulk_create(created list)
views.py
the_rows = col.find({}, {'_id': False})
json_data = list(the_rows)
obj_list = []
for data in json_data:
_open = round(float(data['Open']), 2)
_high = round(float(data['High']), 2)
_low = round(float(data['Low']), 2)
_close = round(float(data['Close']), 2)
obj = Stocks(name=data['Name'], date=data['Date'], open=_open, high=_high, low=_low, close=_close, volume=data['Volume'])
obj_list.append(obj)
Stocks.objects.bulk_create(obj_list)
tables.py 基于 models.py
import django_tables2 as tables
from .models import Stocks
class generateTable(tables.Table):
class Meta:
model = Stocks;
template = 'django_tables2/bootstrap.html';
然后创建 table 和过滤器,我按照我找到的步骤 here
views.py
qs = Stocks.objects.all()
myFilter = stockFilter(request.GET, queryset=qs)
table = generateTable(myFilter.qs)
RequestConfig(request, paginate={"per_page": 30}).configure(table)
return render(request, "home_app/index.html", { 'stocks_table': table, 'myFilter': myFilter })
最后是模板 index.html
{% block content %}
{% load render_table from django_tables2 %}
<div class="row">
<div class="filter_column">
<form>
{{myFilter.form}}
<input type="submit" />
</form>
</div>
<div class="table_column">
{% render_table stocks_table %}
</div>
</div>
{% endblock %}
Result