Skip to content

Commit 8e8aa06

Browse files
authored
Merge pull request paulocheque#104 from paulocheque/model_string
[feature] G, N, T now supports <app_label>.<model_name> strings inste…
2 parents dc8a369 + 0f418f3 commit 8e8aa06

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

django_dynamic_fixture/__init__.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import warnings
88
import six
99

10+
from django.apps import apps
11+
1012
from django_dynamic_fixture.ddf import DynamicFixture, Copier, DDFLibrary, \
1113
set_pre_save_receiver, set_post_save_receiver
1214
from django_dynamic_fixture.django_helper import print_field_values, django_greater_than
@@ -64,7 +66,7 @@ def _new(model, n=1, lesson=None, persist_dependencies=True, **kwargs):
6466
Return one or many valid instances of Django Models with fields filled with auto generated or customized data.
6567
All instances will NOT be persisted in the database, except its dependencies, in case @persist_dependencies is True.
6668
67-
@model: The class of the Django model.
69+
@model: The class of the Django model. It can be a string `<app_label>.<model_name>`
6870
@n: number of instances to be created with the given configuration. Default is 1.
6971
@lesson: use a custom lesson to build the model object.
7072
@persist_dependencies: If True, save internal dependencies, otherwise just instantiate them. Default is True.
@@ -79,6 +81,8 @@ def _new(model, n=1, lesson=None, persist_dependencies=True, **kwargs):
7981
8082
Wrapper for the method DynamicFixture.new
8183
"""
84+
if isinstance(model, str):
85+
model = apps.get_model(model)
8286
kwargs = look_up_alias(**kwargs)
8387
d = fixture(**kwargs)
8488
if n == 1:
@@ -94,7 +98,7 @@ def _get(model, n=1, lesson=None, **kwargs):
9498
Return one or many valid instances of Django Models with fields filled with auto generated or customized data.
9599
All instances will be persisted in the database.
96100
97-
@model: The class of the Django model.
101+
@model: The class of the Django model. It can be a string `<app_label>.<model_name>`
98102
@n: number of instances to be created with the given configuration. Default is 1.
99103
@lesson: use a custom lesson to build the model object.
100104
@@ -108,6 +112,8 @@ def _get(model, n=1, lesson=None, **kwargs):
108112
109113
Wrapper for the method DynamicFixture.get
110114
"""
115+
if isinstance(model, str):
116+
model = apps.get_model(model)
111117
kwargs = look_up_alias(**kwargs)
112118
d = fixture(**kwargs)
113119
if n == 1:
@@ -120,7 +126,7 @@ def _get(model, n=1, lesson=None, **kwargs):
120126

121127
def _teach(model, lesson=None, **kwargs):
122128
'''
123-
@model: The class of the Django model.
129+
@model: The class of the Django model. It can be a string `<app_label>.<model_name>`
124130
@lesson: Name of custom lesson to be created.
125131
126132
@raise an CantOverrideLesson error if the same model/lesson were called twice.
@@ -136,6 +142,8 @@ def _teach(model, lesson=None, **kwargs):
136142
`Shelve` becomes `Teach`
137143
`Library` becomes `Lessons`
138144
'''
145+
if isinstance(model, str):
146+
model = apps.get_model(model)
139147
kwargs = look_up_alias(**kwargs)
140148
d = fixture(**kwargs)
141149
return d.teach(model, lesson=lesson, **kwargs)

django_dynamic_fixture/tests/test_wrappers.py

+14
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,17 @@ def test_pre_save(self):
174174
def test_post_save(self):
175175
POST_SAVE(EmptyModel, lambda x: x)
176176

177+
178+
class UsingModelNameInsteadTest(TestCase):
179+
def test_compatibility_for_new(self):
180+
instance = N('django_dynamic_fixture.ModelWithNumbers', integer=5)
181+
assert instance.integer == 5
182+
183+
def test_compatibility_for_get(self):
184+
instance = G('django_dynamic_fixture.ModelWithNumbers', integer=5)
185+
assert instance.integer == 5
186+
187+
def test_compatibility_for_teach(self):
188+
teach('django_dynamic_fixture.ModelWithDefaultValues', integer_with_default=5)
189+
instance = G('django_dynamic_fixture.ModelWithDefaultValues')
190+
assert instance.integer_with_default == 5

0 commit comments

Comments
 (0)