Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

app name is appended to the table name in django

When I try to fetch data from the table, the app name is appended to the table name and displays an error. Following is my code.

from models import open_cart

class test(APIView):

  def get(self,request,format=None):
    values = open_cart.objects.get()

My app name that I have defined in installed_apps is 'MyApp'.My table name is 'open_cart'. table name in the query goes as MyApp_open_cart instead of open_cart. the error message that i get is relation "untitled_open_cart" does not exist

like image 501
Dilani Avatar asked Jun 15 '17 08:06

Dilani


People also ask

How to specify table name in Django model?

If you want to map an existing table to a django model you have just to use the db_table property of the Meta class of your django model.

What is Db_table?

db_table option gives a flexibility to user for specifying a database table name. From the error it looks like that table gender does not exist in database. That means after specifying this option you haven't performed syncdb . Please do a syncdb or if you are using south then schemamigration .

What is managed false in Django?

The main reason for using managed=False is if your model is backed by something like a database view, instead of a table - so you don't want Django to issue CREATE TABLE commands when you run syncdb .


2 Answers

Appending the app name to the table name is default behavior in Django. If you want to use a custom table name add it in the Meta class.

class MyModel(models.Model):
    class Meta(CommonInfo.Meta):
        db_table = 'student_info'

Refer to the official Django Model Meta options documentation for more info.

like image 88
wobbily_col Avatar answered Oct 01 '22 10:10

wobbily_col


Django Append app name before the model name by default. If we want to use our custom table name then it will have to mention in side the Meta class.

class Open_cart(models.Model):
    class Meta:
        db_table = 'open_cart'
like image 23
MD. SHIFULLAH Avatar answered Oct 01 '22 09:10

MD. SHIFULLAH