Dilani
Dilani

Reputation: 553

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

Upvotes: 8

Views: 3752

Answers (2)

wobbily_col
wobbily_col

Reputation: 11931

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.

Upvotes: 11

MD. SHIFULLAH
MD. SHIFULLAH

Reputation: 1769

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'

Upvotes: 1

Related Questions