<s> from django import forms <EOL> from django . core . validators import EMPTY_VALUES <EOL> class OurModelChoiceIterator ( forms . models . ModelChoiceIterator ) : <EOL> def __iter__ ( self ) : <EOL> if self . field . empty_label is not None : <EOL> yield ( u"<STR_LIT>" , self . field . empty_label ) <EOL> if self . field . cache_choices : <EOL> if self . field . choice_cache is None : <EOL> self . field . choice_cache = [ <EOL> self . choice ( obj ) for obj in self . queryset <EOL> ] <EOL> for choice in self . field . choice_cache : <EOL> yield choice <EOL> else : <EOL> for obj in self . queryset : <EOL> yield self . choice ( obj ) <EOL> class OurModelChoiceField ( forms . ModelChoiceField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . model = kwargs . pop ( "<STR_LIT>" ) <EOL> super ( OurModelChoiceField , self ) . __init__ ( * args , ** kwargs ) <EOL> def to_python ( self , value ) : <EOL> if value in EMPTY_VALUES : <EOL> return None <EOL> value = self . model . get ( value ) <EOL> return value <EOL> def _get_choices ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . _choices <EOL> return OurModelChoiceIterator ( self ) <EOL> choices = property ( _get_choices , forms . ModelChoiceField . _set_choices ) </s>
