根据模型过滤查询获取所有相关对象

时间:2022-09-02 15:16:56

Trying to get all the list of loan along with the related its addresses linked to that particular loan.

试图获得所有贷款清单以及与该特定贷款相关的相关地址。

Models:

class LoanDetail(models.Model):
    job_no = models.AutoField('Job No', primary_key=True)
    loan_account_no = models.CharField('Loan account No', blank=True, max_length=128)
    job_status = models.IntegerField('Job Status', choices=JOB_STATUS, default=1, db_index=True)
    applicant_type = models.IntegerField('Applicant type', choices=APPLICANT_TYPE, default=1, db_index=True)
    customer_name = models.CharField('Customer Name', max_length=128)

class LoanUserAddress(models.Model):
    loan = models.ForeignKey(LoanDetail, on_delete=models.CASCADE, related_name="loanuser")
    address_type = models.ForeignKey(AddressType)
    house_name = models.CharField('House/Flat/Name', max_length=128)
    street = models.CharField('Street', max_length=128)
    area = models.CharField('Area/Location', max_length=128)

views:

class SearchLoan(APIView, ResponseViewMixin):

    def get(self, request, *args, **kwargs):
        loan = kwargs['loan'];
        address = LoanDetail.objects.filter(loan_account_no__in=[loan])

This is returning only the loan details how can I get all the Address along with the loandetails.

这只返回贷款详细信息如何获得所有地址以及贷款细节。

I would like the response to be like {'loan_account_no': '12412412421', 'customer_name': 'Ravi' , address: [ {'id': 1, 'house_name': 'Some aadress'}, {'id': 2, 'house_name': 'Some aadress 2'}, {'id': 3, 'house_name': 'Some aadress 3'}] }

我希望响应像{'loan_account_no':'12412412421','customer_name':'Ravi',地址:[{'id':1,'house_name':'some aadress'},{'id': 2,'house_name':'some aadress 2'},{'id':3,'house_name':'some aadress 3'}}}

1 个解决方案

#1


0  

If you just want the addresses for a set of loads

如果您只想要一组负载的地址

addresses = LoanUserAddress.objects.filter(loan__loan_account_no__in=[loan])

If you want a list of loans with the adresses, get the list of loans like you did

如果你想要一份有地址的贷款清单,请像你一样获得贷款清单

loans = LoanDetail.objects.filter(loan_account_no__in=[loan])

and for each items in the queryset you can access the attribute loanuser that will be the list of addresses for this loan. where loanuser is the related_name of the ForeignKey field.

对于查询集中的每个项目,您可以访问属性loanuser,该属性将是此贷款的地址列表。其中,loanuser是ForeignKey字段的related_name。

for loan_item in loans.all():
    print(loan_item.loanuser)

#1


0  

If you just want the addresses for a set of loads

如果您只想要一组负载的地址

addresses = LoanUserAddress.objects.filter(loan__loan_account_no__in=[loan])

If you want a list of loans with the adresses, get the list of loans like you did

如果你想要一份有地址的贷款清单,请像你一样获得贷款清单

loans = LoanDetail.objects.filter(loan_account_no__in=[loan])

and for each items in the queryset you can access the attribute loanuser that will be the list of addresses for this loan. where loanuser is the related_name of the ForeignKey field.

对于查询集中的每个项目,您可以访问属性loanuser,该属性将是此贷款的地址列表。其中,loanuser是ForeignKey字段的related_name。

for loan_item in loans.all():
    print(loan_item.loanuser)