【发布时间】:2021-04-17 19:53:30
【问题描述】:
尽管到处寻找类似的问题,但我仍然无法使用带有 Django ORM 的 INNER JOIN 进行查询...抱歉,如果这听起来很愚蠢,但这是我第一次使用 Django 进行项目,尤其是 ORM。
我有一个带有 Users 表(在我的例子中名为 Fellows)的 Articles 表,Articles 表的外键是作者,并引用 Fellows 表中的 user_id。
class Fellow(models.Model):
id = models.AutoField(db_column='ID', primary_key=True) # ID
user_id = models.PositiveBigIntegerField(db_column='User_ID', unique=True) # Global User ID.
nickname = models.CharField(db_column='Name', max_length=64, db_collation='utf8mb4_general_ci') # Display Name
user_password = models.CharField(db_column='User_Password', max_length=256, blank=True, null=True) # Passwd
gold = models.IntegerField(db_column='Gold') # Credits
faction = models.ForeignKey('Faction', models.RESTRICT, db_column='Faction', default=1) # ID Faction
class Meta:
managed = False
db_table = 'Fellows'
def __str__(self):
return self.nickname # Test.
class Article(models.Model):
id = models.AutoField(db_column='ID', primary_key=True) # ID
author = models.ForeignKey('Fellow', models.CASCADE, db_column='ID_User', default=1) # Global User ID
title = models.CharField(db_column='Title', max_length=32) # Title
content = models.TextField(db_column='Content') # Content
posted = models.DateTimeField(db_column='Posted') # Date Posted
source = models.CharField(db_column='Source', max_length=64, blank=True, null=True) # Source picture url of the article.
class Meta:
db_table = 'Articles'
我试图获取发布文章的相关作者的显示名称,但没有成功。
这是我的views.py:
from .models import Article
def index(request):
"""
And then Vue.JS will take care of the rest.
"""
# articles = Article.objects.order_by('-posted')[:5] # Returns everything inside Articles table but nothing inside Fellows table.
# articles = Article.objects.select_related() # No Result.
# Still can't get display_name in index.html with this one.
articles = Article.objects.raw('SELECT Fellows.Name AS Display_Name, Articles.ID, Articles.Title, Articles.Content, Articles.Posted, Articles.Source FROM Articles INNER JOIN Fellows ON Fellows.User_ID = Articles.ID_User ORDER BY Articles.ID DESC LIMIT 5;')
data = {
'articles': articles,
}
return render(request, 'home/index.html', data)
原始请求仅使用 sql 解释器返回一切正常,因此有两种选择:
- Django 不会执行 INNER JOIN。
- 我不知道如何读取模板 (index.html) 中的 Display_Name。
这就是我使用 VueJS 检索数据的方式(即使使用原始查询我也无法获得 display_name,它是空的)。
<script>
const store = new Vuex.Store({
state: {
articles: [
{% for article in articles %}
{
title: '{{ article.title }}',
content: '{{ article.content | linebreaksbr }}',
source: "{% static 'home/img/' %}" + '{{article.source}}',
display_name: '{{article.display_name}}', // Maybe this is not how to retrieve the display_name?
},
{% endfor %}
],
},
});
// Components.
ArticleList = Vue.component('article-list', {
data: function () { return { articles: store.state.articles } },
template: '#article-list-template',
});
ArticleItem = Vue.component('article-item', {
delimiters: ['[[', ']]'],
props: ['id', 'title', 'content', 'source', 'display_name'],
template: '#article-item-template',
});
...
</script>
如果有人可以帮助我,我将不胜感激! TT
【问题讨论】:
-
模板中的
Article.objects.select_related('author').all()和{{ article.author.nickname }}应该可以工作。您必须引用字段名称,而不是数据库列名称。 -
谢谢您的回复,我尝试了您建议的方法,但是它返回一个空列表。我已经使用具有外键的其他模型对另一个应用程序进行了测试,但这次使用了一个简单的模板(没有 vuejs),并且它按预期工作。这次我将尝试使用具有相同表格的虚拟应用程序来找出问题所在! x_x
-
请在代码问题中给出minimal reproducible example--cut & paste & runnable code,包括最小的代表性示例输入作为代码;期望和实际输出(包括逐字错误消息);标签和版本;明确的规范和解释。给出您可以给出的最少代码,即您显示的代码可以通过您显示的代码扩展为不正常。 (调试基础。)对于包含 DBMS 和 DDL(包括约束和索引)和输入为格式化为表的代码的 SQL。 How to Ask 停止尝试编写您的总体目标并从给定的代码中解释您的期望以及原因。
标签: django vue.js inner-join