Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:SQL对许多关系的查询_Python_Sql_Django_Phpmyadmin - Fatal编程技术网

Python Django:SQL对许多关系的查询

Python Django:SQL对许多关系的查询,python,sql,django,phpmyadmin,Python,Sql,Django,Phpmyadmin,我有一个应用程序模型,可以采用任意数量的主题: class Application(models.Model): themes = models.ManyToManyField(Theme) date_check = models.BooleanField(blank=True) # other fields... class Theme(models.Model): name = models.CharField(max_length=200)

我有一个应用程序模型,可以采用任意数量的主题:

class Application(models.Model):
     themes = models.ManyToManyField(Theme)
     date_check = models.BooleanField(blank=True)
     # other fields...

class Theme(models.Model):
     name = models.CharField(max_length=200)
     # other fields...
我需要交付一个导出的数据库(通过phpMyAdmin),其中只包含至少有一个主题的应用程序(以及一些其他过滤器)。在Django中,这很简单(尽管我确信我在下面写的不是最简单的方法):

我只是不知道如何将
a.date\u check=True和len(a.get\u themes())>1
转换成SQL,我可以在phpMyAdmin中运行SQL来导出数据库的Excel文件。有什么想法吗


编辑:我通过使用Django直接导出到Excel而不是通过phpMyAdmin解决了这个问题。出于某种奇怪的原因,我认为通过Django会更加困难。尽管我的任务已经解决,但这个问题仍然没有得到回答。

为什么需要“获取主题”?只要len(a.themes)就足够了。
len(a.themes)
效率很低,因为它会将查询集转换为列表,然后对元素进行计数。使用执行SQL计数的
a.themes.count()
进行计数要好得多。效率更高。谢谢你的提示。但是在SQL中这样做怎么样?
apps = Application.objects.filter(date_check=True)
filtered_apps = []
for a in apps:
    if len(a.get_themes()) >= 1:     # get_themes() returns a comma-separated list of themes
        filtered_apps.append(a)