Answer the question
In order to leave comments, you need to log in
How to group data from another django model?
Good day, wise ones! There are models Protocol, Applicant, Type of assistance. The applicant is tied to a specific protocol and in turn to a specific type of assistance. Through the slug we get the page of a specific protocol, and the applicants related to this protocol. It is necessary to display a list of applicants grouped by type of assistance. The output should be like this:
1. Material:
-applicant;
- the applicant;
2. Pat on the shoulder:
-applicant;
- the applicant;
models.py
class Protocol(BaseModel):
"""Протокол"""
title = models.CharField(
_('Название'), max_length=255, db_index=True, blank=True,
help_text=_('Обязательное поле')
)
slug = models.SlugField(
_('Алиас'), max_length=150, db_index=True, unique=True,
help_text=_('Латинские буквы и цифры')
)
date = models.DateTimeField(_('Дата протокола'), default=utcnow)
class Meta:
verbose_name = _('Протокол')
verbose_name_plural = _('Протоколы')
def __str__(self):
return self.title
class HelpType(BaseModel):
"""Тип помощи"""
title = models.CharField(
_('Название'), max_length=255, db_index=True, blank=True,
help_text=_('Обязательное поле')
)
class Meta:
verbose_name = _('Тип помощи')
verbose_name_plural = _('Типы помощи')
def __str__(self):
return self.title
class Applicant(BaseModel):
"""Заявитель"""
name = models.CharField('ФИО', max_length=255, help_text=_('Обязательное поле'))
position = models.CharField('Должность', max_length=255, null=True, blank=True)
cause = models.CharField('Причина', max_length=255, null=True, blank=True)
in_fond = models.CharField('Время пребывания в фонде', max_length=255, null=True, blank=True)
summ = models.IntegerField('Сумма ходатайства', null=True, blank=True)
summ_text = models.CharField('Сумма ходатайства прописью', max_length=255, null=True, blank=True)
protocol = models.ForeignKey(
'Protocol', verbose_name=(_('Протокол')), related_name='applicants', on_delete=models.CASCADE,
blank=True, null=True
)
help_type = models.ForeignKey(
'HelpType', verbose_name=(_('Тип помощи')), related_name='applicants', on_delete=models.CASCADE,
blank=True, null=True
)
class Meta:
verbose_name = 'Заявитель'
verbose_name_plural = 'Заявители'
ordering = ('ordering',)
def __str__(self):
return self.name
class ProtocolView(BaseTemplateView):
"""Страница протокола"""
template_name = 'protocols/protocol.html'
def get_context_data(self, **kwargs):
kwargs = super(ProtocolView, self).get_context_data(**kwargs)
current_page = get_object_or_404(
models.Protocol.objects.published(), slug__exact=kwargs.get('slug')
)
applicants = models.Applicant.objects.published().filter(protocol=current_page.pk)
kwargs.update(
current_page=current_page,
applicants=applicants,
)
return kwargs
Answer the question
In order to leave comments, you need to log in
Briefly: sort the list of applicants by type of assistance and use the groupby tag in the template
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question