我不知道如何在数据库中激活关系中的外键注册

loading...


0

我是巴西人然后在桌子 Veiculo == vehiclePessoa == person 并且因为一个人可以有几辆车加一辆车只有一个人,当在停车场注册新的月度 checkin 时,我需要选择那个人只有他拥有的车辆出现作为一种选择 . 我需要注册一个 Pessoa 和一个 Veiculo 时它们是 realacionamos ,当我使用 Pessoa soment 注册使用它时,它所拥有的Veiculo

在一个论坛给了我使用下面的代码的想法,但我不知道如何实现它

proprietarioId = id
queryList = Veiculo.objects.filter(proprietario__id = proprietarioId)

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})

loading...

1回答

  • 0

    你必须从形式fronend方面做,每当Pessoa改变写一个jquery函数,调用一个终点来获得所有Veiculo与Pessoa的同事

    $("#Pessoa fields id").onchange(function(){
    
          $ajax({
            url:"",
            data:{"id":$("#Pessoa fields id").val()},
            method: get
          })
     })
    

    然后定义一个视图

    def get_veiculo(self, request, *args, **kwarsg):
        proprietarioId = request.GET.get('id')
        queryList = Veiculo.objects.filter(proprietario_id = proprietarioId).all()
        return render('option.html', request, {'Veiculo':queryList})
    

    option.html

    {% for data in Veiculo %}
      <option value="{{data.id}}">{{data.modelo}}</option>
    
评论

暂时没有评论!