简体   繁体   English

如何在Django中获取外键的特定字段?

[英]How to get specific fields of a Foreign key in django?

So I've been learning django and I decided to make a blog with comments. 因此,我一直在学习django,因此决定创建一个带有评论的博客。 Here are my models: 这是我的模型:

class Author(models.Model):

    name = models.CharField(max_length = 20)
    email = models.EmailField(verbose_name = 'e-mail')

    def __str__(self):
        return self.name


class Post(models.Model):

    author = models.ForeignKey(Author)
    title = models.CharField(max_length=80)
    text = models.TextField()
    created_date = models.DateTimeField(
        default=timezone.now)
    published_date = models.DateTimeField(
        blank=True, null=True)

    def __str__(self):
        return self.title

class Comment(models.Model):

    author = models.OneToOneField(Author)
    post = models.ForeignKey(Post)
    text = models.TextField()
    post_date = models.DateTimeField(default = timezone.now)

    def __str__(self):
        return self.author + "commented"

Now,In my template,I can't access the name of the author.It just shiws the comment.{{comment.author.name}} doesn't work. 现在,在我的模板中,我无法访问作者的姓名。它只是隐藏了注释。{{comment.author.name}}不起作用。

Here's the template block. 这是模板块。

{% extends "base.html" %}

{% block content %}
<div class="post">
    {% if post.published_date %}
        <div class="date">
            {{ post.published_date }}
        </div>
    {% endif %}
    <a class="btn btn-default" href="{% url "post_edit" pk=post.pk %}"><span class="glyphicon glyphicon-pencil"></span></a>
    <h1>{{ post.title }}-{{post.author}}</h1>
    <p>{{ post.text|linebreaks }}</p>
</div>
{% for comment in comments %}
    <div class="post">
        <div class="date">
            {{ comment.post_date }}
        </div>
        <h4>Author:{{comment.author.name}}</h4>
        <h5>{{ comment.text|linebreaks }}</h5>
    </div>
{% endfor %}
<h3>New Comment</h3>
<form method="POST" class="post-form">{% csrf_token %}
    {{ form.as_p }}
    <button type="submit" class="save btn btn-default">Save</button>
</form>
{% endblock %}

ViewCode ViewCode

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            author = Author.objects.create()
            author.name = request.user
            author.email = ""
            comment.author = author
            comment.post = post
            comment.post_date = timezone.now()
            comment.save()
            return redirect('/posts/'+pk+'/')

    else:
        form = CommentForm()
    comments = Comment.objects.order_by('post_date')    
    return render(request, 'post_detail.html', {'post': post, 'comments':      comments, 'form': form})

The problem is here: 问题在这里:

author = Author.objects.create()
author.name = request.user
author.email = ""
comment.author = author

You create the author, but don't save the name and email. 您创建的作者,但不保存名称和电子邮件。

try this: 尝试这个:

author = Author.objects.create(name=request.user, email="")
comment.author = author

or this: 或这个:

author = Author.objects.create()
author.name = request.user
author.email = ""
author.save()
comment.author = author.pk

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

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