File: basic_usage.md

package info (click to toggle)
python-model-bakery 1.20.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 532 kB
  • sloc: python: 4,298; sh: 149; makefile: 21
file content (339 lines) | stat: -rw-r--r-- 9,669 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# Basic Usage

Let's say you have an app **shop** with a model like this:

File: **models.py**

```python
class Customer(models.Model):
    """
    Model class Customer of shop app
    """
    enjoy_jards_macale = models.BooleanField()
    name = models.CharField(max_length=30)
    email = models.EmailField()
    age = models.IntegerField()
    bio = models.TextField()
    days_since_last_login = models.BigIntegerField()
    birthday = models.DateField()
    last_shopping = models.DateTimeField()
```

To create a persisted instance, just call Model Bakery:

File: **test_models.py**

```python
#Core Django imports
from django.test import TestCase

#Third-party app imports
from model_bakery import baker

from shop.models import Customer

class CustomerTestModel(TestCase):
    """
    Class to test the model Customer
    """

    def setUp(self):
        self.customer = baker.make(Customer)
```

Importing every model over and over again is boring. So let Model Bakery import them for you:

```python
from model_bakery import baker

# 1st form: app_label.model_name
customer = baker.make('shop.Customer')

# 2nd form: model_name
product = baker.make('Product')
```

```{note}
You can only use the 2nd form on unique model names. If you have an app shop with a Product, and an app stock with a Product, you must use the app_label.model_name form.
```

```{note}
model_name is case insensitive.
```

## Model Relationships

Model Bakery also handles relationships. Let's say the customer has a purchase history:

File: **models.py**

```python
class Customer(models.Model):
    """
    Model class Customer of shop app
    """
    enjoy_jards_macale = models.BooleanField()
    name = models.CharField(max_length=30)
    email = models.EmailField()
    age = models.IntegerField()
    bio = models.TextField()
    days_since_last_login = models.BigIntegerField()
    birthday = models.DateField()
    appointment = models.DateTimeField()

class PurchaseHistory(models.Model):
    """
    Model class PurchaseHistory of shop app
    """
    customer = models.ForeignKey('Customer')
    products = models.ManyToManyField('Product')
    year = models.IntegerField()
```

You can use Model Bakery as:

```python
from django.test import TestCase

from model_bakery import baker

class PurchaseHistoryTestModel(TestCase):

    def setUp(self):
        self.history = baker.make('shop.PurchaseHistory')
        print(self.history.customer)
```

It will also create the Customer, automagically.

**NOTE: ForeignKeys and OneToOneFields** - Since Django 1.8, ForeignKey and OneToOne fields don't accept unpersisted model instances anymore. This means that if you run:

```python
baker.prepare('shop.PurchaseHistory')
```

You'll end up with a persisted "Customer" instance.

## M2M Relationships

By default, Model Bakery doesn't create related instances for many-to-many relationships.
If you want them to be created, you have to turn it on as the following:

```python
from django.test import TestCase

from model_bakery import baker

class PurchaseHistoryTestModel(TestCase):

    def setUp(self):
        self.history = baker.make('shop.PurchaseHistory', make_m2m=True)
        print(self.history.products.count())
```

## Explicit M2M Relationships

If you want to, you can prepare your own set of related object and pass it to Model Bakery. Here's an example:

```python
products_set = baker.prepare(Product, _quantity=5)
history = baker.make(PurchaseHistory, products=products_set)
```

## Explicit values for fields

By default, Model Bakery uses random values to populate the model's fields. But it's possible to explicitly set values for them as well.

```python
from django.test import TestCase

from model_bakery import baker

class CustomerTestModel(TestCase):

    def setUp(self):
        self.customer = baker.make(
            'shop.Customer',
            age=21
        )

        self.older_customer = baker.make(
            'shop.Customer',
            age=42
        )
```

You can use callable to explicitly set values as:

```python
import random

from django.test import TestCase

from model_bakery import baker

class CustomerTestModel(TestCase):
    def get_random_name(self):
        return random.choice(["Suraj Magdum", "Avadhut More", "Rohit Chile"])

    def setUp(self):
        self.customer = baker.make(
            'shop.Customer',
            age=21,
            name = self.get_random_name
        )
```

You can also use iterable to explicitly set values as:

