-
-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support callables as default values in django model fields #116
Comments
I was able to reproduce this issue with the following setup: Versions
from django.db import models
from django.utils.timezone import now
from model_utils.models import TimeStampedModel
class Dummy(TimeStampedModel):
pass
class SimpleDummy(models.Model):
dt = models.DateTimeField(default=now)
from datetime import datetime
from django.test import TestCase
from model_bakery import baker
class DummyTest(TestCase):
def test_bakery_default_timestamped(self):
dummy = baker.make('my_app.Dummy', _fill_optional=True)
self.assertTrue(isinstance(dummy.created, datetime))
def test_bakery_default_simple(self):
dummy = baker.make('my_app.SimpleDummy', _fill_optional=True)
self.assertTrue(isinstance(dummy.dt, datetime)) Here, The issue here is that
class AutoCreatedField(models.DateTimeField):
... This is definitely a bug one way or another, but there are multiple things going on here. The
if field.name in self.attr_mapping:
generator = self.attr_mapping[field.name]
elif getattr(field, "choices"):
generator = random_gen.gen_from_choices(field.choices)
elif is_content_type_fk:
generator = self.type_mapping[contenttypes.models.ContentType]
elif generators.get(field.__class__):
generator = generators.get(field.__class__)
elif field.__class__ in self.type_mapping:
generator = self.type_mapping[field.__class__]
elif field.has_default():
return field.default
else:
raise TypeError("%s is not supported by baker." % field.__class__) ...which means that @berinhard The reason why |
- Also, always generate default value if `field.default` is supplied - Previously, this would not happen, since the `generator` determination was being done earlier in the same `if, elif` block - Add unit tests for generating default value of callables of varying kinds
- Also, always generate default value if `field.default` is supplied - Previously, this would not happen, since the `generator` determination was being done earlier in the same `if, elif` block - Add unit tests for generating default value of callables of varying kinds
Fixed by #117 |
…117) * [#116] Call field default when it is callable - Also, always generate default value if `field.default` is supplied - Previously, this would not happen, since the `generator` determination was being done earlier in the same `if, elif` block - Add unit tests for generating default value of callables of varying kinds * [116] Add a unit test to cover kwarg over attr_mapping * [116] Reduce complexity of custom field class * [116] Update CHANGELOG Co-authored-by: Bernardo Fontes <[email protected]>
Awesome, thanks ! |
When using django-model-utils TimeStampedModel subclass, which uses default=now callables in "created" and "modified" fields, generation of model instances (with _fill_optional=True) via baker fails:
This is probably because bakery uses the default value as is, instead of calling it (when relevant) to get the actual default value.
Versions
Python: 3.7.9
Django: 3.1.2
Model Bakery: 1.2.0
The text was updated successfully, but these errors were encountered: