我是巴西人,然后在表Veiculo ==车辆和Pessoa ==人 由于一个人可以拥有多辆车,只有一个人可以拥有一辆车,因此在停车场中注册新的每月登机手续时,我只需要选择该人即可选择自己拥有的汽车。 我需要在注册Pessoa和Veiculo时将它们作为Realacionamos,以便在我使用Pessoa贴面时使用Views Mensalista注册时将其拥有的Veiculo出现
Models.py
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 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 :(得分:0)
如果您想要一个人的汽车,可以使用以下过滤器:
proprietarioId = id
queryList = Veiculo.objects.filter(proprietario__id = proprietarioId)