IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
|
|
|
|
| 608 | 608 | on fields and their arguments. |
| 609 | 609 | |
| 610 | 610 | |
| | 611 | If you want to make fields required, which are not required |
| | 612 | by model design, you can specify them in ``extra_required`` property |
| | 613 | of Meta:: |
| | 614 | |
| | 615 | |
| | 616 | from django.db import models |
| | 617 | from django.forms import ModelForm |
| | 618 | |
| | 619 | # `title` and `author` are not required in model.. |
| | 620 | class Book(models.Model): |
| | 621 | title = models.CharField(max_length=100, blank=True) |
| | 622 | author = models.CharField(max_length=100, blank=True) |
| | 623 | |
| | 624 | # ..but they may become so in `extra_required` |
| | 625 | class BookForm(ModelForm): |
| | 626 | |
| | 627 | class Meta: |
| | 628 | model = Book |
| | 629 | fields = ['title', 'author'] |
| | 630 | extra_required = ['title', 'author'] |
| | 631 | |
| | 632 | |
| | 633 | |
| 611 | 634 | Enabling localization of fields |
| 612 | 635 | ------------------------------- |
| 613 | 636 | |
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
|
|
|
|
| 515 | 515 | self.assertEqual(list(OrderFields2.base_fields), |
| 516 | 516 | ['slug', 'name']) |
| 517 | 517 | |
| | 518 | def test_extra_required_fields_in_form_meta(self): |
| | 519 | class FormWithExtraRequiredInMeta(forms.ModelForm): |
| | 520 | """For this test we specify fields, |
| | 521 | which are NOT required in model, and |
| | 522 | make sure, that they become required after |
| | 523 | specifying them in Meta.extra_required. |
| | 524 | """ |
| | 525 | class Meta: |
| | 526 | model = Book |
| | 527 | fields = ['author', 'special_id'] |
| | 528 | extra_required = ['author', 'special_id'] |
| | 529 | self.assertEqual([f.required for f in FormWithExtraRequiredInMeta.base_fields.itervalues()], |
| | 530 | [True, True]) |
| | 531 | |
| 518 | 532 | |
| 519 | 533 | class FieldOverridesByFormMetaForm(forms.ModelForm): |
| 520 | 534 | class Meta: |
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
|
|
|
|
| 231 | 231 | self.model = getattr(options, 'model', None) |
| 232 | 232 | self.fields = getattr(options, 'fields', None) |
| 233 | 233 | self.exclude = getattr(options, 'exclude', None) |
| | 234 | self.extra_required = getattr(options, 'extra_required', None) |
| 234 | 235 | self.widgets = getattr(options, 'widgets', None) |
| 235 | 236 | self.localized_fields = getattr(options, 'localized_fields', None) |
| 236 | 237 | self.labels = getattr(options, 'labels', None) |
| … |
… |
|
| 298 | 299 | fields = new_class.declared_fields |
| 299 | 300 | |
| 300 | 301 | new_class.base_fields = fields |
| | 302 | |
| | 303 | # Need to set field.required, if it's in 'extra_required' |
| | 304 | # of ModelForm Meta class. |
| | 305 | for field_name in new_class.base_fields.keys(): |
| | 306 | if opts.extra_required and field_name in opts.extra_required: |
| | 307 | setattr(new_class.base_fields[field_name], 'required', True) |
| 301 | 308 | |
| 302 | 309 | return new_class |
| 303 | 310 | |