I have three models:
class Entity(models.Model):
entity = models.CharField(primary_key=True, max_length=25)
class Report(models.Model):
report = models.CharField(max_length=50)
report_link = models.CharField(max_length=300)
class Item(models.Model):
entities = models.ManyToManyField(Entity, related_name='entities')
report = models.ForeginKey(Report)
A view is built off of the Item model:
def item_list(request):
items = Item.objects.select_related('report').prefetch_related('entities')
return render(request, 'items/item_list.html', {'items':items})
This view gets routed to a template:
{% for item in items %}
<tr>
<td>
{% for entity in item.entities.all %}
{{ entity }}{% if not forloop.last %}, {% endif %}
{% endfor %}
</td>
<td>{{ item.report }}</td>
<td>{{ item.report.report_link|urlize }}</td>
</tr>
{% endfor %}
This line (<td>{{ item.report.report_link|urlize }}</td>) manifests like this in the browser:
https://server.domain/reports/specificReport?entity=
How would I pass the entities into the URL to filter the report? The desired result would look like this:
https://server.domain/reports/specificReport?entity=12345
...or if there are multiple entities for one item:
https://server.domain/reports/specificReport?entity=12345,56789
Each report in the Report model has a link, but the link does not necessarily take the entity parameter, so it wouldn't be ideal to globally change all links (i.e. through jQuery or some other JS). However, there is JS running on this page, so it is possible to use that...although I think a Django option might be best.
One thing I've thought about is adding an indicator to the Report model that flags whether entities should be added to the link...but this doesn't solve the main problem of attaching one model field to the end of another and "urlizing" both of them together.