Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the prefix of a table for a Django model?

Tags:

I create a new Django app (not project) called Bussinesses, then add following class to the models.py.

class Bussinesses(models.Model):     business_email = models.EmailField()     password = models.CharField(max_length=20)     contact_first_name = models.CharField(max_length=30)     contact_last_name = models.CharField(max_length=30) 

If I use Bussinesses directly, the Django will access the "bussinesses_bussinesses" table in the database, which obviously does not exist.

Because the table "bussinesses" is also used by another APP, So can't rename it. I want to know how to use the Django model without using table prefix, and I don't want to use the raw database API directly.

like image 582
redice Avatar asked Mar 24 '12 06:03

redice


People also ask

How do I delete an existing model in Django?

Delete a Record of a Model Using the delete() Method in Django. All the model objects or instances in Django have a delete() method, which can be used to delete that record. Now, this delete() method can be also be used to delete a single record and a bunch of them.

How do I delete all records in Django?

If you want to remove all the data from all your tables, you might want to try the command python manage.py flush . This will delete all of the data in your tables, but the tables themselves will still exist. He did say "all the data from all your tables", which should indicate that it's a very destructive operation.

How do you name a database table in Django?

To save you time, Django automatically derives the name of the database table from the name of your model class and the app that contains it. A model’s database table name is constructed by joining the model’s “app label” – the name you used in manage.py startapp – to the model’s class name, with an underscore between them.

How to remove a table from a Django model?

If you don't want to delete and re-sync your current database, the best way is to drop the table from your model manually: $ python manage.py dbshell > (Inside the DB shell) > DROP TABLE {app-name}_ {model-name}; Now manage.py migrate complains that the original table no longer exists. There is no syntax. Django doesn't removes tables or columns.

What is a model in Django?

A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table. Each model is a Python class that subclasses django.db.models.Model. Each attribute of the model represents a database field.

How do you create an object in a database in Django?

To represent database-table data in Python objects, Django uses an intuitive system: A model class represents a database table, and an instance of that class represents a particular record in the database table. To create an object, instantiate it using keyword arguments to the model class, then call save () to save it to the database.


2 Answers

Just use the model meta options.

class Bussinesses(models.Model):     business_email = models.EmailField()     password = models.CharField(max_length=20)     contact_first_name = models.CharField(max_length=30)     contact_last_name = models.CharField(max_length=30)      class Meta:         db_table = "bussinesses" 

BTW businesses is misspelled. Since you're specifying the name of the table you don't have to give your model the same name as the table, so if the table name is misspelled and you can't easily fix it, you can at least change the name of your class to the proper spelling of businesses. I would also get rid of the pluralization, and make it class Business. Finally it's not uncommon when using Django or Rails on an existing database to need to set a custom table name for every table.

like image 139
Benjamin Atkin Avatar answered Oct 24 '22 07:10

Benjamin Atkin


As an aside, if you want to change (or remove) the prefix for all tables you can provide your own meta-class. This is useful if you have multiple cases like Businesses where you'd just like the class and table name to be the same.

For instance the following code prefixes all tables with FOO_ instead of appname_:

class MyModelBase( ModelBase ):     def __new__( cls, name, bases, attrs, **kwargs ):         if name != "MyModel":             class MetaB:                 db_table = "FOO_" + name              attrs["Meta"] = MetaB          r = super().__new__( cls, name, bases, attrs, **kwargs )         return r         class MyModel( Model, metaclass = MyModelBase ):     class Meta:         abstract = True  class Businesses( MyModel ):     ... 
like image 41
c z Avatar answered Oct 24 '22 07:10

c z