In django admin, there are fields I'd like to require if a model is being edited standalone. If it is inline, I don't want them to be required. Is there a way to do this?
相关问题
- Django __str__ returned non-string (type NoneType)
- Django & Amazon SES SMTP. Cannot send email
- Django check user group permissions
- Django restrict pages to certain users
- UnicodeEncodeError with attach_file on EmailMessag
相关文章
- Profiling Django with PyCharm
- Why doesn't Django enforce my unique_together
- MultiValueDictKeyError in Django admin
- Django/Heroku: FATAL: too many connections for rol
- Django is sooo slow? errno 32 broken pipe? dcramer
- Django: Replacement for the default ManyToMany Wid
- Upgrading transaction.commit_manually() to Django
- UnicodeEncodeError when saving ImageField containi
While Daniel Roseman's answer works, it's not the best solution. It requires a bit of code duplication by having to re-declare the form field. For example, if you had a
verbose_name
on that field, you would also have to addlabel='My verbose_name already set on model'
to theCharField
line, since re-declaring the whole field basically erases everything set on your model for that field.The better approach is to override the form's
__init__
method and explicitly setfield.required
toTrue
orFalse
there.Sure. Just define a custom form, with your required field overridden to set required=True, and use it in your admin class.
So here MyAdmin is using the overridden form, but MyInlineAdmin isn't.