Django ForeignKey属性未显示在HTML中。

huangapple go评论65阅读模式
英文:

Django ForeignKey attribute is not showing up in HTML

问题

以下是您的代码的翻译部分:

我有一些模型它们相互连接在我的视图中我按发布日期对*Chapter*模型进行排序然后删除与其他模型关联的相同*Manga*外键关联的章节

但显然除了接受字段外它与任何内容都没有连接我无法访问模型字段但也因为我正在使用查询集我无法按ID或其他方式进行排序另外我必须访问与章节相关的*Manga*模型字段但它不会显示在HTML中我该怎么做

我的模型

```python
class Manga(models.Model):
    manga_name = models.CharField(max_length=255, default="null")
    manga_image = models.ImageField(upload_to="thumbnail", default="thumbnail/noimage.png")
    manga_views = models.IntegerField(default=0)

    def __str__(self):
        return f"{self.id}, {self.manga_name}"

class Fansub(models.Model):
    fansub_name = models.CharField(max_length=255, default="null")

    def __str__(self):
        return f"{self.id}, {self.fansub_name}"

class Chapter(models.Model):
    chapter_number = models.IntegerField(default=0)
    chapter_url = models.URLField(default="www.example.com")
    manga = models.ForeignKey(Manga, on_delete=models.CASCADE)
    fansub = models.ForeignKey(Fansub, on_delete=models.CASCADE)
    relase_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.chapter_number}, {self.manga}, {self.fansub}"

我的视图:

def Index(request):
    manga = Manga.objects.all()
    chapter = Chapter.objects.all().values()
    fansub = Fansub.objects.all().values()

    mostview = manga.order_by("-manga_views")[:5]

    relasedateorder = chapter.order_by("relase_date")

    context = {
        "Manga": manga,
        "Chapter": chapter,
        "Fansub": fansub,
        "Mostview": mostview,
        "LastUpdated": relasedateorder,
    }

    template = loader.get_template("Index.html")

最后是HTML部分:

{% for d in LastUpdated %}
    <p>{{ d.manga }}</p>
{% endfor %}

希望这可以帮助您理解代码的结构和内容。

英文:

I have some models and connected each other. And in my views I get the Chapter model ordered by date published, and then remove the ones with same Manga (foreign key associated with other model).

But apparently accept for the fields is not connected with anything, and I can't access the models fields. But also because I am working with a query set I can't sort by id or anything. Also, I have to access the Manga models fields associated with chapter. But it does not show up in the HTML. How can I do it?

My models:

 class Manga(models.Model):
    manga_name = models.CharField(max_length=255, default=&quot;null&quot;)
    manga_image = models.ImageField(upload_to=&quot;thumbnail&quot;, default=&quot;thumbnail/noimage.png&quot;)
    manga_views = models.IntegerField(default=0)

    def __str__(self):
        return f&quot;{self.id}, {self.manga_name}&quot;


class Fansub(models.Model):
    fansub_name = models.CharField(max_length=255, default=&quot;null&quot;)

    def __str__(self):
        return f&quot;{self.id}, {self.fansub_name}&quot;

class Chapter(models.Model):
    chapter_number = models.IntegerField(default=0)
    chapter_url = models.URLField(default=&quot;www.example.com&quot;)
    manga = models.ForeignKey(Manga, on_delete=models.CASCADE)
    fansub = models.ForeignKey(Fansub, on_delete=models.CASCADE)
    relase_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f&quot;{self.chapter_number}, {self.manga}, {self.fansub}&quot;

My view:

def Index(request):
manga = Manga.objects.all()
chapter = Chapter.objects.all().values()
fansub = Fansub.objects.all().values()

mostview = manga.order_by(&quot;-manga_views&quot;)[:5]

relasedateorder = chapter.order_by(&quot;relase_date&quot;)

context = {
    &quot;Manga&quot;: manga,
    &quot;Chapter&quot;: chapter,
    &quot;Fansub&quot;: fansub,
    &quot;Mostview&quot;: mostview,
    &quot;LastUpdated&quot;: relasedateorder,
}

template = loader.get_template(&quot;Index.html&quot;)

And finally the HTML:

{%for d in LastUpdated%}
    &lt;p&gt;{{d.manga}}&lt;/p&gt;
{%endfor%}

答案1

得分: 1

你可以通过以下方式访问与chapter实例相关联的Manga模型的所有实例:

{% for i in LastUpdated %}
    &lt;p&gt;{{i.chapter_number}}&lt;/p&gt;
    &lt;h2&gt; 下面是漫画模型的属性 &lt;/h2&gt;
    {% for j in i.manga_set.all %}
        {{j.manga_name}}
        {{j.manga_views}}
    {% endfor %}    
{% endfor %}

此外,你应该使用Chapter.objects.all().order_by(&quot;relase_date&quot;)这个查询集,因为使用.values()会给你一个键值对(字典)。

英文:

You can access the all the instances of Manga model associated with chapter instance by using _set as suffix in the following way:

{% for i in LastUpdated %}
    &lt;p&gt;{{i.chapter_number}}&lt;/p&gt;
    &lt;h2&gt; manga model attributes below&lt;/h2&gt;
    {% for j in i.manga_set.all %}
        {{j.manga_name}}
        {{j.manga_views}}
    {% endfor %}    
{% endfor %}

Also you should use Chapter.objects.all().order_by(&quot;relase_date&quot;) this queryset as using .values() gives you a key-value pair (dict).

huangapple
  • 本文由 发表于 2023年2月7日 01:33:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定