Re: Decoupling fields from forms

2006-05-11 Thread Ivan Sagalaev

George Sakkis wrote:

>>I understand that Django maintains two parallel hierarchies of field
>>classes, one related to models that correspond to table columns and one
>>that maps these fields to forms so that they can be edited in the admin
>>interface. This works ok most of the time, but what if one wants to
>>have an alternative or additional widget associated to a field ? For
>>instance, say I have a "birthday" DateField that I want to render as a
>>date range widget in a search form ("From - Up to"). Depending on which
>>form fields are filled, this widget creates an appropriate field lookup
>>(birthday__gte, birthday__lte, birthday__range). What I did so far was
>>to make a DateRangeField as subclass of DateField and override the
>>appropriate methods. This works fine for rendering the form but breaks
>>several other things (e.g. in the admin interface I still want to show
>>the field as regular DateField widget, not a range; also the backend
>>knows nothing about how to map a DateRangeField to a table column). Any
>>ideas on how to decouple the field from its widget(s) ?
>>
>>
>Given that there were no replies on this, should I assume that either
>1) people find the default Django form widgets adequate in all cases,
>or
>2) they hardcode their widgets in html if they need anything more ?
>  
>
It's 3) in my case :-). I was about to reply but get busy and forgot 
about it :-)

The thing that decouples model fields from form fields is a manipulator. 
Automatic manipulators that are created for models provide default form 
fields for model fields. If you need some other representation in your 
own form you can subclass automatic manipulator or create a custom one 
from scratch depending on what is simpler. This is the example how to do 
it in your case when you need to replace one single form field for a date:

class MyManipulator(MyModel.ChangeManipulator):
  def __init__(self, id):
MyModel.ChangeManipulator.__init__(self, id)
   
# Remove default field from manipulator fields
for field in self.fields:
  if field.field_name == 'some_date':
date_field = field
break
self.fields.remove(date_field)
   
# Add your own field
self.fields.append(MyDateRangeField(field_name='some_date'))

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: Decoupling fields from forms

2006-05-10 Thread George Sakkis

> I understand that Django maintains two parallel hierarchies of field
> classes, one related to models that correspond to table columns and one
> that maps these fields to forms so that they can be edited in the admin
> interface. This works ok most of the time, but what if one wants to
> have an alternative or additional widget associated to a field ? For
> instance, say I have a "birthday" DateField that I want to render as a
> date range widget in a search form ("From - Up to"). Depending on which
> form fields are filled, this widget creates an appropriate field lookup
> (birthday__gte, birthday__lte, birthday__range). What I did so far was
> to make a DateRangeField as subclass of DateField and override the
> appropriate methods. This works fine for rendering the form but breaks
> several other things (e.g. in the admin interface I still want to show
> the field as regular DateField widget, not a range; also the backend
> knows nothing about how to map a DateRangeField to a table column). Any
> ideas on how to decouple the field from its widget(s) ?
>
> Thanks,
> George

Given that there were no replies on this, should I assume that either
1) people find the default Django form widgets adequate in all cases,
or
2) they hardcode their widgets in html if they need anything more ?


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Decoupling fields from forms

2006-04-28 Thread George Sakkis

Hi all,

I understand that Django maintains two parallel hierarchies of field
classes, one related to models that correspond to table columns and one
that maps these fields to forms so that they can be edited in the admin
interface. This works ok most of the time, but what if one wants to
have an alternative or additional widget associated to a field ? For
instance, say I have a "birthday" DateField that I want to render as a
date range widget in a search form ("From - Up to"). Depending on which
form fields are filled, this widget creates an appropriate field lookup
(birthday__gte, birthday__lte, birthday__range). What I did so far was
to make a DateRangeField as subclass of DateField and override the
appropriate methods. This works fine for rendering the form but breaks
several other things (e.g. in the admin interface I still want to show
the field as regular DateField widget, not a range; also the backend
knows nothing about how to map a DateRangeField to a table column). Any
ideas on how to decouple the field from its widget(s) ?

Thanks,
George


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---