在Django Detail View Template中显示外键信息



正如标题所说,我通过Django模板展示了一个详细的视图。我有一把外国钥匙,我也想在详细的视图中展示,但我就是无法使用它。我已经尝试了所有的东西,但我得到的只是基本的详细视图模板,没有外键信息。如有任何帮助,我们将不胜感激。

到目前为止,我得到的是:

型号:

class Cust(models.Model): #this is the main model
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False)
email = models.CharField(max_length=200)
firstName = models.CharField(max_length=200)
lastName = models.CharField(max_length=200)
watchmanSlug = models.CharField(max_length=200, unique=True)
class Watchman(models.Model):
group = models.ForeignKey(Cust, on_delete=models.CASCADE,to_field='watchmanSlug', 
related_name='watchman_group_slug')
uid = models.CharField(max_length=500)
computer_name = models.CharField(max_length=500)
computer_url = models.CharField(max_length=500)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

视图

class CustomerDetailView(DetailView):
model = Cust
template_name = 'cust/cust_detail.html'

def get_context_data(self, ** kwargs):
context = super(CustomerDetailView, self).get_context_data( ** kwargs)
context['computer_name'] = Watchman.objects.all()

return context

详细模板


<tbody>
<ul>
{% for p in watchman_group_slug.all %}
<li>{{ watchman.computer_name }}</li>
{% endfor %}

</ul>
</tbody>

您使用访问相关的Watchman对象

<tbody>
<ul>
{% for p inobject.watchman_group_slug.all%}
<li>{{p.computer_name }}</li>
{% endfor %}
</ul>
</tbody>

object.watchman_group_slug.allp.computer_name也是如此。

最新更新