7

I'm pretty new to this, apologies if i'm unclear....

running django, with bootstrap, and i've been fiddling with django-bootstrap. BootstrapModelForm is pretty cool, i didn't include the django-bootstrap stuff in my example, but i though i should note.

I have models:

class spam(models.Model):
   name = models.CharField()

class Eggs(models.Model):
   spam = models.ForiegnKey('spam')

I use a modelform,something like:

class EggsForm(forms.ModelForm):
   meta:
      model=Eggs

EggsForm allows selection of which spam will be referenced by eggs. I noticed the admin interface has a cool way to either select or add another egg.

How can i get similar functionality into my create form?

4

3 回答 3

1

为什么不为此使用标准表格(而不是 ModelForm)?添加“添加新”作为额外选项,并在表单的 clean() 方法中捕获它,然后您应该在其中创建新对象、重定向到另一个表单等。

于 2012-07-20T22:27:45.700 回答
1

有点晚了,但我在寻找解决方案并进行更多互联网冒险时遇到了这个 SO 问题,我发现此链接上的说明非常好,而且确实有人问: http: //www.hoboes.com /Mimsy/hacks/replicating-djangos-admin/

于 2014-08-12T20:06:02.003 回答
0

在您的模板中,您可以添加一个新(或“+”)按钮,该按钮可以重定向或显示弹出窗口或类似的显示另一个表单以添加新的Egg.

于 2012-07-21T06:23:51.773 回答