[英]Filter a DJango ContentType queryset to only include models that have a specific method
How can I filter a ContentType
queryset to only include models that have a specific method? 如何过滤
ContentType
以仅包括具有特定方法的模型?
class myModel(models.Model):
...
def mySpecificMethod:
...
What can I put in ???
我可以放什么
???
to get all models where mySpecificMethod
exists? 获取存在
mySpecificMethod
所有模型?
ContentType.objects.filter(???)
I think is more proper to filter it by loop. 我认为按循环过滤它更合适。
If you intend to get a result of type QuerySet
, you can simply loop them, then get the ID list, then use pk__in
parameter to filter it. 如果要获取
QuerySet
类型的结果,则可以简单地循环它们,然后获取ID列表,然后使用pk__in
参数对其进行过滤。
ContentType.objecst.filter(
pk__in=[
ct.pk for ct ContentType.objects.all()
if content_type_has_method(ct, 'method_name')
]
)
So, you have reduced this question to write a method: 因此,您减少了这个问题,可以编写一个方法:
def content_type_has_method(ct, method_name):
...
And I think that is simpler for you, good luck! 我想这对您来说更简单,祝您好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.