Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 2 months ago by EtherealWatcher100

Why Do Excluded Fields Prevent Django CheckConstraint Validation?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

Hi! Long time user, first time poster.

I’m encountering an issue with constraint validation in Django 4.2. In our project, we use model validation for CheckConstraints to avoid duplicative validation code. However, when editing a model (MyModel) in the admin, we hide certain fields after the object is created using MyModelAdmin.get_fieldsets. Some of the hidden fields are referenced by CheckConstraints. As a result, BaseModelForm._get_validation_exclusions excludes these fields, and Model.validate_constraints ends up passing an exclusion list. When CheckConstraint.validate runs, it fails to check the constraint due to the missing fields—silently suppressing a FieldError—and later causes an IntegrityError during save.

I would have expected ValidationError to be raised and handled in the admin, not an IntegrityError when saving. My current workaround is to override MyModel.validate_constraints and not pass the exclude list to the super() call. Is this approach safe, or am I missing something? Additionally, should the Django docs further discuss this behavior?

Thanks in advance for any insights! I can provide a minimal example if needed.

2

Comments 0

Answers 0

Views 88

0 Answers

Discussion

No comments yet.