Python django select与3个表相关
我在django有3种型号,如下所示:Python django select与3个表相关,python,django,Python,Django,我在django有3种型号,如下所示: class SlackConfigurationMode(models.Model): MODES = ( ("NORMAL", "normal"), ("ALERT", "alert"), ("DANGER", "danger") ) mode = models.CharField(choices=MODES,
class SlackConfigurationMode(models.Model):
MODES = (
("NORMAL", "normal"),
("ALERT", "alert"),
("DANGER", "danger")
)
mode = models.CharField(choices=MODES, default=MODES[0][0], max_length=20)
time_send_slack_notification_minute = models.IntegerField(default=0)
time_send_slack_notification_hour = models.IntegerField(default=0)
description = models.TextField(blank=True)
class WebHookConfiguration(models.Model):
webhook_url = models.CharField(max_length=100)
slack_configuration_mode = models.ForeignKey(
SlackConfigurationMode,
on_delete=models.CASCADE,
related_name='webhook_configurations'
)
class MonitorSource(models.Model):
TYPES = (
("FACEBOOK", "facebook"),
("WEBSITE", "website"),
("YOUTUBE", "youtube")
)
target = models.CharField(max_length=100)
type = models.CharField(choices=TYPES, max_length=20)
created_at = models.DateTimeField(auto_now_add=True)
owner = models.ForeignKey("auth.User", on_delete=models.CASCADE)
name = models.CharField(max_length=100)
slack_configuration = models.ForeignKey(
SlackConfigurationMode, on_delete=models.DO_NOTHING,
default=SlackConfigurationMode.objects.filter(mode="NORMAL")[0].id,
related_name='monitor_sources'
)
我想通过slackconfiguration by mode获取webhook_配置和monitorsource筛选器的数据
我使用这个查询:
queryset = SlackConfigurationMode.objects.select_related('webhook_configurations', 'monitor_sources').filter(
mode='HIGH'
)
但是有一个错误:
在与select_相关的“监视_源”、“webhook_配置”中提供的字段名无效。选择是:(无)
我如何修复它,以及为什么我的查询不起作用,tks这里,
webhook\u配置
和monitor\u源
是混响关系,因此您必须使用
queryset=SlackConfigurationMode.objects\
.prefetch_相关('webhook_配置'、'monitor_源')\
.filter(mode='HIGH')
它可以工作,非常感谢你,但是我可以再问一个问题吗,我如何通过这个查询集从webhook配置和监视源中获取数据?
queryset = SlackConfigurationMode.objects \
.prefetch_related('webhook_configurations', 'monitor_sources') \
.filter(mode='HIGH')