【发布时间】:2015-07-30 23:25:46
【问题描述】:
我有两个模型通过一个不是主键的字段在逻辑上相关。是否可以在不引入ForeignKey 列的情况下查询它们(例如select_related(…))?
例如,考虑人为的模型:
class LogEntry(Model):
source_name = CharField(…)
log_message = CharField(…)
class LogSource(Model):
name = CharField(…)
domain = CharField(…)
我希望能够查询LogEntry,加入并过滤相关的LogSource(例如,我可以访问log_entry.source而无需额外查询):
LogEntry.objects
.select_related(
source=Join(LogSource, on="logsource.name = logentry.source_name")),
)
.filter(source__domain="example.com")
这是否可能不引入 ForeignKey?
【问题讨论】:
标签: django django-models