我通过OneToOneField链接了这两个模型
“父亲”课:
from django.db import models
class EmpresaManager(models.Manager):
def create_empresa(self):
empresa = self.create()
return empresa
class Empresa(models.Model):
codigo = models.CharField(max_length=3, null=False, default='000')
nombre = models.CharField(max_length=100, null=False, default='Sin nombre')
email = models.EmailField(null=False, default='email@empresa.com')
direccion = models.CharField(max_length=100, null=False, default='Sin direccion')
localidad = models.CharField(max_length=100, null=False, default='Sin localidad')
codDgi = models.SmallIntegerField(null=True)
docDgi = models.CharField(max_length=13, null=True)
sIva = models.CharField(max_length=1, null=True, default='1')
mesCierre = models.IntegerField(null=True, default=0)
fecIni = models.DateField(null=True)
ruca = models.IntegerField(null=True)
novedad = models.CharField(max_length=50, null=True, default='')
fecNovedad = models.DateTimeField(null=True)
ultVersion = models.CharField(max_length=20, null=True, default='')
logo = models.ImageField(null=True)
habilitado = models.CharField(max_length=1, null=True, default='S')
codEmpresa = models.CharField(max_length=3, null=True)
codComp = models.CharField(max_length=3, null=True, default='')
def __str__(self):
return "%s" % self.nombre
objects = EmpresaManager()
还有另一类:
from django.db import models
from Empresa.models import Empresa
class UsuarioManager(models.Manager):
def create_usuario(self):
usuario = self.create()
return usuario
class Usuario(models.Model):
empresa = models.OneToOneField(
Empresa,
on_delete=models.CASCADE,
primary_key=False,
)
codigo = models.CharField(max_length=10, null=False, default='Sin cod')
nombre = models.CharField(max_length=50, null=True)
puesto = models.CharField(max_length=50, null=True)
password = models.CharField(max_length=100, null=True)
rol = models.CharField(max_length=15, null=True, default='')
habilitado = models.CharField(max_length=1, null=True, default='S')
def __str__(self):
return self.nombre
objects = UsuarioManager()
问题: 我正在尝试Usuario.empresa继承 Empresa.nombre 的值(这是一个CharField,例如'McDonalds'),但是它继承了Empresa的行ID的值,我不明白为什么。 有什么建议么?非常感谢!