Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased](https://github.com/model-bakers/model_bakery/tree/main)

### Added
- Support to Field `db_default` value

### Changed

Expand Down
4 changes: 4 additions & 0 deletions model_bakery/baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
Model,
OneToOneField,
)
from django.db.models.fields import NOT_PROVIDED
from django.db.models.fields.proxy import OrderWrt
from django.db.models.fields.related import (
ReverseManyToOneDescriptor as ForeignRelatedObjectsDescriptor,
Expand Down Expand Up @@ -727,6 +728,7 @@ def generate_value(self, field: Field, commit: bool = True) -> Any: # noqa: C90

Generator Resolution Precedence Order:
-- `field.default` - model field default value, unless explicitly overwritten during baking
-- `field.db_default` - model field db default value, unless explicitly overwritten
-- `attr_mapping` - mapping per attribute name
-- `choices` -- mapping from available field choices
-- `type_mapping` - mapping from user defined type associated generators
Expand All @@ -750,6 +752,8 @@ def generate_value(self, field: Field, commit: bool = True) -> Any: # noqa: C90
if callable(field.default):
return field.default()
return field.default
elif getattr(field, "db_default", NOT_PROVIDED) != NOT_PROVIDED:
return field.db_default
elif field.name in self.attr_mapping:
generator = self.attr_mapping[field.name]
elif field.choices:
Expand Down
2 changes: 2 additions & 0 deletions tests/generic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ class Person(models.Model):
email = models.EmailField()
id_document = models.CharField(unique=True, max_length=10)
data = models.JSONField()
if django.VERSION >= (5, 0):
retirement_age = models.IntegerField(db_default=20)

try:
from django.contrib.postgres.fields import (
Expand Down
10 changes: 10 additions & 0 deletions tests/test_filling_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from os.path import abspath
from tempfile import gettempdir

import django
from django.conf import settings
from django.core.validators import (
validate_ipv4_address,
Expand Down Expand Up @@ -202,6 +203,15 @@ def test_fill_SmallIntegerField_with_a_random_number(self):
assert isinstance(small_int_field, fields.SmallIntegerField)
assert isinstance(dummy_int_model.small_int_field, int)

@pytest.mark.skipif(
django.VERSION < (5, 0),
reason="The db_default field attribute was added after 5.0",
)
def test_respects_db_default(self):
person = baker.make(models.Person, age=10)
assert person.age == 10
assert person.retirement_age == 20


@pytest.mark.django_db
class TestFillingPositiveIntFields:
Expand Down