Django中的ModelForm验证自定义自动完成,而不是难看多选的M2M, [英] Django ModelForm Validate custom Autocomplete for M2M, instead of ugly Multi-Select

查看:125
本文介绍了Django中的ModelForm验证自定义自动完成,而不是难看多选的M2M,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下车型(上剪下来的理解):

Given the following models (cut down for understanding):

class Venue(models.Model):
    name = models.CharField(unique=True)

class Band(models.Model):
    name = models.CharField(unique=True)

class Event(models.Model):    
    name = models.CharField(max_length=50, unique=True)       
    bands = models.ManyToManyField(Band) 
    venue = models.ForeignKey(Venue)
    start = models.DateField()
    end = models.DateField()

管理方面的伟大工程,为我在做什么,但我想开网站了一下,让某些用户可以添加新的活动。对于公共部分,我对这些模型,我不希望公众看到(这是很容易的修复)几个行政领域。

The admin area works great for what I'm doing, but I'd like to open the site up a bit so that certain users can add new Events. For the public portions, I have several "administrative" fields on these models that I don't want the public to see (which is easy enough to fix).

我的具体问题,虽然,创建一个新的事件时,改变了多对多的选择的显示。因为乐队可以列出事件的数量不应该一起为多选箱送,我想用处理倍数为自动完成(如标签中,在这里计算器!)。

My specific problem, though, is changing the display of the ManyToMany selections when creating a new Event. Because the number of Bands possible to list for an event should not be sent along as a multiselect box, I'd like to use an AutoComplete that handles multiples (like the Tags box, here on StackOverflow!).

我有这部分工作,它与Band.id的用逗号一个值分隔的隐藏输入正确填写。 然而,我不知道如何放在一起让Django的使用 ModelForms 做了验证,并以某种方式也验证乐队的选择。

I have this part working, and it correctly fills in a hidden input with the Band.id's separated by commas for a value. However, I can't understand how to put together letting Django do the validation using the ModelForms, and somehow also validating the 'Bands' selection.

在理想情况下,我想自动完成喜欢这里StackOverflow上的标签,并沿着所选择的波段发送ID的某种分隔字符串的 - 同时让Django的验证通过的带存在,等等,好像我离开恼人的多选列表到位。

Ideally, I want to auto-complete like the tags here on StackOverflow, and send along the selected Bands ID's in some kind of Delimited string - all while letting Django validate that the bands passed exist, etc, as if I left the annoying multi-select list in place.

我一定要创建一个模型或模特我自己的自动完成字段类型和使用?有没有别的东西我俯瞰?

Do I have to create my own Auto-Complete Field type for a form or model, and use that? Is there something else I'm overlooking?

我看到了一些现有的自动完成窗口小部件,但我真的,真的,真的很喜欢用我自己的自动完成code,因为它已经成立,其中一些看起来有点令人费解。

有很多更多的文本/解释在这里,但我切回,因为我避免华尔街的文字。如果我离开重要的东西出来,让我知道。

推荐答案

这是一个有点硬不知道到底是什么你自动完成code是做这么说,但只要发送带的IDS像他们将与发送的<选择方式> 时,应该的ModelForm验证它们像往常一样

It's a little hard to say without knowing exactly what your autocomplete code is doing, but as long as it is sending the ids of the bands like they would be sent with the <select>, the ModelForm should validate them as usual.

基本上,你的文章的字符串应该是这样的:

Basically, your POST string should look like:

name=FooBar2009&bands=1&bands=3&bands=4&venue=7&start=...

乐队的名称输入的每个乐队的标识这样做可能是使用Javascript功能来添加(和删除)一个隐藏的输入字段的最简单方法乐队的价值。然后,当用户提交表单时,浏览器会照顾发布正确的东西,而会的ModelForm对其进行验证。

The easiest way to do this might be to use Javascript to add (and remove) a hidden input field for each band entered with the name band and the id of the band as the value. Then, when the user submits the form, the browser will take care of posting the right stuff, and the ModelForm will validate it.

这篇关于Django中的ModelForm验证自定义自动完成,而不是难看多选的M2M,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