[英]Django Pass Model Field as Parameter to URL Stored in Another Model
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: 视图是基于Item
模型构建的:
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: 这行( <td>{{ item.report.report_link|urlize }}</td>
)在浏览器中显示如下:
https://server.domain/reports/specificReport?entity=
How would I pass the entities
into the URL to filter the report? 如何将entities
传递到URL中以过滤报告? The desired result would look like this: 所需的结果如下所示:
https://server.domain/reports/specificReport?entity=12345
...or if there are multiple entities
for one item
: ...或者一个item
有多个entities
:
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 (ie through jQuery or some other JS). Report
模型中的每个报表都有一个链接,但是该链接不一定采用实体参数,因此全局更改所有链接(即通过jQuery或其他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. 但是,此页面上正在运行JS,因此可以使用它...尽管我认为Django选项可能是最好的。
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. 我考虑过的一件事是在Report
模型中添加一个指示器,用于标记是否应将entities
添加到链接中……但这并不能解决将一个模型字段附加到另一个模型字段的最后一个主要问题。 “他们俩在一起。
How about a model method to create the link? 创建链接的模型方法怎么样? The Django docs mention the following about using model methods, which is applicable to your use case Django文档提到以下有关使用模型方法的内容,适用于您的用例
This is a valuable technique for keeping business logic in one place – the model. 这是将业务逻辑放在一个位置(模型)中的宝贵技术。
Meaning you don't need to dig though your templates and template tags to see how an Item's report link is generated -- you can see it all in models.py 这意味着您无需挖掘模板和模板标签即可查看如何生成项目的报告链接-您可以在models.py中查看所有内容
Something along the following could be a place to start 以下内容可能是一个起点
class Item(models.Model):
entities = models.ManyToManyField(Entity, related_name='entities')
report = models.ForeginKey(Report)
def get_report_link(self):
link_text = self.report.report_link
if self.entities.count() > 0:
link_text + "?entity={}".format(','.join([entity.id for entity in self.entities.all()]))
return link_text
and then in your template: change item.report.report_link|urlize
to item.get_report_link
然后在您的模板中:将item.report.report_link|urlize
更改为item.get_report_link
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.