简体   繁体   English

具有查询集的xDjango内部联接表

[英]xDjango Inner Join tables with queryset

I'm trying to INNER join 2 tables: Comments and Auth . 我正在尝试INNER加入2个表格: CommentsAuth So I store userid in Comments table and I need to pair it with the Auth . 因此,我将userid存储在Comments表中,并且需要将其与Auth配对。

I did with .raw() , but I don't want to do with the raw() , I have tried also other like get_objects_or_404 but that does not work either because of multiple queries. 我使用过.raw() ,但是我不想使用raw() ,我也尝试了其他类似get_objects_or_404类的get_objects_or_404但是由于多个查询而无法正常工作。

Here is my query, works as excepted. 这是我的查询,例外。

SELECT * FROM index_comment INNER JOIN auth_user WHERE index_comment.userid=auth_user.id

And here is my Comment model: 这是我的评论模型:

class Comment(models.Model):
content = models.TextField()
userid = models.IntegerField()
published = models.DateField(default=timezone.now)
postid = models.IntegerField()

Views.py Views.py

def readPost(request, postName):
content = get_object_or_404(Post, link=postName)
kategori = get_object_or_404(Category, id=content.category_id)
user = get_object_or_404(User, id=content.username_id)  


if request.method == "POST":
    form = sendComment(request.POST)
    if form.is_valid:
        formContent = strip_tags(request.POST["content"])
        newComment = Comment()
        newComment.content = formContent
        newComment.postid = content.id
        newComment.save()
        return redirect('post',content.link)


else:
    form = sendComment
    args = {
        "content": content,
        "kategori": kategori,
        "user":user,
        "commentForm": form,

    }

# return HttpResponse("cat.id")
    return render(request, "theme/single.html", args)

And here is the forms.py 这是forms.py

class sendComment(forms.Form):
content = forms.CharField(widget=forms.TextInput(attrs={"class":"form-control"}))

So I need to pair userid from Comments table to Auth id, and then get the username . 因此,我需要将Comments表中的userid与身份Auth ID配对,然后获取username

By setting up your models properly with a foreign key relationship ( django docs ) from index_comment.userid to auth_user.id, Django will handle the join for you and make the columns of the joined table(auth_user) available through the primary model (index_comment). 通过使用从index_comment.userid到auth_user.id的外键关系( django docs )正确设置模型,Django将为您处理联接并通过主模型(index_comment)使联接表(auth_user)的列可用。

Something like: 就像是:

from django.db import models
from django.contrib.auth.models import User

class Comment(models.Model):
    content = models.TextField()
    userid = models.ForeignKey(User,on_delete=models.CASCADE)
    published = models.DateField(default=timezone.now)
    postid = models.IntegerField()

will tie the userid in your comment table to the user table of django's built auth functionality. 会将注释表中的用户标识与django内置的auth功能的用户表绑定。 the name fields will be available to Comment like 名称字段将可用于评论,例如

comment.userid.username comment.userid.username

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

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