1) you  should create a form for building
2) you should create a form for inhabitant and set key to the  correct
value  before saving

 IMHO   simple way is create a edit form for buiding which has buttons
add/remove inhabitant .  check  DetailView  in docs please

 thanks, Serge
skype: skhohlov
tel: +380636150445


2012/12/12 Jonas Geiregat <jo...@geiregat.org>:
> Hello,
>
> Suppose I have a model with a foreign key.
>
> class Building(models.Model):
>     pass
> class Inhabitant(models.Model):
>     building = models.ForeignKey(Building)
>
>
> I would like to display a form where all fields from the Building model and 
> Inhabitant model are visible (but only once)
>
> Should I create 2 forms, one for Building and one for Inhabitant ? And upon 
> submission add the Building to the Inhabitant.
>
> Currently I've worked together a little working something using the 
> CreateView (Generic views)
>
>
> def post(..)
>     self.inhabitant_form = InhabitantForm(data=request.POST)
>     if form.is_valid() and self.inhabitant_form.is_valid():
>         ….
>
> def form_valid(self, form):
>     # form is an instance of BuildingForm
>     building = form.instance
>     building.save()
>
>     # I'm also overriding the post method where self.inhabitant_form is being 
> set
>     inhabitant = self.inhabitant_form.instance
>     self.inhabitant.building = building
>     self.inhabitant.save()
>
>
> Am I on the right path or are there are better ways of handling such 
> situations ?
>
> Regards,
>
> Jonas.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to