Database table names with Django

I already have a database called "mydb", where I have a table called "AERODROME".

My models.py looks like this:

from django.db import models class Aerodrome(models.Model): Name = models.CharField(max_length=48) Latitude = models.DecimalField(decimal_places=4, max_digits=7) Longitude = models.DecimalField(decimal_places=4, max_digits=7) 

And I have this method on views.py:

 from django.shortcuts import render from helloworld.models import Aerodrome def aerodromes(request): return render(request, 'aerodromes.html', {'aerodromes': Aerodrome.objects.all()}) 

In my templates folder, I have aerodromes.html, which is also pretty simple:

 <!doctype html> <html> <head> </head> <body> <table> {% for aerodrome in aerodromes %} <tr> <td>{{ aerodrome.Name }}</td> <td>{{ aerodrome.Longitude }}</td> <td>{{ aerodrome.Latitude }}</td> </tr> {% endfor %} </table> </body> </html> 

When I test my browser, I get an error message because it looks like it is accessing a table with the wrong name. My application is called "helloworld" because it is a test, and instead of accessing mydb.AERODROMES it accesses mydb.helloworld_aerodrome (also note the case-sensitive problem).

Since I already had a database, I did not run syncdb (I realized that this was not necessary, but maybe this is a problem).

So, the problem is that I don’t know why it adds “helloworld_” to the name of the table, and also that I still don’t know exactly where I am correcting the name of the table (and from there comes a case-sensitive problem having “airfield ", not" AERODROMS ").

Any help here?

+4
source share
4 answers

Use the Meta class ( here ) in the models.py model models.py :

 class Aerodrome(models.Model): Name = models.CharField(max_length=48) Latitude = models.DecimalField(decimal_places=4, max_digits=7) Longitude = models.DecimalField(decimal_places=4, max_digits=7) class Meta: db_table = 'AERODROMES' 

This will override the default naming scheme for model tables in the SQL database.


You can also add the managed attribute to control whether the python manage.py syncdb and python manage.py flush tables manage.

 class Aerodrome(models.Model): # ... class Meta: db_table = 'AERODROMES' managed = False 

With this, you can syncdb without fear of erasing your data.

+9
source

Specifying a table name for your model should clearly help:

 from django.db import models class Aerodrome(models.Model): Name = models.CharField(max_length=48) Latitude = models.DecimalField(decimal_places=4, max_digits=7) Longitude = models.DecimalField(decimal_places=4, max_digits=7) class Meta: db_table = 'AERODROMES' 
+1
source

from django docs: it is highly recommended that you use lowercase table names when overriding the table name through db_table, especially if you use the MySQL backend. See MySQL Notes for more details.

https://docs.djangoproject.com/en/1.11/ref/databases/#table-names

+1
source

You can set the db table name in Meta class models . as

 class Aerodrome(models.Model): Name = models.CharField(max_length=48) Latitude = models.DecimalField(decimal_places=4, max_digits=7) Longitude = models.DecimalField(decimal_places=4, max_digits=7) class Meta: db_table="AERODROMES" 

If you pre-populate the table from the outside. make sure the data is consistent / compatible in each record / field.

However, you must syncdb as other tables are created for django to be created.

+1
source

Source: https://habr.com/ru/post/1479550/


All Articles