| | 2337 | Similarly, changes to field attributes do not persist from one Form instance |
|---|
| | 2338 | to the next. |
|---|
| | 2339 | >>> class Person(Form): |
|---|
| | 2340 | ... first_name = CharField(required=False) |
|---|
| | 2341 | ... last_name = CharField(required=False) |
|---|
| | 2342 | ... def __init__(self, names_required=False, *args, **kwargs): |
|---|
| | 2343 | ... super(Person, self).__init__(*args, **kwargs) |
|---|
| | 2344 | ... if names_required: |
|---|
| | 2345 | ... self.fields['first_name'].required = True |
|---|
| | 2346 | ... self.fields['last_name'].required = True |
|---|
| | 2347 | >>> f = Person(names_required=False) |
|---|
| | 2348 | >>> f['first_name'].field.required, f['last_name'].field.required |
|---|
| | 2349 | (False, False) |
|---|
| | 2350 | >>> f = Person(names_required=True) |
|---|
| | 2351 | >>> f['first_name'].field.required, f['last_name'].field.required |
|---|
| | 2352 | (True, True) |
|---|
| | 2353 | >>> f = Person(names_required=False) |
|---|
| | 2354 | >>> f['first_name'].field.required, f['last_name'].field.required |
|---|
| | 2355 | (False, False) |
|---|
| | 2356 | >>> class Person(Form): |
|---|
| | 2357 | ... first_name = CharField(max_length=30) |
|---|
| | 2358 | ... last_name = CharField(max_length=30) |
|---|
| | 2359 | ... def __init__(self, name_max_length=None, *args, **kwargs): |
|---|
| | 2360 | ... super(Person, self).__init__(*args, **kwargs) |
|---|
| | 2361 | ... if name_max_length: |
|---|
| | 2362 | ... self.fields['first_name'].max_length = name_max_length |
|---|
| | 2363 | ... self.fields['last_name'].max_length = name_max_length |
|---|
| | 2364 | >>> f = Person(name_max_length=None) |
|---|
| | 2365 | >>> f['first_name'].field.max_length, f['last_name'].field.max_length |
|---|
| | 2366 | (30, 30) |
|---|
| | 2367 | >>> f = Person(name_max_length=20) |
|---|
| | 2368 | >>> f['first_name'].field.max_length, f['last_name'].field.max_length |
|---|
| | 2369 | (20, 20) |
|---|
| | 2370 | >>> f = Person(name_max_length=None) |
|---|
| | 2371 | >>> f['first_name'].field.max_length, f['last_name'].field.max_length |
|---|
| | 2372 | (30, 30) |
|---|
| | 2373 | |
|---|