简体   繁体   中英

many to many relationship between multiple tables using django

I have two tables borrower and lender . A borrower can request many lenders and lenders also can get multiple requests from borrowers.

So I want to give many to many relation between two of this. Something like:

class borrower(models.Model):
    name=models.CharField(max_length=20)
    # . . . other fields

class lender(models.Model):
    city = models.CharField(max_length=20)
    # . . . other fields

Now in the loanrequest table I want to define many to many relation between these two tables. How to achieve that?

Based on my understanding from the little information that you have provided, you may try doing this:

Class Borrower(models.Model):
  name = models.CharField(max_length=20)
  lender = models.ManyToManyField(to='Lender', related_name='borrow', through='LoanRequest')

class LoanRequest(models.Model):
  borrower = models.ForeignKey(to='Borrower', related_name='loan_borrower')
  lender = models.ForeignKey(to='Lender', related_name='loan_lender')

Also, you may add other fields in LoanRequest model as per your need (for ex- final_rate_of_interest - this ofcourse is an example, you may change as per your requirement).

Anyways, you might giving this , this (for knowing 'through' tables),and also this a read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM