I have two models, field of one of them pointing to the other as shown below:
class Group(models.Model):
group_company_id = models.CharField(primary_key=True, ...)
class Company(models.Model):
company_id = models.CharField(primary_key=True, ...)
group_company = models.ForeignKey(Group, related_name="related_grp_company", ...)
I am trying to get all the Companies that have been created for a particular Group. So I am trying to get the company_id
(and other) values in Djnago UpdateView
as a list in the template. My CBV is as shown:
class GroupCompanyChangeView(UpdateView):
template_name = ...
model = Group
form_class = ...
success_url = reverse_lazy('group_list')
grp_coy_units = Group.objects.prefetch_related('related_grp_company') # I am trying to get the values of `company_id` in the template but nothing is displayed.
Could somebody please let me know how to get this to work?
Update
As explained (@Mahmoud Adel), I have modified my UpdateView
as shown below:
class GroupCompanyChangeView(UpdateView):
template_name = ...
model = Group
form_class = ...
success_url = reverse_lazy('group_list')
def get_object(self, *args, **kwargs):
return Group.objects.get(pk=self.kwargs['pk'])
And then in the template, I am doing:
{{ group.related_grp_company }}
With this I am getting an output of <app>.Company.None
.