presetting a field value with ModelForm

63 views
Skip to first unread message

MikeKJ

unread,
Nov 12, 2013, 9:30:43 AM11/12/13
to django...@googlegroups.com
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

Timothy W. Cook

unread,
Nov 12, 2013, 10:02:53 AM11/12/13
to django...@googlegroups.com
Thanks for posting this. I was just about to post a question with the
same exact scenario.

Maybe we are missing it in the docs somewhere???
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@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/31885005-52f5-49b7-8ad4-c770961c6481%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.



--
MLHIM VIP Signup: http://goo.gl/22B0U
============================================
Timothy Cook, MSc +55 21 94711995
MLHIM http://www.mlhim.org
Like Us on FB: https://www.facebook.com/mlhim2
Circle us on G+: http://goo.gl/44EV5
Google Scholar: http://goo.gl/MMZ1o
LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook

Sergiy Khohlov

unread,
Nov 12, 2013, 10:07:40 AM11/12/13
to django-users
It is easy to do. You should update object before executing save()...
Many thanks,

Serge


+380 636150445
skype: skhohlov
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2B%3DOU3Xd%3DP6_h%3Dw8%2B8-W6JXi5tfPoBJ9%2Bipv04S%3DNdGS7%2Bz1DQ%40mail.gmail.com.

C. Kirby

unread,
Nov 12, 2013, 10:13:18 AM11/12/13
to django...@googlegroups.com
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()

Sergiy Khohlov

unread,
Nov 12, 2013, 10:22:26 AM11/12/13
to django-users
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


> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@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.

MikeKJ

unread,
Nov 12, 2013, 10:38:39 AM11/12/13
to django...@googlegroups.com
Perfect, simple and blindingly obvious..... thanks C.Kirby

MikeKJ

unread,
Nov 12, 2013, 10:40:39 AM11/12/13
to django...@googlegroups.com
Perfect, simple and blindingly obvious now it is pointed out, many thanks C.Kirby and thanks to Sergiy for replying.

Reply all
Reply to author
Forward
0 new messages