数据库原有表生成django models
数据库原有表生成django models
当数据库已经有表的时候,但是django里没有对应的models映射,手写的话很麻烦,这时可以借助python manage.py inspectdb命令来自动生成对应的models
在运行这条命令前你需要在 :settings.py文件中配置好你的数据库连接。每个表的结果会是一个包含表模板的文件。你可能想保存这个文件:
$ python manage.py inspectdb > models.py
输出文件会存入到你当前目录。移动文件至正确应用,你就可以开始更进一步的定制化了。
下面是自动生成的models.py以及models表
# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
# * Rearrange models' order
# * Make sure each model has one field with primary_key=True
# * Make sure each ForeignKey and OneToOneField has `on_delete` set to the desired behavior
# * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
from django.db import models
class App01Attachment(models.Model):
id = models.BigAutoField(primary_key=True)
transaction_id = models.UUIDField(blank=True, null=True)
attachment_name = models.CharField(max_length=100)
attach_file = models.CharField(max_length=100, blank=True, null=True)
class Meta:
managed = False
db_table = 'app01_attachment'
class App01Author(models.Model):
id = models.BigAutoField(primary_key=True)
name = models.CharField(max_length=100)
age = models.IntegerField()
class Meta:
managed = False
db_table = 'app01_author'
class App01Book(models.Model):
id = models.BigAutoField(primary_key=True)
name = models.CharField(max_length=10)
price = models.DecimalField(max_digits=10, decimal_places=2)
uuid = models.UUIDField(unique=True)
publish = models.ForeignKey('App01Publish', models.DO_NOTHING, blank=True, null=True)
pages = models.IntegerField(blank=True, null=True)
pubdate = models.DateField(blank=True, null=True)
rating = models.FloatField(blank=True, null=True)
class Meta:
managed = False
db_table = 'app01_book'
class App01BookAuthors(models.Model):
id = models.BigAutoField(primary_key=True)
book = models.ForeignKey(App01Book, models.DO_NOTHING)
author = models.ForeignKey(App01Author, models.DO_NOTHING)
class Meta:
managed = False
db_table = 'app01_book_authors'
unique_together = (('book', 'author'),)
class App01Client(models.Model):
id = models.BigAutoField(primary_key=True)
password = models.CharField(max_length=128)
last_login = models.DateTimeField(blank=True, null=True)
is_superuser = models.BooleanField()
username = models.CharField(unique=True, max_length=150)
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
email = models.CharField(max_length=254)
is_staff = models.BooleanField()
is_active = models.BooleanField()
date_joined = models.DateTimeField()
phone = models.BigIntegerField(blank=True, null=True)
class Meta:
managed = False
db_table = 'app01_client'
class App01ClientGroups(models.Model):
id = models.BigAutoField(primary_key=True)
client = models.ForeignKey(App01Client, models.DO_NOTHING)
group = models.ForeignKey('AuthGroup', models.DO_NOTHING)
class Meta:
managed = False
db_table = 'app01_client_groups'
unique_together = (('client', 'group'),)
class App01ClientUserPermissions(models.Model):
id = models.BigAutoField(primary_key=True)
client = models.ForeignKey(App01Client, models.DO_NOTHING)
permission = models.ForeignKey('AuthPermission', models.DO_NOTHING)
class Meta:
managed = False
db_table = 'app01_client_user_permissions'
unique_together = (('client', 'permission'),)
class App01Item(models.Model):
id = models.BigAutoField(primary_key=True)
name = models.CharField(max_length=10)
data = models.IntegerField()
rate = models.IntegerField(blank=True, null=True)
class Meta:
managed = False
db_table = 'app01_item'
class App01Publish(models.Model):
id = models.BigAutoField(primary_key=True)
name = models.CharField(max_length=10)
addr = models.CharField(max_length=30)
uuid = models.UUIDField(unique=True, blank=True, null=True)
class Meta:
managed = False
db_table = 'app01_publish'
class App01Store(models.Model):
id = models.BigAutoField(primary_key=True)
name = models.CharField(max_length=300)
class Meta:
managed = False
db_table = 'app01_store'
class App01StoreBooks(models.Model):
id = models.BigAutoField(primary_key=True)
store = models.ForeignKey(App01Store, models.DO_NOTHING)
book = models.ForeignKey(App01Book, models.DO_NOTHING)
class Meta:
managed = False
db_table = 'app01_store_books'
unique_together = (('store', 'book'),)
class App01Transaction(models.Model):
id = models.BigAutoField(primary_key=True)
uuid = models.UUIDField(unique=True)
txn_name = models.CharField(max_length=100)
amount = models.DecimalField(max_digits=8, decimal_places=2)
class Meta:
managed = False
db_table = 'app01_transaction'
class App01Uploadfile(models.Model):
id = models.BigAutoField(primary_key=True)
uuid = models.UUIDField(unique=True)
file = models.CharField(max_length=100)
info = models.JSONField(blank=True, null=True)
class Meta:
managed = False
db_table = 'app01_uploadfile'
class App01Userinfo(models.Model):
id = models.BigAutoField(primary_key=True)
name = models.CharField(max_length=50)
registered_on = models.DateField()
account_type = models.CharField(max_length=1)
amount = models.FloatField()
addr = models.CharField(max_length=100)
school = models.CharField(max_length=100)
class Meta:
managed = False
db_table = 'app01_userinfo'
class AuthGroup(models.Model):
name = models.CharField(unique=True, max_length=150)
class Meta:
managed = False
db_table = 'auth_group'
class AuthGroupPermissions(models.Model):
id = models.BigAutoField(primary_key=True)
group = models.ForeignKey(AuthGroup, models.DO_NOTHING)
permission = models.ForeignKey('AuthPermission', models.DO_NOTHING)
class Meta:
managed = False
db_table = 'auth_group_permissions'
unique_together = (('group', 'permission'),)
class AuthPermission(models.Model):
name = models.CharField(max_length=255)
content_type = models.ForeignKey('DjangoContentType', models.DO_NOTHING)
codename = models.CharField(max_length=100)
class Meta:
managed = False
db_table = 'auth_permission'
unique_together = (('content_type', 'codename'),)
class Company(models.Model):
name = models.CharField(max_length=255)
age = models.IntegerField()
class Meta:
managed = False
db_table = 'company'
class DjangoAdminLog(models.Model):
action_time = models.DateTimeField()
object_id = models.TextField(blank=True, null=True)
object_repr = models.CharField(max_length=200)
action_flag = models.SmallIntegerField()
change_message = models.TextField()
content_type = models.ForeignKey('DjangoContentType', models.DO_NOTHING, blank=True, null=True)
user = models.ForeignKey(App01Client, models.DO_NOTHING)
class Meta:
managed = False
db_table = 'django_admin_log'
class DjangoContentType(models.Model):
app_label = models.CharField(max_length=100)
model = models.CharField(max_length=100)
class Meta:
managed = False
db_table = 'django_content_type'
unique_together = (('app_label', 'model'),)
class DjangoMigrations(models.Model):
id = models.BigAutoField(primary_key=True)
app = models.CharField(max_length=255)
name = models.CharField(max_length=255)
applied = models.DateTimeField()
class Meta:
managed = False
db_table = 'django_migrations'
class DjangoSession(models.Model):
session_key = models.CharField(primary_key=True, max_length=40)
session_data = models.TextField()
expire_date = models.DateTimeField()
class Meta:
managed = False
db_table = 'django_session'
class FieldHistoryFieldhistory(models.Model):
id = models.BigAutoField(primary_key=True)
object_id = models.TextField()
field_name = models.CharField(max_length=500)
serialized_data = models.TextField()
date_created = models.DateTimeField()
content_type = models.ForeignKey(DjangoContentType, models.DO_NOTHING)
user = models.ForeignKey(App01Client, models.DO_NOTHING, blank=True, null=True)
class Meta:
managed = False
db_table = 'field_history_fieldhistory'
class TokenBlacklistBlacklistedtoken(models.Model):
id = models.BigAutoField(primary_key=True)
blacklisted_at = models.DateTimeField()
token = models.OneToOneField('TokenBlacklistOutstandingtoken', models.DO_NOTHING)
class Meta:
managed = False
db_table = 'token_blacklist_blacklistedtoken'
class TokenBlacklistOutstandingtoken(models.Model):
id = models.BigAutoField(primary_key=True)
token = models.TextField()
created_at = models.DateTimeField(blank=True, null=True)
expires_at = models.DateTimeField()
user = models.ForeignKey(App01Client, models.DO_NOTHING, blank=True, null=True)
jti = models.CharField(unique=True, max_length=255)
class Meta:
managed = False
db_table = 'token_blacklist_outstandingtoken'