Django

Code

Show
Ignore:
Timestamp:
03/19/08 18:10:45 (10 months ago)
Author:
jacob
Message:

Fixed #4620: you can now easily add custom labels to ModelChoiceFields? via subclassing. Thanks, PhiR.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/tests/modeltests/model_forms/models.py

    r7322 r7326  
    660660ValidationError: [u'Select a valid choice. That choice is not one of the available choices.'] 
    661661 
     662# check that we can safely iterate choices repeatedly 
     663>>> gen_one = list(f.choices) 
     664>>> gen_two = f.choices 
     665>>> gen_one[2] 
     666(2L, u"It's a test") 
     667>>> list(gen_two) 
     668[(u'', u'---------'), (1L, u'Entertainment'), (2L, u"It's a test"), (3L, u'Third')] 
     669 
     670# check that we can override the label_from_instance method to print custom labels (#4620) 
     671>>> f.queryset = Category.objects.all() 
     672>>> f.label_from_instance = lambda obj: "category " + str(obj) 
     673>>> list(f.choices) 
     674[(u'', u'---------'), (1L, 'category Entertainment'), (2L, "category It's a test"), (3L, 'category Third'), (4L, 'category Fourth')] 
    662675 
    663676# ModelMultipleChoiceField #################################################### 
     
    745758ValidationError: [u'Select a valid choice. 4 is not one of the available choices.'] 
    746759 
     760>>> f.queryset = Category.objects.all() 
     761>>> f.label_from_instance = lambda obj: "multicategory " + str(obj) 
     762>>> list(f.choices) 
     763[(1L, 'multicategory Entertainment'), (2L, "multicategory It's a test"), (3L, 'multicategory Third'), (4L, 'multicategory Fourth')] 
    747764 
    748765# PhoneNumberField ############################################################