djangoORM 通过外键字段找对应类
Posted LCQ
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了djangoORM 通过外键字段找对应类相关的知识,希望对你有一定的参考价值。
两个方法其实是一种,用哪个都行,看实例:
方法一:
从list_filter中的字符串,找到model对象的字段,然后得到这个外键对应的类
![](https://image.cha138.com/20210607/8d64ce8a285c414082d1013dd6007e9f.jpg)
![](https://image.cha138.com/20210607/3b34a1a0cb094280afa870d293862cc7.jpg)
![](https://image.cha138.com/20210607/fb9ae646226945a6b07c97bf669dacb2.jpg)
![](https://image.cha138.com/20210607/3d881ee4ee8b40e5888f538997c73dc1.jpg)
![](https://image.cha138.com/20210607/ff31c95487a346a7983ad8a3bb249fa5.jpg)
![](https://image.cha138.com/20210607/ed229c6db83b4d69bbaa98c26a32edbe.jpg)
循环,把list_filter中对应的类所有对象
![](https://image.cha138.com/20210607/4258612864eb4b8f98e10fd2291a2661.jpg)
![](https://image.cha138.com/20210607/b3c355efcaad402b8c0c8bf25247784a.jpg)
方法二:
![](https://image.cha138.com/20210607/cb590a6674474235b36f137f80c27ced.jpg)
![](https://image.cha138.com/20210607/b54e552a96f54d658edf42c67b143143.jpg)
两方法实际上是一样的,导入方式和使用方式不同:
方法一:
导入:from django.db.models.fields.related import ForeignKey,ManyToManyField
使用:field.rel.to
方法二:
导入:from django.db.models import ForeignKey,ManyToManyField
使用:field.rel.model
以上是关于djangoORM 通过外键字段找对应类的主要内容,如果未能解决你的问题,请参考以下文章