I need relationship one for many between 3 tables

Asked

Viewed 61 times

0

Hello I have the table People, Vehicles and Messengers who are the users who pay per month the precise staging that each person can have more than one vehicle, but when registering in the monthly table when selecting a person appear in the other field only the cars that she owns, as it is now, is appearing all the cars of everyone.

I am very beginner and need detailed explanations, I appreciate the help and patience

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

py views.

@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})
  • You need to clarify your doubt, you mean the backend or the front? This seems to refer to the frontend, if yes put the screen to see, point the fields in which you want the result.

1 answer

0


From what I understand , at this point this attempt to relate two tables in a relation of N-N i.e., one person can have several cars , and one car can have several people, for a better database structure , needs a table the part where you have the foreign key for the person and another for the car. And for your doubt , where in the car dropdown appears depending on the person you choose , I advise to read this tutorial , can help.

https://simpleisbetterthancomplex.com/tutorial/2018/01/29/how-to-implement-dependent-or-chained-dropdown-list-with-django.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.