Neurion
Neurion

Reputation: 388

Django - database data not showing in application?

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.

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 %}

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>]
>>> 

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:

Thanks.

settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'mysql.connector.django',
        'NAME': 'flows_db',
        'USER': 'root',
        'PASSWORD': 'dbroot',
        'OPTIONS': {
          'autocommit': True,
        },        
    }
}

Upvotes: 2

Views: 5736

Answers (1)

Edwin Lunando
Edwin Lunando

Reputation: 2816

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'

Upvotes: 2

Related Questions