Django-数据库数据未显示在应用程序中? [英] Django - database data not showing in application?
问题描述
我有一个名为"flows_db"的数据库,该数据库维护2个表"ingress_flows"和"egress_flows".我对两个表都进行了sqldump,因此可以在家用计算机上使用数据.因此,我使用转储文件在此计算机上重新创建表,并且由于select COUNT(*) from ingress_flows;
显示大约2000个条目,因此可以看到数据.
I have a database called 'flows_db' which maintains 2 tables 'ingress_flows' and 'egress_flows'. I did a sqldump of both tables so I could use the data on my home computer. So I used the dump files to re-create the tables on this computer, and I can see the data is there since select COUNT(*) from ingress_flows;
shows there are about 2000 entries.
然后我使用python manage.py inspectdb
从表中创建模型.为了测试它是否有效,我尝试获取一个包含表中所有条目的对象:
I then used python manage.py inspectdb
to create models from the tables. To test that it worked, I tried to get an object containing all entries in the table:
views.py
:
ingress_all = IngressFlows.objects.all()
context = {
'ingress_all': ingress_all,
}
return render(request, 'visualise/index.html', context)
index.html
:
{% if ingress_all %}
{% for flow in ingress_all %}
<script>
console.log("{{ flow.protocol }}");
</script>
{% endfor %}
{% else %}
<p>No data is available.</p>
{% endif %}
但是我只看到No data is available.
.因此,对于Django来说,即使我使用了包含原始数据库中所有数据的INSERT
语句的转储文件,我的数据库似乎还是空的:
But all I see is No data is available.
. So to Django it appears that my database is empty, even though I used the dump file which contains the INSERT
statements of all the data in the original database:
INSERT INTO `ingress_flows` VALUES (1434506151,1434506151,'UDP',48,'ff:ff:ff:ff:ff:ff','10.30.150.100','10.30.191.255',137,137,1024,102400),...
python manage.py shell
:
>>> from visualise.models import IngressFlows
>>> IngressFlows.objects.all()
[]
>>>
但是我可以手动插入对象:
But I can insert an object manually:
>>> t = IngressFlows(time_start=1434506151,time_end=1434506151,protocol='UDP',inf_in=48,mac_dst='ff:ff:ff:ff:ff:ff',ip_src='10.30.150.100',ip_dst='10.30.191.255',port_src=137,port_dst=137,packets_in=1024,bytes_in=102400)
>>> t.save()
>>> IngressFlows.objects.all()
[<IngressFlows: 1434506151, 1434506151, UDP, 48:ff:ff:ff:ff:ff:ff:10.30.150.100:10.30.191.255, 137:137, 1024, 102400>]
>>>
Django是否维护自己的数据库,而不是在系统上使用原始的MySQL数据库?如果是这样,由于它不使用由其他应用程序更新的系统范围的数据库,因此该方法如何适用? Django是如何看到这些变化的?
Does Django maintain its own database instead of using the original MySQL database on the system? If so, how is this applicable since it's not using the system-wide database which is being updated by other applications? How is Django meant to see these changes:
谢谢.
settings.py
:
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'flows_db',
'USER': 'root',
'PASSWORD': 'dbroot',
'OPTIONS': {
'autocommit': True,
},
}
}
推荐答案
很高兴发现问题所在.您需要为您的Django模型设置数据库名称. https://docs.djangoproject.com/zh-CN/1.8/ref/models/options/#db-table
Glad that you found the problem. You need to set the database name for yout Django model. https://docs.djangoproject.com/en/1.8/ref/models/options/#db-table
class IngressFlow(models.Model):
class Meta:
db_table = 'ingress_flow'
这篇关于Django-数据库数据未显示在应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!