Tuesday, November 27, 2012

Re: Seeding Foreign Key with known object with Class Based Views

Thanks Sergi; I just realized my earlier response to you is still sitting in my drafts folder.  This is helpful.



On Mon, Nov 26, 2012 at 12:08 AM, Sergiy Khohlov <skhohlov@gmail.com> wrote:
I'm proposing  to add form_valid  function  and make  next trick


 def form_valid(self, form)
 # avoid to early savind od the new address
        newaddress  = form.save(commit=False)
       # you need to update object by correct client id value. Keep in
mind that I'm writing  without django from brain only :-)
        newaddress.client_id = self.client_data.id
       # now  we are ready to save new address
        newaddress.save()
        return http.HttpResponseRedirect(self.get_success_url())

2012/11/25 Lee Hinde <leehinde@gmail.com>:
>
> On Nov 24, 2012, at 2:41 PM, Sergiy Khohlov <skhohlov@gmail.com> wrote:
>
>> I would like to clarify:
>>
>> 1) you dont need to add client id to the form
>> (this value is already  set in your view)
>>
>> 2) If you dont know how to  save address let ask
>> Of course please add your code. I can not hack your PC and understand
>> your problem.
>>
>> thanks, Serge
>> 2012/11/24 Lee Hinde <leehinde@gmail.com>:
>>> nt to seed the client id programmatically without presenting a form
>>>> field to enter what's a known value
>>
>> --
>
>
> Thanks for sticking with this.
>
> class AddressForm(ModelForm):
>     def __init__(self, *args, **kwargs):
>         super(AddressForm, self).__init__(*args, **kwargs)
>
>         self.fields['city'].widget.attrs["class"] = 'capme'
>         self.fields['zip'].widget.attrs["class"] = 'zipcode'
>         self.fields['state'].widget.attrs["class"] = 'input-mini upper_case'
>
>     class Meta:
>         model = Address
>         fields = ('street_one','street_two','city','state','zip','response_type')
>         exclude = ('client',)
>
>
> class AddressNew(CreateView):
>     model = Address
>     template_name = 'letters/address.html'
>     form_class = AddressForm
>     context_object_name = 'myfields'
>
>     def get(self, request, *args, **kwargs):
>         set_current_page(request, "", "active", "", "")
>         return super(AddressNew, self).get(request, *args, **kwargs)
>
>     def get_context_data(self, **kwargs):
>         context = super(AddressNew, self).get_context_data(**kwargs)
>         context['client_data'] = get_object_or_404(Client,pk=self.kwargs["pk"])
>         return context
>
>
> Then, in the template I'm putting client_data.id into a hidden field to be returned when the form is saved.
>
>
> --
> 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.

No comments:

Post a Comment