Hello, everyone!
I want to make a template with a button, hooked up to the custom python function, so that when i clicked it i could access my function with the request and form.
For example,
I have this model:
class Statistic(Model):
"""Модель для просмотра статистики"""
id: AutoField = AutoField(primary_key=True)
work_name: CharField = CharField(verbose_name="Название работы")
user: ForeignKey = ForeignKey(
User,
on_delete=models.SET_NULL,
null=True,
verbose_name="Создатель",
)
department: ForeignKey = ForeignKey(
Department,
on_delete=models.SET_NULL,
null=True,
verbose_name="Департамент",
)
start_date: DateField = DateField(verbose_name="Начальная дата")
end_date: DateField = DateField(verbose_name="Конечная дата")
@property
def dates(self) -> tuple[date, date]:
return (self.start_date, self.end_date)
@property
def statistics(self) -> list[Model]:
return StatisticsService.get_filtered_objects(self.dates, self.user, self.department)
class Meta:
managed = False
verbose_name = "Статистика"
verbose_name_plural = verbose_name
db_table = "statistic"
The idea here, that user should get to its view-only page in the admin panel, fill the dates, department and user and press generate. After that i would get necessary objects and with the context push them to the template
My current half-attempt
def changelist_view(self, request, extra_context=None):
date_from = request.GET.get('date_from')
date_to = request.GET.get('date_to')
filtered_objects = StatisticsService.get_filtered_objects(
(date_from, date_to), request.user, request,department
)
I haven’t changed the template yet.
Any ideas how to do add custom button?
I understand that i would need to overwrite the ‘submit’ logic and somehow check the value / class of the pressed button, right?
If that’s so, how can i do it?
