Browse Source

Fixed #35731 -- Extended db_default docs.

This added a missing db_default reference in docs/topics/db/models.txt,
and added a reference to the DatabaseDefault object.
Yash 5 months ago
parent
commit
35ab2e0182
2 changed files with 14 additions and 0 deletions
  1. 5 0
      docs/ref/models/fields.txt
  2. 9 0
      docs/topics/db/models.txt

+ 5 - 0
docs/ref/models/fields.txt

@@ -426,6 +426,11 @@ precedence when creating instances in Python code. ``db_default`` will still be
 set at the database level and will be used when inserting rows outside of the
 set at the database level and will be used when inserting rows outside of the
 ORM or when adding a new field in a migration.
 ORM or when adding a new field in a migration.
 
 
+If a field has a ``db_default`` without a ``default`` set and no value is
+assigned to the field, a ``DatabaseDefault`` object is returned as the field
+value on unsaved model instances. The actual value for the field is determined
+by the database when the model instance is saved.
+
 ``db_index``
 ``db_index``
 ------------
 ------------
 
 

+ 9 - 0
docs/topics/db/models.txt

@@ -224,6 +224,15 @@ ones:
     object. If callable it will be called every time a new object is
     object. If callable it will be called every time a new object is
     created.
     created.
 
 
+:attr:`~Field.db_default`
+    The database-computed default value for the field. This can be a literal
+    value or a database function.
+
+    If both ``db_default`` and :attr:`Field.default` are set, ``default`` will
+    take precedence when creating instances in Python code. ``db_default`` will
+    still be set at the database level and will be used when inserting rows
+    outside of the ORM or when adding a new field in a migration.
+
 :attr:`~Field.help_text`
 :attr:`~Field.help_text`
     Extra "help" text to be displayed with the form widget. It's useful for
     Extra "help" text to be displayed with the form widget. It's useful for
     documentation even if your field isn't used on a form.
     documentation even if your field isn't used on a form.