0

I have the class Pessoa and Veiculo that are related, and I have the class Menssalista, only that I need that when registering someone in the monthly class, selecting Veiculo or Pessoa that automatically only the relaxed data is presented. Example if I choose the Pessoa X that the data of the Veiculo X appear for choice and vice versa. How are you now accepting data that is not related

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)
  • First, a few comments about your english (don't want to be rude, but if you want to get help, you need to make yourself be understood): The first sentence can easily be split up into two or three sentences. Then next, please try to be more clear about what you want to achieve. And also very important: what did you try so far to achieve it? As it is now, the code you post just shows us some models and we do not really know what you want to do with them. Please also read up https://stackoverflow.com/help/how-to-ask – Mekanik Dec 06 '18 at 14:51
  • I think you need to do this in the view: see these answers - I would recommend the second answer: https://stackoverflow.com/questions/232435/how-do-i-restrict-foreign-keys-choices-to-related-objects-only-in-django – ger.s.brett Dec 06 '18 at 14:55
  • It's not rude at all, I'm Brazilian and I use google to translate. What I basically want is that when I register in the class Mensalista you can only register the Pessoa that have the related Veiculo enter, as foreign key – Mauricio Kalfelz Dec 06 '18 at 15:04

0 Answers0