英文:
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="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}"
My view:
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")
And finally the HTML:
{%for d in LastUpdated%}
<p>{{d.manga}}</p>
{%endfor%}
答案1
得分: 1
你可以通过以下方式访问与chapter
实例相关联的Manga
模型的所有实例:
{% for i in LastUpdated %}
<p>{{i.chapter_number}}</p>
<h2> 下面是漫画模型的属性 </h2>
{% for j in i.manga_set.all %}
{{j.manga_name}}
{{j.manga_views}}
{% endfor %}
{% endfor %}
此外,你应该使用Chapter.objects.all().order_by("relase_date")
这个查询集,因为使用.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 %}
<p>{{i.chapter_number}}</p>
<h2> manga model attributes below</h2>
{% for j in i.manga_set.all %}
{{j.manga_name}}
{{j.manga_views}}
{% endfor %}
{% endfor %}
Also you should use Chapter.objects.all().order_by("relase_date")
this queryset as using .values()
gives you a key-value pair (dict).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论