I'm using class based view and have a simple example below. Not ideal
but good for start:
form.py
class CreateGoodsForm(ModelForm):

    class Meta:
        model = Goods
        exclude = ('category',)

view.py

class GoodsCatalogCreateView( CreateView):

    """ View for creating  new category items"""
    form_class = CreateCategoryForm
    model = GoodsCatalog
    template_name = 'goods/create_category.html'

    success_url = '/goodscatalog/'
    def get_object(self):
        currentnode = super(GoodsCatalogCreateView,self).get_object()
        return currentnode

    def form_valid(self, form, **kwargs):
        try:
            GoodsCatalog.objects.create(name =
form.cleaned_data['name'],parent= GoodsCatalog.objects.get(id =
int(self.kwargs['pk']))  )
        except KeyError:
            GoodsCatalog.objects.create(name = form.cleaned_data['name']  )
        return redirect(self.success_url)

    def form_invalid(self, form):
        from django.forms.util import ErrorList
        return HttpResponse("Error form")

Many thanks,

Serge


+380 636150445
skype: skhohlov


On Tue, Nov 12, 2013 at 5:13 PM, C. Kirby <mist...@gmail.com> wrote:
> You don't show your view, which would be helpful but you process the form
> with commit = false, then set the org, then save so something like:
>
> if adviceform.is_valid():
>     af = adviceform(commit = false)
>     af.organization = org_id#However you get this from the session
>     af.save()
>
>
>
> On Tuesday, November 12, 2013 8:30:43 AM UTC-6, MikeKJ wrote:
>>
>> This is driving me nuts....  I do not want the user to be able to select
>> organisation I want to populate the form field with the known
>> organisation....
>>
>> organisation is a foreignkey to the model I am creating this form on
>>
>> so
>>
>> class AdviceLevel(models.Model):
>>     advice = models.ForeignKey(Advice)
>>     organisation = models.ForeignKey(Organisation)
>>     level_1 = models.BooleanField()
>>     level_2 = models.BooleanField()
>>     level_3 = models.BooleanField()
>>     level_4 = models.BooleanField()
>>
>> class AdviceForm(ModelForm):
>>     class Meta:
>>         model = AdviceLevel
>>         exclude = ('organisation',)
>>
>> BUT excluding organisation in the form builder disables being able to use
>> form.save() in the view even though I explicitly set organisation to the
>> correct value in the view,, even added <p><label
>> for="id_organisation">Organisation:</label><input type="text"
>> name="organisation" id="id_organisation" value="{{ org }}" /></p> to the
>> form to no avail
>>
>> so how can I explicitly set the value of the organisation bearing in mind
>> I can't access a session value in the AdviceForm (or so it seems) to use
>> something like organisation = queryset(Organisation.objects.get(pk=org_id)
>> where org_id is a session value.
>>
>> Im probably not seeing the woods for the trees here
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/c3d48fd6-ab4e-4e00-a156-59893fa0bc4a%40googlegroups.com.
>
> For more options, visit https://groups.google.com/groups/opt_out.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADTRxJPuh5RhCVq1Sy1_h7jn6RwQzN7MMMsVwPvtZeNn0bBhdA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to