List Info

Thread: newforms, foreignKeys & constraints




newforms, foreignKeys & constraints
user name
2007-01-29 13:52:47
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 django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: newforms, foreignKeys & constraints
user name
2007-01-30 02:37:02
Hi all,

  I'm trying to learn newforms. I create a form with
form_for_model 
and now present 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
*


NOTE: I'm sending this again since it doesn't appear on
google after 14
      hours. First time sent via web now via mail. It's not
the first time I
      experiment this problem! 


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


Re: newforms, foreignKeys & constraints
user name
2007-01-30 12:38:40
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"
<san...e-den.it> 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 django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: newforms, foreignKeys & constraints
user name
2007-01-30 13:52:18
On 1/30/07, Sandro Dentella <sandroe-den.it> wrote:
> NOTE: I'm sending this again since it doesn't appear on
google after 14
>       hours. First time sent via web now via mail. It's
not the first time I
>       experiment this problem! 

Please don't re-send when that happens. It usually means
that Google's
spam filter is overactive and has delayed the message;
re-sending
means the list gets two copies of the message (and I, at
least, *did*
get two copies of it).

-- 
"Bureaucrat Conrad, you are technically correct -- the
best kind of correct."

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


Re: newforms, foreignKeys & constraints
user name
2007-01-30 17:39:28


On 30 Gen, 19:38, "chasfs" <cha...yahoo.com> wrote:
> If you don't want the foreign key (or another field) 
to be displayed
> in the form you can define a formfield_callback:

This in not really the point. I *do* want Foreignkeys to
appear. 
Suppose the foreign key is a project. I want a select
widgets with 
projects /but/ I don't want all the possible projects, just
some (eg. 
project the user belongs to).

I think a possibility is to use formfield_callback that
returns 
ChoiceField with a correct QuerySet  as choices, I was just
wondering 
whether there was a simpler way.

sandro
*


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


Re: newforms, foreignKeys & constraints
user name
2007-01-31 04:24:10
You can create your own ChoiceField with your own set of
choices and
replace default property to your own
For example:

FormClass = forms.models.form_for_model(MyModel)
FormClass.base_fields['foreign_key_field'].widget =
forms.ChoiceField(choices=[(obj.id, obj.name) for obj in
MyModel.objects.filter(...)])

something like this


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


Re: newforms, foreignKeys & constraints
user name
2007-01-31 04:27:15
Sorry, this:

On Jan 31, 3:24 pm, "ak" <a...khalikov.ru> wrote:
> FormClass.base_fields['foreign_key_field'].widget =
forms.ChoiceField(choices=[(obj.id, obj.name) for obj in

must be:
FormClass.base_fields['foreign_key_field'] =
forms.ChoiceField(choices=[(obj.id, obj.name) for obj in
MyModel.objects.filter(...)])


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


[1-7]

about | contact  Other archives ( Real Estate discussion Medical topics )