Django динамически фильтрует объекты и отображает их в шаблоне ⇐ Python
-
Anonymous
Django динамически фильтрует объекты и отображает их в шаблоне
I produced a report that provides a count of how many musicians played each genre at a festival. the musician model has a genre field that is a foreign key to the genre table. I can successfully produce a template that renders this report, but it is hard coded in a way that is not ideal. I would like to be able to have users edit the genre table without me having to edit the view and template to account for new genres.
What direction can I take this where the genres played at a festival are identified dynamically, based on the musicians that played within the daterange of the festival, then passed as context to the template? See model, template, and view below:
Model:
class Festival(models.Model): id = models.AutoField(db_column='Id', primary_key=True, blank=True, null=False) number = models.TextField(db_column='Number', blank=True, null=True) startdate = models.DateField(db_column='StartDate', blank=True, null=True) enddate = models.DateField(db_column='EndDate', blank=True, null=True) class Meta: managed = True db_table = 'Festival' verbose_name_plural = 'Festival' class Musician(models.Model): id = models.AutoField(db_column='Id', primary_key=True, blank=True, null=False) startdate = models.DateTimeField(db_column='StartDate', null=False) enddate = models.DateTimeField(db_column='EndDate', blank=True, null=True) genre = models.ForeignKey('Genre', models.DO_NOTHING, db_column='GenreId', null=True) class Meta: managed = True db_table = 'Musician' verbose_name_plural = 'Musician' class Genre(models.Model): id = models.AutoField(db_column='Id', primary_key=True, blank=True, null=False) name = models.TextField(db_column='Name', blank=True, null=True) class Meta: managed = True db_table = 'Genre' verbose_name_plural = 'Genre' view:
def festivalreport(request, pk): #festival object and musicians by festival daterange festival=Festival.objects.filter(id=pk) festival_object=festival.last() startdate=festival_object.startdate enddate=festival_object.enddate musician=Musician.objects.filter(startdate__range=[startdate, enddate]) #genre 1 calculations musician_genre1=musician.filter(genre='1') musician_genre1_count=musician_genre1.count() #genre 2 calculations musician_genre2=musician.filter(genre='2') musician_genre2_count=musician_genre2.count() #genre 3 calculations musician_genre3=musician.filter(genre='3') musician_genre3_count=musician_genre3.count() context ={ 'festival':festival, 'musician': musician, 'musician_genre1_count':musician_genre1_count, 'musician_genre2_count':musician_genre2_count, 'musician_genre3_count':musician_genre3_count, } return render(request, "wwdb/reports/festivalreport.html", context) Template:
{% extends "wwdb/base.html" %} {% block content %} {% for f in festival %} {{f.number}} Festival Report start date: {{f.startdate}}
end date: {{f.enddate}}
{% endfor %} Genre Report Genre 1 {% if musician_genre1_count %}
Genre 1 musician count: {{musician_genre1_count}}
{% else %}
No musicians played genre 1
{% endif %} Genre 2 {% if musician_genre2_count %}
Genre 2 musician count: {{musician_genre2_count}}
{% else %}
No musicians played genre 2
{% endif %} Genre 3 {% if musician_genre3_count %}
Genre 2 musician count: {{musician_genre3_count}}
{% else %}
No musicians played genre 3
{% endif %} {% endblock content %}
Источник: https://stackoverflow.com/questions/780 ... n-template
I produced a report that provides a count of how many musicians played each genre at a festival. the musician model has a genre field that is a foreign key to the genre table. I can successfully produce a template that renders this report, but it is hard coded in a way that is not ideal. I would like to be able to have users edit the genre table without me having to edit the view and template to account for new genres.
What direction can I take this where the genres played at a festival are identified dynamically, based on the musicians that played within the daterange of the festival, then passed as context to the template? See model, template, and view below:
Model:
class Festival(models.Model): id = models.AutoField(db_column='Id', primary_key=True, blank=True, null=False) number = models.TextField(db_column='Number', blank=True, null=True) startdate = models.DateField(db_column='StartDate', blank=True, null=True) enddate = models.DateField(db_column='EndDate', blank=True, null=True) class Meta: managed = True db_table = 'Festival' verbose_name_plural = 'Festival' class Musician(models.Model): id = models.AutoField(db_column='Id', primary_key=True, blank=True, null=False) startdate = models.DateTimeField(db_column='StartDate', null=False) enddate = models.DateTimeField(db_column='EndDate', blank=True, null=True) genre = models.ForeignKey('Genre', models.DO_NOTHING, db_column='GenreId', null=True) class Meta: managed = True db_table = 'Musician' verbose_name_plural = 'Musician' class Genre(models.Model): id = models.AutoField(db_column='Id', primary_key=True, blank=True, null=False) name = models.TextField(db_column='Name', blank=True, null=True) class Meta: managed = True db_table = 'Genre' verbose_name_plural = 'Genre' view:
def festivalreport(request, pk): #festival object and musicians by festival daterange festival=Festival.objects.filter(id=pk) festival_object=festival.last() startdate=festival_object.startdate enddate=festival_object.enddate musician=Musician.objects.filter(startdate__range=[startdate, enddate]) #genre 1 calculations musician_genre1=musician.filter(genre='1') musician_genre1_count=musician_genre1.count() #genre 2 calculations musician_genre2=musician.filter(genre='2') musician_genre2_count=musician_genre2.count() #genre 3 calculations musician_genre3=musician.filter(genre='3') musician_genre3_count=musician_genre3.count() context ={ 'festival':festival, 'musician': musician, 'musician_genre1_count':musician_genre1_count, 'musician_genre2_count':musician_genre2_count, 'musician_genre3_count':musician_genre3_count, } return render(request, "wwdb/reports/festivalreport.html", context) Template:
{% extends "wwdb/base.html" %} {% block content %} {% for f in festival %} {{f.number}} Festival Report start date: {{f.startdate}}
end date: {{f.enddate}}
{% endfor %} Genre Report Genre 1 {% if musician_genre1_count %}
Genre 1 musician count: {{musician_genre1_count}}
{% else %}
No musicians played genre 1
{% endif %} Genre 2 {% if musician_genre2_count %}
Genre 2 musician count: {{musician_genre2_count}}
{% else %}
No musicians played genre 2
{% endif %} Genre 3 {% if musician_genre3_count %}
Genre 2 musician count: {{musician_genre3_count}}
{% else %}
No musicians played genre 3
{% endif %} {% endblock content %}
Источник: https://stackoverflow.com/questions/780 ... n-template