本文介绍了在 Django 的 Admin 之外使用 filter_horizontal 的最简单方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个非管理员表单,我想在其中使用 filter_horizontal.我读过 this不仅仅是我想要的(我只想要 filter_horizontal).我想看看是否有人想出了一种更简单(更当前)的方法来实现 filter_horizontal.
代码如下:
class County(models.Model):"""县名"""name = models.CharField(max_length=64)state = USStateField(null=True)类公司(模型.模型):"""公司的基本知识"""名称 = 模型.CharField(max_length = 100)县 = 模型.ManyToManyField(县,空白=真,空=真)
那么我们的表单目前看起来是这样的.我以为这会奏效..
from django.contrib.admin.widgets import FilteredSelectMultiple类评级公司表格(模型表格):元类:模型 = 评级组织exclude = ('remrate_projects',)widgets = {'counties': FilteredSelectMultiple(verbose_name="Counties",is_stacked=True,) }课堂媒体:css = {'all':['admin/css/widgets.css']}js = ['/admin/jsi18n/']
顺便说一句:我知道这可能是 this 的重复,但他的问题不是没有回答.我在这里和这里 但这些似乎都不起作用.
解决方案
我知道这个帖子很旧,但希望这个信息能帮助像我一样偶然发现这个页面的其他人.
在经历了很多痛苦和磨难之后,我能够让它与 Django 1.4 一起工作.像 rh0dium 一样,我尝试了所有这些文章,但不得不进行很多调整.
您不必对 ModelForm 做任何特别的事情,但您必须在模板中包含所有这些 js 和 css 文件:
<script type="text/javascript" src="/admin/jsi18n/"></script><script type="text/javascript" src="{{ STATIC_URL }}admin/js/core.js"></script><script type="text/javascript" src="{{ STATIC_URL }}admin/js/jquery.min.js"></script><script type="text/javascript" src="{{ STATIC_URL }}admin/js/jquery.init.js"></script><script type="text/javascript" src="{{ STATIC_URL }}admin/js/SelectFilter2.js"></script><script type="text/javascript" src="{{ STATIC_URL }}admin/js/SelectBox.js"></script><link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}admin/css/widgets.css"/><link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}admin/css/base.css"/><link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}admin/css/forms.css"/>
然后像往常一样渲染表单,但您需要获取正确的字段集元素和类名,以便 css 正常工作.例如:
<div class="form-row"><form method="post" action=".">{% csrf_token %}{{ form.as_p }}<button type="submit" value="submit">添加</button></表单>
</fieldset>
然后在模板的底部(在呈现表单的标记之后),添加此脚本并将价格标签替换为模型表单模型上的多对多 (M2M) 关系名称:
显然您的媒体位置可能有所不同,但 {{ STATIC_URL }}admin/为我工作.
I have a non-admin form in which I'd like to use filter_horizontal on. I have read this which does much more than what I want (I only want the filter_horizontal). I wanted to check to see if anyone has come up with a simpler (more current) way to just implement the filter_horizontal.
So here is the code:
class County(models.Model):
"""County Names"""
name = models.CharField(max_length=64)
state = USStateField(null=True)
class Company(models.Model):
"""The basics of a company"""
name = models.CharField(max_length = 100)
counties = models.ManyToManyField(County,blank=True, null=True)
Then our form currently look like this. I thought this would work..
from django.contrib.admin.widgets import FilteredSelectMultiple
class RaterCompanyForm(ModelForm):
class Meta:
model = RaterOrganization
exclude = ('remrate_projects',)
widgets = {'counties': FilteredSelectMultiple(verbose_name="Counties",
is_stacked=True,) }
class Media:
css = {'all':['admin/css/widgets.css']}
js = ['/admin/jsi18n/']
BTW: I understand this may be a duplicate of this but his question wasn't answered. I've done plenty of homework here and here but neither of these appear to work.
解决方案
I know this thread is old, but hopefully this info will help someone else who stumbles upon this page like I did.
After much pain and suffering, I was able to get this to work with Django 1.4. Like rh0dium, I tried all those articles, but had to make a lot of tweaks.
You don't have to do anything special with the ModelForm, but you do have to include all these js and css files in the template:
<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/core.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/jquery.min.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/jquery.init.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/SelectFilter2.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/SelectBox.js"></script>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}admin/css/widgets.css"/>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}admin/css/base.css"/>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}admin/css/forms.css"/>
Then render the form as you normally would, but you need to get the fieldset elements and class names right for the css to work. For example:
<fieldset>
<div class="form-row">
<form method="post" action=".">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" value="submit">Add</button>
</form>
</div>
</fieldset>
Then at the BOTTOM of the template (after the markup to render the form), add this script and replace pricetags with whatever your Many to Many (M2M) relationship name is on the model form's model:
<script type="text/javascript">
addEvent(window, "load", function(e) { SelectFilter.init("id_pricetags", "pricetags", 0, "{{ STATIC_URL }}admin/"); });
</script>
Apparently your media location may be something different, but {{ STATIC_URL }}admin/ worked for me.
这篇关于在 Django 的 Admin 之外使用 filter_horizontal 的最简单方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!