Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Add New" to foreign key field in django modelform

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?

like image 982
danatron Avatar asked Jul 20 '12 21:07

danatron


1 Answers

Why not use a standard form (not a ModelForm) for this? Add the "add new" as an extra option and catch it in the form's clean() method where you should then create the new object, redirect to another form etc.

like image 175
Chris Avatar answered Sep 24 '22 02:09

Chris