> If you wanted to use the standard form_for_* functions, that sounds
> like something you could solve by creating a BaseForm which overrides
> __init__ (taking any extra arguments you require,.e.g. querysets which
> specify data to be used to populate a field's choices), and modifies
> the form's fields after the call to super(...).__init__, passing this
> BaseForm into form_for_* functions using their "form" argument.
This will work but you'll hit database twice.
This is because when ForeignKey is converted to ModelChoiceField
it prepopulates widget choices list. AFAIR this causes query to be
executed
during field creation.

> class JobBaseForm(forms.BaseForm):
>     def __init__(self, *args, **kwargs):
>         super(JobBaseForm, self).__init__(*args, **kwargs)
>         self.fields['number'].required = False
Yes, it is good way I think.

My callback (that in fact uses modified version of ModelChoiceField
and
 modified QuerySetIteratof look this way:

# this callback is called for every field
def pre_customer_callback(field, defaults, extra):
    """ defaults - dictionary passed to newforms' field constructor
         extra - additional parameters
    """
    # get default queryset for field
    queryset = defaults.get('queryset', None)

    # we want to show only subcompanies that belong to this company
    company = extra['company']

    # mark all not null fields as required
    defaults['required']=not field.null

    # additionally mark other fields as required
    if field.name in ['phone', 'fax']:
        defaults['required']=True

    if field.name=='subcompany':
        defaults['queryset'] =
queryset.filter(company__exact=company)
        # in the <select> combo box we want user to see something
like: 'RedCompany (Paris)'
        defaults['queryset_label']=lambda obj: obj.name
+' ('+obj.city.name+')'


--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to