On May 25, 1:27 pm, Simon Davies <simon...@gmail.com> wrote: > I have a choices field called status in my model form which can be set > to a number of values, however the options available will be based on > its current state something like this, so it needs to refer to itself: > > class ContractForm(ModelForm): > > def getStatusChoices() > if status == 'r': > return (('C', 'Confirmed'), ('N', 'Cancelled)) > elif status == 'C': > return (('M', 'Completed'), ('P', 'Paid')) > .... > > status = fields.ChoiceField(choices=getStatusChoices()) > > I read in another thread that you can do this by accessing and setting > the self.fields['status'] which contains the status state of the > current object and is created by the meta form class. How do I access > self in the model function. If i pass it in as a parameter to the > method as shown below what should the function call look like in the > status field, i.e. how do I refer to the current object to be passed. > > def getStatusChoices(self): > if not self.key: > self.fields['STATUS'] = (('R', 'Requested'), > ('C', 'Confirmed')) > elif self.fields['STATUS'] == 'R': > self.fields['STATUS'] = (('N', 'Cancelled'), > ('C', 'Confirmed')) > return self > > Thanks > > Simon
Hi, it might help if you post a bit more from your form definition, but I'll try to answer. Take the following example: class MyForm(forms.Form): status_choices = forms.ChoiceField(choices=(('0', 'False'),)) def __init__(self, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) if kwargs.has_key('initial') and kwargs['initial'].has_key ('status'): self.fields['status_choices'].choices = getStatusChoices(kwargs ['initial']['status'] that is you would like to set the available choices for the status_choices field. You do this in the form's __init__ method. First you initialise the form as usual (see the super line), then you adjust the choices value of the status_choices field. Clearly, in my setting the getStatusChoices function should return a valid choices tuple. have a nice day --~--~---------~--~----~------------~-------~--~----~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---