# Proposal: To have db\_default considered in model forms

**URL:** <https://forum.djangoproject.com/t/proposal-to-have-db-default-considered-in-model-forms/33358>\
**Category:** Django Internals\
**Created:** [July 28, 2024, 3:03pm UTC](https://forum.djangoproject.com/t/proposal-to-have-db-default-considered-in-model-forms/33358 "2024-07-28T15:03:01Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![shangxiao](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shangxiao/32/10313_2.png) [@shangxiao](https://forum.djangoproject.com/u/shangxiao)\
**Post date:** [July 29, 2024, 7:19am UTC](https://forum.djangoproject.com/t/proposal-to-have-db-default-considered-in-model-forms/33358/3 "2024-07-29T07:19:03Z")

</div>

Thanks for the quick response Lily 🙏

I just realised that setting the value to `DatabaseDefault()` now syncs up nicely with this patch for a model’s full clean: [Fixed #35223 -- Made Model.full\_clean() ignore fields with db\_default when validating empty values. by bcail · Pull Request #17939 · django/django · GitHub](https://github.com/django/django/pull/17939)

There is one last thing that is blocking me from delegating to the default though - during a full clean validation of constraints is included and I have a check constraint on this particular field that is now failing because the select statement doesn’t know what `DEFAULT` means. 🤔

I couldn’t see how to workaround this in my code but altering Django to refer to the underlying expression instead during `Q.check()` seems to work:

(Note this isn’t ideal as the supplied `DatabaseDefault` ends up being wrapped in a `Value` by `Model._get_field_value_map()`.)

I might raise a separate follow-up ticket to #35223

**Edit** I realise doing `value.value` below will cause exceptions, but this is just for demonstrative purposes 😅

```auto
index 1bf396723e..4238397de2 100644
--- a/django/db/models/query_utils.py
+++ b/django/db/models/query_utils.py
@@ -120,12 +120,16 @@ class Q(tree.Node):
         """
         # Avoid circular imports.
         from django.db.models import BooleanField, Value
+ from django.db.models.expressions import DatabaseDefault
         from django.db.models.functions import Coalesce
         from django.db.models.sql import Query
         from django.db.models.sql.constants import SINGLE

+
         query = Query(None)
         for name, value in against.items():
+ if isinstance(value.value, DatabaseDefault):
+ value = value.field.db_default
             if not hasattr(value, "resolve_expression"):
                 value = Value(value)
             query.add_annotation(value, name, select=False)

```

---

_[View the full topic](https://forum.djangoproject.com/t/proposal-to-have-db-default-considered-in-model-forms/33358)._
