Answer the question
In order to leave comments, you need to log in
How to display a list of users in a template?
It is not possible to display all users who are owner in the template. It turns out only the first, one.
models.py
class Application(models.Model):
STATUS_CHOICES = (
('In the work', 'В работе'),
('New', 'Новая'),
('Complited', 'Завершена')
)
author = models.ForeignKey(User, related_name = 'author', null = True, on_delete=models.CASCADE)
title = models.CharField(max_length=50)
text = models.TextField()
room = models.CharField(max_length = 5)
published_date = models.DateField(blank=True, null=True, default = timezone.now)
status = models.CharField(max_length=15, choices=STATUS_CHOICES, default='New')
def __str__(self):
return self.title
class Executor(models.Model):
application = models.ForeignKey('Application', on_delete=models.CASCADE)
owner = models.ForeignKey(User, related_name = 'owner', null = True, blank = True,
on_delete=models.CASCADE, limit_choices_to={ 'groups__name': 'Техническая поддержка'})
class ExecutorInline(admin.TabularInline):
model = Executor
insert_after = 'status'
extra = 1
class ApplicationAdmin(admin.ModelAdmin):
inlines = [ExecutorInline]
admin.site.register(Application, ApplicationAdmin)
def application_detail(request, pk):
post = get_object_or_404(Application, pk=pk)
own = get_object_or_404(Executor, pk=request.user.id)
return render(request, 'helpdesk/application_detail.html', {'post': post, 'own': own})
<li>Исполнитель(и): {{own.owner.get_full_name}}
Answer the question
In order to leave comments, you need to log in
def application_detail(request, pk):
application = get_object_or_404(Application, pk=pk)
return render(request, 'helpdesk/application_detail.html', {'application': application})
<li>Исполнитель(и): {% for executor in application.executor_set.all %}{{ executor.owner.get_full_name }}{% if not forloop.last %}, {%endif %}{% endfor %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question