Saturday, May 29, 2010

How to customize ModelChoiceField?

I have a table of Rates:

class Rate( models.Model ):
edition = ForeignKey( Edition )
description = ForeignKey( Foo )
rate = IntegerField( )
...
[ other fields not relevant here ]

and a RateForm:

class RateForm( forms.ModelForm ):

class Meta:
fields = ( 'description', 'rate' )

and a formset for editing a table of rates for a given edition:

class BaseRateFormSet( BaseModelFormSet ):
def __init__( self, edition, *args, **kw ):
self.edition = edition
queryset = kw.pop( 'queryset', Rate.objects.all() )
queryset = queryset.filter( edition = edition )
kw[ 'queryset' ] = queryset
super( BaseRateFormSet, self ).__init__( *args, **kw )

RateFormSet = modelformset_factory( Rate, from = RateForm, formset =
BaseRateFormSet )

Now, I want to edit the "description" field inline... basically I just
want a text box, and then I'll either search for
that description or create a new one. (But I need to have the parent
"Rate" instance available somehow to store the pk).

Since I'm already using a formset, as far as I can tell, an inline
formset doesn't seem to be applicable. What I think I want to do is
override the description field in the RateForm to be a custom
derivative of ModelChoiceField... but I'm not sure where to intercept
validation, etc. -- Indeed the documentation seems to skip over
ModelChoiceField altogether. Can anyone give me some pointers?

Thanks!

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

No comments:

Post a Comment