如何 return 子对象名称以及 django 中的父对象详细信息
How to return child object names along with parent details in django
我有两个模型 Destination 和 Cruise
#Models
class Destination(models.Model):
name = models.CharField(unique= True, null= False, blank=False, max_length= 50)
description = models.TextField(max_length= 2000, null= False, blank=False)
slug = models.SlugField()
def __str__(self) -> str:
return self.name
class Cruise(models.Model):
name = models.CharField(unique= True, null= False, blank=False, max_length= 50)
description = models.TextField(max_length= 2000, null= False, blank=False)
destinations = models.ForeignKey("Destination", null=True, blank=True, on_delete=models.SET_NULL)
我正在尝试显示目的地详细信息以及该目的地下的所有邮轮。
#Views
class DestinationDetailView(DetailView):
template_name = 'destination_detail.html'
queryset = models.Destination.objects.all()
context_object_name = "destination"
HTML
#html
{% block content %}
Destination Name = {{destination.name}}
Description = {{destination.description}}
Cruise = {{destination.cruises}}
{% blockend content %}
请告知如何打印perticual destination下的所有游轮。
我估计你想在你身上展示一些邮轮领域的内容html。
您可以按照 here 所述使用 for
模板标签,例如:
#html
{% block content %}
Destination Name = {{destination.name}}
Description = {{destination.description}}
Cruise =
{% for cruise in destination.cruise_set.all %}
{{ cruise.name }} </br>
{% endfor %}
{% blockend content %}
这通过在 HTML 中添加 for 循环解决了我的问题。
#html
{% block content %}
Destination Name = {{destination.name}}
Description = {{destination.description}}
Cruise =
{% for cruise in destination.cruises.all %}
{{ cruise.name }} </br>
{% endfor %}
{% blockend content %}
我有两个模型 Destination 和 Cruise
#Models
class Destination(models.Model):
name = models.CharField(unique= True, null= False, blank=False, max_length= 50)
description = models.TextField(max_length= 2000, null= False, blank=False)
slug = models.SlugField()
def __str__(self) -> str:
return self.name
class Cruise(models.Model):
name = models.CharField(unique= True, null= False, blank=False, max_length= 50)
description = models.TextField(max_length= 2000, null= False, blank=False)
destinations = models.ForeignKey("Destination", null=True, blank=True, on_delete=models.SET_NULL)
我正在尝试显示目的地详细信息以及该目的地下的所有邮轮。
#Views
class DestinationDetailView(DetailView):
template_name = 'destination_detail.html'
queryset = models.Destination.objects.all()
context_object_name = "destination"
HTML
#html
{% block content %}
Destination Name = {{destination.name}}
Description = {{destination.description}}
Cruise = {{destination.cruises}}
{% blockend content %}
请告知如何打印perticual destination下的所有游轮。
我估计你想在你身上展示一些邮轮领域的内容html。
您可以按照 here 所述使用 for
模板标签,例如:
#html
{% block content %}
Destination Name = {{destination.name}}
Description = {{destination.description}}
Cruise =
{% for cruise in destination.cruise_set.all %}
{{ cruise.name }} </br>
{% endfor %}
{% blockend content %}
这通过在 HTML 中添加 for 循环解决了我的问题。
#html
{% block content %}
Destination Name = {{destination.name}}
Description = {{destination.description}}
Cruise =
{% for cruise in destination.cruises.all %}
{{ cruise.name }} </br>
{% endfor %}
{% blockend content %}