Como dice el título, tengo una vista detallada que estoy presentando a través de las plantillas de Django. Tengo una clave externa que también me gustaría presentar dentro de esa vista detallada y simplemente no puedo hacer que funcione. Lo he intentado todo, pero todo lo que obtengo es la plantilla de vista detallada básica sin información de clave externa. Cualquier ayuda sería muy apreciada.
Esto es lo que tengo hasta ahora:
Modelos:
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)Puntos de vista
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 contextPlantilla de detalle
<tbody> <ul> {% for p in watchman_group_slug.all %} <li>{{ watchman.computer_name }}</li> {% endfor %} </ul> </tbody>Accede a los objetos Watchman relacionados con:
<tbody> <ul> {% for p in object.watchman_group_slug.all %} <li>{{ p. computer_name }}</li> {% endfor %} </ul> </tbody> así con object. watchman_group_slug.all , y con el .computer_name de p .