在同一模型上但在两个不同的字段上使用外键

时间:2019-09-10 12:15:49

标签: django

如果这个问题很愚蠢,对不起,但是我在Google上找不到任何有用的东西。

所以我有一个由两个字段组成的模型。

我想用其他一些字段创建模型,但是其中两个应该是上一个字段中的一个。有什么办法可以得到这个?

代码:

from django.db import models
from django.contrib.auth.models import User
from datetime import date

# Create your models here.
class CORMeserii(models.Model):
    CodCOR = models.CharField(max_length=25, primary_key=True, unique=True)
    MeserieCor = models.CharField(max_length=50, unique=True)

class Oferta(models.Model):
    solicitant = models.ForeignKey(User, on_delete=models.CASCADE)
    cor = models.ForeignKey(CORMeserii, max_length=25, on_delete=models.CASCADE)
    meserie = models.ForeignKey(CORMeserii, max_length=50, to_field='MeserieCor', on_delete=models.CASCADE)
    dataSolicitare = models.DateField(default=date.today)
    denumireMeserie = models.CharField(max_length=50)
    locuri = models.IntegerField()
    agentEconomic = models.CharField(max_length=50)
    adresa = models.CharField(max_length=150)
    dataExpirare = models.DateField()
    experientaSolicitata = models.CharField(max_length=200)
    studiiSolicitate = models.CharField(max_length=200)
    judet = models.CharField(max_length=20)
    localitate = models.CharField(max_length=25)
    telefon = models.CharField(max_length=12)
    emailContact = models.EmailField(max_length=40)
    rezolvata = models.BooleanField(default=False)
    def __str__(self):
        return self.cor

我认为这可能有效,但出现以下错误:

from django.db import models
from django.contrib.auth.models import User
from datetime import date
from .validators import validate_file_size

# Create your models here.
class CORMeserii(models.Model):
    CodCOR = models.CharField(max_length=25, primary_key=True, unique=True)
    MeserieCor = models.CharField(max_length=50, unique=True)

class Oferta(models.Model):
    solicitant = models.ForeignKey(User, on_delete=models.CASCADE)
    cor = models.ForeignKey(CORMeserii, max_length=25, on_delete=models.CASCADE)
    meserie = models.ForeignKey(CORMeserii, max_length=50, to_field='MeserieCor', on_delete=models.CASCADE)
    dataSolicitare = models.DateField(default=date.today)
    denumireMeserie = models.CharField(max_length=50)
    locuri = models.IntegerField()
    agentEconomic = models.CharField(max_length=50)
    adresa = models.CharField(max_length=150)
    dataExpirare = models.DateField()
    experientaSolicitata = models.CharField(max_length=200)
    studiiSolicitate = models.CharField(max_length=200)
    judet = models.CharField(max_length=20)
    localitate = models.CharField(max_length=25)
    telefon = models.CharField(max_length=12)
    emailContact = models.EmailField(max_length=40)
    rezolvata = models.BooleanField(default=False)
    def __str__(self):
        return self.cor

0 个答案:

没有答案