我需要3张桌子之间的一对多关系

时间:2018-12-11 00:37:29

标签: python django

我需要从Pessoa到Veiculo的一对多关系在Mensalista中注册 我是巴西人,然后在桌子上Veiculo ==车辆,Pessoa ==人,Mensalista ==停车

我需要一个人拥有多辆车,并且当您在停车场登记时选择一个人出现在另一个过滤器中时,只会出现他们的车辆。 我很新,需要详细说明,感谢您的帮助和耐心。

Models.py

TATE_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 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

views.py

@login_required
def mensalista_novo(request):
    if request.method == 'POST':
        form = MensalistaForm(request.POST or None)
        if form.is_valid():
            form.save()
            return redirect('core_lista_mensalista')
    else:
        form = MensalistaForm
    return render(request, 'core/lista_mensalistas.html', {'form': form})

0 个答案:

没有答案