I have a django admin page which is for viewing purposes only and rather than displaying data from the model, it is displaying data for a table linked by a foreignkey to an intermediate table which is linked via a foreign key to my model. I want to apply a date range filter on the third table.
class Brand(models.Model):
data ...
class Organisation (models.Model):
data ...
brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
class DailyStat (models.Model):
stat_type = model.CharField(max_lenth=11, choices=STAT_TYPE_CHOICES
date = models.DateField()
organisation = models.ForeignKey(Organisation, on_delete=models.CASCADE)
I then created a change_form.html template in 'templates/admin/brand' which displays the data from DailyStat which I want for the Brand.
But I want to be able to filter this so I created a new form
class BrandAdminForm(forms.ModelForm):
from_date = forms.DateField(widget=admin.widgets.AdminDateWidget())
to_date = forms.DateField(widget=admin.widgets.AdminDateWidget())
class Meta:
model = Brand
fields = ['id','from_date','to_date']
And within the BrandAdmin definition, referenced it
class BrandAdmin(admin.ModelAdmin):
list_display = ['name','get_page_views','cqc_brand_id']
ordering = ['name']
search_fields = ['name']
form = BrandAdminForm
These fields didn't automatically show in the detail page so I added the following within the form tags of {% block content %}
of the change_form.html
<table style="width:60%">
<tr>
<td>From: </td>
<td>{{ adminform.form.from_date }}</td>
<td rowspan=2><button type="submit" value="Save and continue editing" class="btn viewsitelink">Filter</button></td>
</tr>
<tr>
<td>To: </td>
<td>{{ adminform.form.to_date }}</td>
</tr>
</table>
So the fields now show in the form (I haven't written the processing to use the fields yet) BUT, I am running django-cms and when I click the filter button, it isn't returning to the pages under the django-cms admin panel rather than returning to the admin view.
If there is a better approach, how can I get to filtering the data I require more effectively OR what am I doing wrong that it isn't returning to the correct view (the form tag shows action=""
)
Thanks