创建一个tables.py文件(也可以直接写在models.py里),然后用python描述一段表结构:
from django.db import models
class customer(models.Model):
id = models.CharField(max_length=30)
name = models.CharField(max_length=100)
django自动生成的表名是app名和模型名的组合,假设该app名叫test,那么这个表名就叫test_customer。但如果我不想用默认的这个名字,那就在customer类里自定义db_table属性,代码如下:
from django.db import models
class customer(models.Model):
id = models.CharField(max_length=30)
name = models.CharField(max_length=100)
class Meta:
db_table="customer"