jeudi 24 janvier 2019

OneToOneField attribute is linking ID instead of CharField

I have these two models linked by OneToOneField

"Father" class:

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()

And the other class:

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()

The problem: I'm trying Usuario.empresa to inherit the value from Empresa.nombre (which is a CharField, for example 'McDonalds'), but it is inheriting the value of the Empresa's row ID instead, and I can't get why. Any suggestions? Many thanks!




Aucun commentaire:

Enregistrer un commentaire