Changes between Initial Version and Version 3 of Ticket #35731


Ignore:
Timestamp:
Sep 5, 2024, 7:15:29 AM (3 months ago)
Author:
Natalia Bidart
Comment:

Overall, I agree with Simon analysis. Specifically, the docs for db_default say:

The database-computed default value for this field.

So, when doing val = IntegerField(db_default=10); foo = Foo(); foo.val, I would expect anything but the value that was set as db_default (10), since the code "hasn't gone to the db yet". Intuitively I would have expected an exception such as "you can't have a value since DB hasn't been reached yet", but getting a instance of a "db value promise" is even better and clearer.

OTOH, I do agree that we may be lacking docs about this. In particular, I think we should:

  1. add a small note to the existing ref/models/fields.txt docs about db_default, and
  2. add a richer section to the topics/db/models.txt for :attr:~Field.db_default since most of the Field options are documented there but db_default is missing.

Accepting and re-purposing this ticket with that goal in mind.

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #35731

    • Property Cc Simon Charette Lily Foote added
    • Property Owner set to YashRaj1506
    • Property Status newassigned
    • Property Triage Stage UnreviewedAccepted
    • Property Summary Fields with db_default, and without default, are initialized to instance of DatabaseDefaultExtend documentation about db_default and DatabaseDefault
    • Property Version 5.0dev
  • Ticket #35731 – Description

    initial v3  
     1I would be helpful if the existing docs at `ref/models/fields.txt`, when describing `db_default`, would mention `DatabaseDefault`.
     2Also, the docs `topics/db/models.txt` describe most of the `Field` options but `db_default` is missing, so ideally we would add a section for it with examples, including some that would show how and when `DatabaseDefault` is returned/used.
     3
     4==== Original report ====
     5
    16For example, if client code creates a model `Foo` with `val = IntegerField(db_default=10)`, does `foo = Foo()`, and accesses `foo.val`, they get an instance of `django.db.models.expressions.DatabaseDefault`.
    27
Back to Top