Wednesday, December 12, 2012

Re: Foreignkey Form

Thanks for the response Serge,

So basically I was on the right path.

But I don't see what the DetailView has to do with this ? Don't you mean the CreateView ?

Jonas.

> 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 <jonas@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+unsubscribe@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+unsubscribe@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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate