Code

Ticket #3534: model_choice_field_cache_tests.patch

File model_choice_field_cache_tests.patch, 1.1 KB (added by adrian, 7 years ago)

Unit tests

  • tests/modeltests/model_forms/models.py

     
    311311>>> f.clean(2) 
    312312<Category: It's a test> 
    313313 
     314# Add a Category object *after* the ModelChoiceField has already been 
     315# instantiated. This proves clean() checks the database during clean() rather 
     316# than caching it at time of instantiation. 
     317>>> Category.objects.create(name='Fourth', url='4th') 
     318<Category: Fourth> 
     319>>> f.clean(4) 
     320<Category: Fourth> 
     321 
     322# Delete a Category object *after* the ModelChoiceField has already been 
     323# instantiated. This proves clean() checks the database during clean() rather 
     324# than caching it at time of instantiation. 
     325>>> Category.objects.get(id=4).delete() 
     326>>> f.clean(4) 
     327Traceback (most recent call last): 
     328... 
     329ValidationError: [u'Select a valid choice. That choice is not one of the available choices.'] 
     330 
    314331>>> f = ModelChoiceField(Category.objects.filter(pk=1), required=False) 
    315332>>> print f.clean('') 
    316333None