使用外键的表之间的关系错误

时间:2018-12-06 14:32:13

标签: python django

我有相关的Pessoa和Veiculo类别,还有Menssalista类别,仅当我在每月一次的课程中注册某个人时,需要选择仅显示轻松数据的Veiculo或Pessoa才需要。 例如,如果我选择Pessoa X,则出现Veiculo X的数据供选择,反之亦然。 您现在如何接受不相关的数据

models.py

from django.db import models
from django.core.mail import send_mail
import math, datetime
from django.utils import timezone

STATE_CHOICES = (
    ('AC', 'Acre'), ('AL', 'Alagoas'), ('AP', 'Amapá'),
    ('AM', 'Amazonas'), ('BA', 'Bahia'), ('CE', 'Ceará'),
    ('DF', 'Distrito Federal'), ('ES', 'Espírito Santo'),
    ('GO', 'Goiás'), ('MA', 'Maranhão'), ('MT', 'Mato Grosso'),
    ('MS', 'Mato Grosso do Sul'), ('MG', 'Minas Gerais'),
    ('PA', 'Pará'), ('PB', 'Paraíba'), ('PR', 'Paraná'),
    ('PE', 'Pernambuco'), ('PI', 'Piauí'), ('RJ', 'Rio de Janeiro'),
    ('RN', 'Rio Grande do Norte'), ('RS', 'Rio Grande do Sul'),
    ('RO', 'Rondônia'), ('RR', 'Roraima'), ('SC', 'Santa Catarina'),
    ('SP', 'São Paulo'), ('SE', 'Sergipe'), ('TO', 'Tocantins')
)

class Pessoa(models.Model):
    nome = models.CharField(max_length=50, blank=False)
    email = models.EmailField(blank=False)
    cpf = models.CharField(max_length=11, unique=True, blank=False)
    endereco = models.CharField(max_length=50)
    numero = models.CharField(max_length=10)
    bairro = models.CharField(max_length=30)
    telefone = models.CharField(max_length=20, blank=False)
    cidade = models.CharField(max_length=20)
    estado = models.CharField(max_length=2, choices=STATE_CHOICES)

    def __str__(self):
        return str(self.nome) + ' - ' + str(self.email)

class Marca(models.Model):
    nome = models.CharField(max_length=50)

    def __str__(self):
        return self.nome


class Veiculo(models.Model):
    marca = models.ForeignKey(Marca, on_delete=models.CASCADE, blank=False)
    modelo = models.CharField(max_length=20, blank=False)
    ano = models.CharField(max_length=7)
    placa = models.CharField(max_length=7)
    proprietario = models.ForeignKey(
    Pessoa, on_delete=models.CASCADE, blank=False, )
    cor = models.CharField(max_length=15, blank=False)


    def __str__(self):
        return self.modelo + ' - ' + self.placa

class Marca(models.Model):
    nome = models.CharField(max_length=50)

    def __str__(self):
    return self.nome


class Mensalista(models.Model):
    veiculo = models.ForeignKey(Veiculo, on_delete=models.CASCADE, 
        blank=False)
    inicio = models.DateField(("Date"), default=datetime.date.today)
    validade = models.DateField(("Date"), blank=False, )
    pessoa = models.ForeignKey(Pessoa, on_delete=models.CASCADE, 
        blank=False)
    valor_mes = models.DecimalField(
        max_digits=6, decimal_places=2, blank=False)
    email = models.ForeignKey(Pessoa, on_delete=models.CASCADE, blank=False)
    pago = models.CharField(max_length=15, choices=PAGO_CHOICES)

    @property
    def proprietario(self):
        return self.veiculo.proprietario

    @property
    def email(self):
        return self.pessoa.email

    def mensal(self):
        return math.ceil((self.validade - self.inicio).total_seconds() / 
            86400)

    def total_mes(self):
        return math.ceil(self.mensal() // 30)

    def total_mes_pagar(self):
        return self.valor_mes * self.total_mes()

    def __str__(self):
        return str(self.veiculo) + ' - ' + str(self.inicio)

0 个答案:

没有答案