Thursday, November 15, 2012

Form widget for ForeignKey, how to access the selected object attributes (for displaying)

Hi,
althought I love django, I don't like what it does with ForeignKey in
ModelForms - I don't find HTML's select widget suitable for selecting
when there are dozens or even hundreds related records to select from.

In the form template I'm rendering the field as_hidden, and there's a
script which displays jQueryUI dialog with the searchable grid.
When closing the dialog, the selected row's id is set as a value of
the hidden field, and row's label is displayed in the placeholder.

This works for the Create view, but should work also for Update view -
but I don't know how to access the field's model in the widget's
render method.

def render(value, ...)
obj = FooModel.objects.get(pk = value)
return '<input type="hidden" value="%d"/> %s' % (value, str(obj))

that would work of course, but I need it to be more generic- to handle
any type of related model, not just only FooModel

any tips?

--
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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate