使用过滤器将表单提交到 django-admin 模型
我想要一个自定义的 html 表单代码片段,允许用户选择一个“训练”,然后将其用作“参与者”的 django-admin 模型过滤器的查询参数。
我已经在 modeladmin 上成功创建了过滤器:
class ParticipantAdmin(RestrictedModelAdmin):
list_filter = ('training__name',)
可能值得注意的是 RestrictedModleAdmin 是 ModelAdmin 的子类,它为模型提供行级安全性;登录的用户应该只能看到他们拥有的行。
因此,当仅使用该管理界面时,使用此过滤器的网址看起来像这样:
/admin/core/participant/?training__name=Menno+Ropes
一切都很好。现在我想我应该能够创建一个非常简单的表单,允许选择有效的“训练”并将其作为 GET 提交到 /admin/core/participant/ 。
<form method="GET" action="/admin/core/participant/">{% csrf_token %}
<ol>
<li>Select your training:
<select name='training__name'>
<option value=''>—</option>
{% for training in trainings %}
<option value='{{ training.name }}'>{{ training }}</option>
{% endfor %}
</select>
</li>
<li>See participants for that training.
<input type='submit' name='submit' value='Submit' /></li>
</ol>
</form>
最后一点似乎不起作用。 django 内部的一些神奇的 foo 似乎总是将提交弄乱:
/admin/core/participant/?e=1
这显然没有选择适当的过滤器值,因此显示了未经过滤的“参与者”列表。
这是怎么回事?我该怎么做才能让它允许我的 GET 参数传递到管理模型?
提前致谢。
PS) 姜戈 1.3+
I would like to have a custom snippet of html form code that takes allows the user to select a 'training' that is then used as a query parameter to a django-admin model filter for 'participants'.
I've successfully created the filter on the modeladmin:
class ParticipantAdmin(RestrictedModelAdmin):
list_filter = ('training__name',)
It's probably worth noting that RestrictedModleAdmin is a subclass of ModelAdmin that provides row-level security for the model; logged in users should only see rows they own.
Thus, urls using this filter look something like this when just using that admin interface:
/admin/core/participant/?training__name=Menno+Ropes
All that works great. Now I think I should be able to create a very simple form that allows selecting a valid 'training' and submitting that to /admin/core/participant/ as a GET.
<form method="GET" action="/admin/core/participant/">{% csrf_token %}
<ol>
<li>Select your training:
<select name='training__name'>
<option value=''>—</option>
{% for training in trainings %}
<option value='{{ training.name }}'>{{ training }}</option>
{% endfor %}
</select>
</li>
<li>See participants for that training.
<input type='submit' name='submit' value='Submit' /></li>
</ol>
</form>
This last bit doesn't see to work. Some magic foo in the django innards seems to always mangle the submission to be:
/admin/core/participant/?e=1
This obviously doesn't select the appropriate filter value and thus shows an unfiltered list of 'participants'.
What's going on? What can I do to get it to allow my GET parameter to pass through to the admin model?
Thanks in advance.
PS) Django 1.3+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您的
中有一个
name
属性,导致出现额外的 GET 参数:submit
这会引发无效查找错误,因此e=1
删除
name
属性就可以了。我做了一个小实验来确认,因为我认为服务器可能以某种方式以不同的方式对待浏览器 GET 很奇怪。
The problem is that you have a
name
attribute in your<input type="submit">
, causing an extra GET parameter:submit
which is throwing the invalid lookup error and thuse=1
Remove the
name
attribute and you're good to go.I did a little experiment to confirm since I thought it odd that the server might somehow treat a browser GET differently.
这有点棘手,但对我有用:
It's a little bit tricky, but it works for me: