Skip to content
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

Fix of issue 218 #219

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,41 @@ Sub-factory attribute points to the model fixture of the sub-factory.
Attributes of sub-factories are injected as dependencies to the model fixture and can be overridden_ via
the parametrization.

Django Model SubFactory with a custom fixture name
--------------------------------------------------

By default, the subfactory is retrieved by using the lowercase-underscore form of the Django model class name
and invoking that fixture from the pytest request.
If you want to use a different name, you must define it in the factory class Meta and use a custom DjangoModelFactory

.. code-block:: python

from factory.django import DjangoModelFactory, DjangoOptions

class CustomFixtureFactoryOptions(DjangoOptions):
"""Allow overriding the fixture name when referencing the factory via SubFactory/RelatedFactory."""
def _build_default_options(self):
return super()._build_default_options() + [
factory.base.OptionDefault("fixture_name", None),
]


class CustomFixtureDjangoModelFactory(DjangoModelFactory):
_options_class = CustomFixtureFactoryOptions

class Meta:
abstract = True


@register
class AuthorFactory(CustomFixtureDjangoModelFactory):
class Meta:
model = Author
fixture_name = "other_author"

name = "Charles Dickens"


Related Factory
---------------

Expand Down
6 changes: 5 additions & 1 deletion pytest_factoryboy/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ def inject_into_caller(name: str, function: Callable[..., Any], locals_: Box[dic

def get_model_name(factory_class: FactoryType) -> str:
"""Get model fixture name by factory."""
overriden_fixture_name = getattr(factory_class._meta, "fixture_name", None)
if overriden_fixture_name is not None:
return overriden_fixture_name

model_cls = factory_class._meta.model

if isinstance(model_cls, str):
Expand Down Expand Up @@ -501,7 +505,7 @@ def attr_fixture(request: SubRequest, value: T) -> T:

def subfactory_fixture(request: SubRequest, factory_class: FactoryType) -> Any:
"""SubFactory/RelatedFactory fixture implementation."""
fixture = inflection.underscore(factory_class._meta.model.__name__)
fixture = inflection.underscore(get_model_name(factory_class))
return request.getfixturevalue(fixture)


Expand Down