If you don't want the foreign key (or another field)  to be displayed 
in the form you can define a formfield_callback:

def obj_callback(f, **kwargs):
  "tell forms to ignore uwb foreign key & slug"
  if f.name == "uwb" or f.name == "slug": # don't include uwb or slug 
in the form
    return None
  return f.formfield(**kwargs)

then, when you instantiate the form:
 form = forms.form_for_model(Mymodel, formfield_callback=obj_callback)
()

whether you displayed the foreign key or not, when its time to save 
the changes you need to update the clean_data with the proper foreign 
key:
 if request.method == 'POST':
    form = forms.form_for_model(Mymodel, 
formfield_callback=obj_callback)(request.POST)
    if form.is_valid():
      form.clean_data['uwb'] = get_correct_uwb()
      n = Mymodel.objects.filter(uwb=uwb).count() + 1 #get count of 
objects
      form.clean_data['slug'] = 'Object%d'%(n)
      form.save()
      return HttpResponseRedirect(...)

and finally, if you are using form_for_instance, to update an existing 
instance, you can't use the form.save - instead try something like 
this:
  form = forms.form_for_instance(obj,  formfield_callback=obj_callback)
(request.POST)
    if form.is_valid():
      for i in form.clean_data:
        obj.__setattr__(i, form.clean_data[i])
      obj.save()
      return HttpResponseRedirect(...)

Hope this helps!
-chasfs

On Jan 29, 2:52 pm, "sandro.dentella" <[EMAIL PROTECTED]> wrote:
> Hi all,
>
>   I'm trying to learn newforms. I create a form with form_for_model
> and now presenti it in the template. A foreign key renders as a select
> widget, that is ok, but how can I limit the values filtering them out?
>
> Should I pass the widget when creating the Form using
> formfield_callback? Can I change the values after the Form is created?
> (or after the instance is created?)
>
> Thanks in advance
> sandro
> *:-)


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

Reply via email to