Django的模型里有外键列
如下:
from django.db import models
# Create your models here.
class UserInfo(models.Model):
username = models.CharField(verbose_name='用户名', max_length=32)
password = models.CharField(verbose_name='密码', max_length=64)
createtime = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
class Meta:
# 设置表名
db_table = "userinfo"
class Avatar(models.Model):
photouuid = models.CharField(max_length=100)
photoname = models.CharField(max_length=100)
editor = models.ForeignKey(UserInfo, blank=True, on_delete=models.DO_NOTHING,db_column='userinfo_avator_id')
class Meta:
# 设置表名
db_table = "useravatar"
如果不单独设置外键名称,外键会根据字段名,直接在后面加上’_id’,来进行区分外键,如果需要自定义外键名称,需要一个属性,外键会根据这个属性生成
db_column='userinfo_avator_id'
|