Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Commit

Permalink
Merge pull request #296 from vandersonmota/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
berinhard committed Nov 17, 2016
2 parents 6fad6f2 + 485be22 commit f41cf43
Show file tree
Hide file tree
Showing 17 changed files with 810 additions and 615 deletions.
12 changes: 3 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ language: python
python:
- "2.6"
- "2.7"
- "3.2"
- "3.4"
- "3.5"
env:
- DJANGO=django14
- DJANGO=django15
- DJANGO=django16
- DJANGO=django17
- DJANGO=django18
- DJANGO=django19
- DJANGO=django110
matrix:
exclude:
- python: "2.6"
Expand All @@ -21,16 +19,12 @@ matrix:
env: DJANGO=django18
- python: "2.6"
env: DJANGO=django19
- python: "3.2"
env: DJANGO=django14
- python: "3.2"
env: DJANGO=django19
- python: "2.6"
env: DJANGO=django110
- python: "3.4"
env: DJANGO=django14
- python: "3.5"
env: DJANGO=django14
- python: "3.5"
env: DJANGO=django15
- python: "3.5"
env: DJANGO=django16
- python: "3.5"
Expand Down
8 changes: 8 additions & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
1.3.1
-----
- Dropped Django 1.5 support
- Custom parameters passing to Model.save method
- New parameter to save related instance on Mommy.prepare
- Lookup from relationship related names
- New API to add custom generators

1.3.0
-----
- Django 1.10 support
Expand Down
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ Usage and Info
==============

* http://model-mommy.readthedocs.org/


Maintainers
===========

* [Vanderson Mota (creator)](https://github.com/vandersonmota/)
* [Bernardo Fontes](https://github.com/berinhard/)
1 change: 1 addition & 0 deletions dev_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-r requirements.txt
mock>=1.0.1
sphinx
django-test-without-migrations==0.4
15 changes: 13 additions & 2 deletions docs/source/basic_usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ File: test_model.py ::
age=6
)

Related objects attributes are also reachable:
Related objects attributes are also reachable by their name or related names:

File: test_model.py ::

Expand Down Expand Up @@ -235,6 +235,7 @@ File: test_model.py ::
owner__name='Bob'
)


Non persistent objects
----------------------

Expand All @@ -246,7 +247,17 @@ If you don't need a persisted object, *Mommy* can handle this for you as well:
kid = mommy.prepare('family.Kid')
It works like `make`, but it doesn't persist the instance.
It works like `make`, 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:

.. code-block:: python
from model_mommy import mommy
dog = mommy.prepare('family.Dog', _save_related=True)
assert dog.id is None
assert bool(dog.owner.id) is True
More than one instance
----------------------
Expand Down
1 change: 1 addition & 0 deletions docs/source/deprecation_warnings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Because of the changes of model_mommy's API, the following methods are deprecate
* `mommy.prepare_one` -> should use the method `mommy.prepare` instead
* `mommy.make_many` -> should use the method `mommy.make` with the `_quantity` parameter instead
* `mommy.make_many_from_recipe` -> should use the method `mommy.make_recipe` with the `_quantity` parameter instead
* `MOMMY_CUSTOM_FIELDS_GEN` -> should use the method `mommy.generators.add` instead
43 changes: 31 additions & 12 deletions docs/source/how_mommy_behaves.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ You can override this behavior by:
.. code-block:: python
# from "Basic Usage" page, assume all fields either null=True or blank=True
from .models import Kid
from .models import Kid
from model_mommy import mommy
kid = mommy.make(Kid, happy=True, bio='Happy kid')
2. Passing `_fill_optional` with a list of fields to fill with random data

.. code-block:: python
Expand Down Expand Up @@ -54,34 +54,33 @@ Custom fields
-------------

Model-mommy allows you to define generators methods for your custom fields or overrides its default generators.
This could be achieved by specifing a dict on settings with keys defining the fields and values the generator functions.
This could be achieved by specifing the field and generator function for the `generators.add` function.
Both can be the real python objects imported in settings or just specified as import path string.

Examples:

.. code-block:: python
# on your settings.py file:
from model_mommy import mommy
def gen_func():
return 'value'
MOMMY_CUSTOM_FIELDS_GEN = {
'test.generic.fields.CustomField': gen_func,
}
mommy.generators.add('test.generic.fields.CustomField', gen_func)
.. code-block:: python
# in the module code.path:
def gen_func():
return 'value'
# in your settings.py file:
MOMMY_CUSTOM_FIELDS_GEN = {
'test.generic.fields.CustomField': 'code.path.gen_func',
}
# in your tests.py file:
from model_mommy import mommy
mommy.generatos.add('test.generic.fields.CustomField', 'code.path.gen_func')
Customizing Mommy
-------------
-----------------

In some rare cases, you might need to customize the way Mommy behaves.
This can be achieved by creating a new class and specifying it in your settings files. It is likely that you will want to extend Mommy, however the minimum requirement is that the custom class have `make` and `prepare` functions.
Expand All @@ -102,3 +101,23 @@ Examples:
# in your settings.py file:
MOMMY_CUSTOM_CLASS = 'code.path.CustomMommy'
Save method custom parameters
-----------------------------

If you have overwritten the `save` method for a model, you can pass custom parameters to it using model mommy. Example:

.. code-block:: python
class ProjectWithCustomSave(models.Model)
# some model fields
created_by = models.ForeignKey(settings.AUTH_USER_MODEL)
def save(self, user, *args, **kwargs):
self.created_by = user
return super(ProjectWithCustomSave, self).save(*args, **kwargs)
#with model mommy:
user = mommy.make(settings.AUTH_USER_MODEL)
project = mommy.make(ProjectWithCustomSave, _save_kwargs={'user': user})
assert user == project.user

0 comments on commit f41cf43

Please sign in to comment.