Search code examples
djangoadminmodels

How can I make all fields in Django admin not required by default?


I may want these fields to be required at another point on a form, but for now how can I set all fields from my model to not required for the admin? Id prefer not required to be default if that is possible. To elaborate...I go to enter data into the django admin form, but it does not save it because it says that the other fields are required. Is there a way for me to fix this for just the admin? Thank you in advance for your help!


Solution

  • First: If your Model has such fields as blank=False or null=False (depending on the field), two things will happen:

    1. Yoy MUST create a custom ModelForm having each same field declared, each with required=False
    2. Such model will make boom when trying to save it to database (because null values cannot be passed to null=False columns).

    So, in first place, you must have all your fields in your model as null=True and blank=True. Consequently, and by-default, ModelForm classes will be created with fields with the same restrictions - in this case, having required=False. So it will be open by default, and you can safely use a ModelAdmin as always. BUT this implies you must declare an explicit ModelForm class for customers/guests, with explicit same-fields but having required=True (edited. by default the fields would be created with required=False, since the database had blank=False in the example I gave).

    ModelAdmin does not have a way to freely-open form fields by itself - not even Forms have ways to say "Do you remember this field declared in the parent? well: I want to have it but without the required in True".