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
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.
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 .
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 .
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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With