简体   繁体   English

如何在 Django 模型中返回包含给定值的字段名称?

[英]How to return field name containing a given value in a django model?

I have a list of strings I want to search for in my database, and I want my function to return the name of the field that contains the string:我有一个要在数据库中搜索的字符串列表,并且我希望我的函数返回包含该字符串的字段的名称:

    for word in word_list:
        if word in MyModel.objects.all():
           return ?? 

How do I complete the function so that it accomplishes this?我如何完成该功能以实现此目的?

This way you can pass the list of words and can return all the QuerySet that contains all the words in the word_list通过这种方式,您可以传递单词列表并返回包含 word_list 中所有单词的所有 QuerySet

for word in word_list:
    model = MyModel.objects.all()
    if word in model.filter(word__icontains = word):
        return model

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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