```python
from django.test import TestCase

from model_bakery import baker

class CustomerTestModel(TestCase):
    def setUp(self):
        names = ("Onkar Awale", "Pruthviraj Patil", "Shubham Ojha")

        self.customer = baker.make(
            'shop.Customer',
            age=21,
            name = itertools.cycle(names)
        )
```

Sometimes, you have a field with an unique value and using `make` can cause random errors. Also, passing an attribute value just to avoid uniqueness validation problems can be tedious. To solve this you can define a sequence with `seq`

```python
from django.test import TestCase

from model_bakery import baker

from model_bakery.recipe import seq

class CustomerTestModel(TestCase):
    def setUp(self):
        self.customer = baker.make(
            'shop.Customer',
            age=21,
            name = seq('Joe')
        )
```

Related objects fields are also reachable by their name or related names in a very similar way as Django does with [field lookups](https://docs.djangoproject.com/en/dev/ref/models/querysets/#field-lookups):

```python
from django.test import TestCase

from model_bakery import baker

class PurchaseHistoryTestModel(TestCase):

    def setUp(self):
        self.bob_history = baker.make(
            'shop.PurchaseHistory',
            customer__name='Bob'
        )
```

## Creating Files

Model Bakery does not create files for FileField types. If you need to have the files created, you can pass the flag `_create_files=True` (defaults to `False`) to either `baker.make` or `baker.make_recipe`.

**Important**: the lib does not do any kind of file clean up, so it's up to you to delete the files created by it.

## Refreshing Instances After Creation

By default, Model Bakery does not refresh the instance after it is created and saved.
If you want to refresh the instance after it is created,
you can pass the flag `_refresh_after_create=True` to either `baker.make` or `baker.make_recipe`.
This ensures that any changes made by the database or signal handlers are reflected in the instance.

```python
from model_bakery import baker

# default behavior
customer = baker.make('shop.Customer', birthday='1990-01-01', _refresh_after_create=False)
assert customer.birthday == '1990-01-01'

customer = baker.make('shop.Customer', birthday='1990-01-01', _refresh_after_create=True)
assert customer.birthday == datetime.date(1990, 1, 1)
```

## Non persistent objects

If you don't need a persisted object, Model Bakery can handle this for you as well with the **prepare** method:

```python
from model_bakery import baker

customer = baker.prepare('shop.Customer')
```

It works like `make` method, but it doesn't persist the instance neither the related instances.

If you want to persist only the related instances but not your model, you can use the `_save_related` parameter for it:

```python
from model_bakery import baker

history = baker.prepare('shop.PurchaseHistory', _save_related=True)
assert history.id is None
assert bool(history.customer.id) is True
```

## More than one instance

If you need to create more than one instance of the model, you can use the `_quantity` parameter for it:

```python
from model_bakery import baker

customers = baker.make('shop.Customer', _quantity=3)
assert len(customers) == 3
```

It also works with `prepare`:

```python
from model_bakery import baker

customers = baker.prepare('shop.Customer', _quantity=3)
assert len(customers) == 3
```

The `make` method also accepts a parameter `_bulk_create` to use Django's [bulk_create](https://docs.djangoproject.com/en/3.0/ref/models/querysets/#bulk-create) method instead of calling `obj.save()` for each created instance.

```{note}
Django's `bulk_create` does not update the created object primary key as explained in their docs. Because of that, there's no way for model-bakery to avoid calling `save` method for all the foreign keys. But this behavior can depends on which Django version and database backend you're using.

So, for example, if you're trying to create 20 instances of a model with a foreign key using `_bulk_create` this will result in 21 queries (20 for each foreign key object and one to bulk create your 20 instances).
```

If you want to avoid that, you'll have to perform individual bulk creations per foreign keys as the following example:

```python
from model_bakery import baker

baker.prepare(User, _quantity=5, _bulk_create=True)
user_iter = User.objects.all().iterator()
baker.prepare(Profile, user=user_iter, _quantity=5, _bulk_create=True)
```

## Multi-database support

Model Bakery supports django application with more than one database.
If you want to determine which database bakery should use, you have the `_using` parameter:

```python
from model_bakery import baker

custom_db = "your_custom_db"
assert custom_db in settings.DATABASES
history = baker.make('shop.PurchaseHistory', _using=custom_db)
assert history in PurchaseHistory.objects.using(custom_db).all()
assert history.customer in Customer.objects.using(custom_db).all()
# default database tables with no data
assert not PurchaseHistory.objects.exists()
assert not Customer.objects.exists()
```