[Fixed]-How use a few foreignkey in view.py django and make some logic?

1👍

You forgot the objects after the Rent, but I did it for you here and gave you some examples:

rent = Rent.objects.get(id=1)
apartment_cost = rent.id_outlet.cost
payment = Payment.objects.get(id_rent=rent)
date_of_payment = payment.date_of_payment
....

So the first one is to create a variable called rent and assign it to Rent with and id=1. With that I could easily get the cost of the Apartment since you have a ForeignKey in your Rent model to the Apartment model. So we went from Rent to the id_outlet which links us to the Apartment model, and from their we accessed the price. Easy right? ForeignKeys make them easier for you, you can jump from one place to another easily just by connecting them well, which I say you did pretty well! Hope that answered your question.

Try not to call your FK fields like client_id or rent_id as you did in your question, because you are referring the whole model Client and not just the client’s id, so it’s better to you client as the field. Same goes for the `rent_id.

Leave a comment