Ticket #22925: extra_required.patch

File extra_required.patch, 3.5 KB (added by Andriy Sokolovskiy, 10 years ago)

implements extra_required

  • django/docs/topics/forms/modelforms.txt

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    608608    on fields and their arguments.
    609609
    610610
     611If you want to make fields required, which are not required
     612by model design, you can specify them in ``extra_required`` property
     613of 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
    611634Enabling localization of fields
    612635-------------------------------
    613636
  • django/tests/model_forms/tests.py

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    515515        self.assertEqual(list(OrderFields2.base_fields),
    516516                         ['slug', 'name'])
    517517
     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
    518532
    519533class FieldOverridesByFormMetaForm(forms.ModelForm):
    520534    class Meta:
  • django/django/forms/models.py

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    231231        self.model = getattr(options, 'model', None)
    232232        self.fields = getattr(options, 'fields', None)
    233233        self.exclude = getattr(options, 'exclude', None)
     234        self.extra_required = getattr(options, 'extra_required', None)
    234235        self.widgets = getattr(options, 'widgets', None)
    235236        self.localized_fields = getattr(options, 'localized_fields', None)
    236237        self.labels = getattr(options, 'labels', None)
     
    298299            fields = new_class.declared_fields
    299300
    300301        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)
    301308
    302309        return new_class
    303310
Back to Top