wob*_*col 16 django action django-queryset
我有一个Django动作函数,我想在基于不同模型的查询集上使用它.
检查我的查询集组成的模型类型的最佳方法是什么?假设我想检查我的models.py中定义的Library类
目前我可以使用它
for object in queryset :
if object.__class__.__name__ == "Library"
Run Code Online (Sandbox Code Playgroud)
但我相信有更好的方法可以做到这一点.
我假设我使用queryset.model做某事.我尝试了以下内容,但它没有按照我的要求做到:
import myapp.models.Library
def my function(modeladmin,request queryset )
if isinstance(queryset.model , Library ) :
# do something specific here
Run Code Online (Sandbox Code Playgroud)
wob*_*col 30
好的,我知道,我使用的是isinstance():
if queryset.model is Library :
# do something.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6320 次 |
| 最近记录: |