Here is an approach I have used. I can't tell from your question whether this will help you or not.
class AccountEntryModelForm(ModelForm): def __init__(self, organization=None, operation=None, *args, **kwargs): super(ModelForm, self).__init__(*args, **kwargs) self.organization = organization if self.organization: account_entry_types = AccountEntryType.objects.filter(organization=self.organization) self.fields['account_entry_type'].choices = [('', '-----------')] self.fields['account_entry_type'].choices.extend([(aet.id, aet.name) for aet in account_entry_types]) HTH Alan On Dec 2, 2:00 pm, Alex Boyko <alex.kyr...@gmail.com> wrote: > Hi Cal! > > Thank you very much for your reply and advice, but unfortunately it doesn't > work. It seems like in __init__ method we can't re-assign the choices. Even > if I try: > > class Something(models.Model): > def __init__(self, *args, **kwargs): > _choices = ((SomeValue, 'SomeString'), ) > self.range_marks = models.IntegerField(choices = _choices, > verbose_name = 'Marks of Something') > super(Something, self).__init__(*args, **kwargs) > > In admin page I see that my field has choices option initially defined in > global scope (I've showed that definition in my first letter). > Any suggestions about how I can assign choice attribute on the fly during > instance creation. In future It doesn't change but my instances have to have > different ranges in choices option. > > Best Regards, > Alex > > On 2 December 2010 10:15, Cal Leeming [Simplicity Media Ltd] < > > > > > > > > cal.leem...@simplicitymedialtd.co.uk> wrote: > > Hi Alex, > > > I think this is what you might be looking for. > > > class Something(models.Model): > > def __init__(self, *args, **kwargs): > > if kwargs.has_key('range_marks'): > > _choices = kwargs.get('range_marks') > > del kwargs['range_marks'] > > else: > > _choices = (('default', 'default'), ) > > self.range_marks = models.IntegerField(choices = _choices, > > verbose_name = 'Marks of Something') > > super(Something, self).__init__(*args, **kwargs) > > > Let us know if it works :) > > > Cheers > > > Cal > > > On 02/12/2010 06:53, Alex Boyko wrote: > > >> Hi! > > >> I got such kind of problem. > >> I'd like to define the tuple(list) of choices option at the moment of > >> instance created on the fly > >> In the future these choices will not change. > >> Hence I've found just one way to do it - override __init__() of my model > >> and > >> pass there the particular predefined list of choices that depends on > >> current model's instance (in code - 'range_list') > >> But this approach seems doesn't work properly. > >> Would You be so kind to help with advice - how I can solve my task with > >> dynamical choices per model's instance. > > >> --------------------------------------- > >> RANGE_CHOICES = () > > >> class Something(models.Model): > >> def __init__(self, *args, **kwargs): > >> range = kwargs.pop('range_list', None) > >> super(Covenant, self).__init__(*args, **kwargs) > >> self._meta.get_field_by_name('range_marks')[0]._choices = range > > >> range_marks = models.IntegerField(choices = RANGE_CHOICES, verbose_name > >> = 'Marks of Something') > >> ----------------------------------------- > > >> Thanks in Advance! > >> Best Regards! > >> Alex > >> -- > >> You received this message because you are subscribed to the Google Groups > >> "Django users" group. > >> To post to this group, send email to django-us...@googlegroups.com. > >> To unsubscribe from this group, send email to > >> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@google > >> groups.com> > >> . > >> For more options, visit this group at > >>http://groups.google.com/group/django-users?hl=en. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.