model_bakery icon indicating copy to clipboard operation
model_bakery copied to clipboard

Crashes when django.contrib.contentypes is not installed

Open adamchainz opened this issue 4 years ago • 0 comments

If django.contrib.contenttypes is not installed, Model Bakery fails to run. Model bakery doesn't declare a hard dependency on content types, and it doesn't look like it needs one. It just has some unconditional imports that could be guarded.

Expected behavior

Model bakery works.

Actual behavior

It crashes when importing from contenttypes:

In [1]: from model_bakery import baker

In [2]: from example.core.models import Book

In [3]: baker.make(Book)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-3-aea38cdd5d01> in <module>
----> 1 baker.make(Book)

.../site-packages/model_bakery/baker.py in make(_model, _quantity, make_m2m, _save_kwargs, _refresh_after_create, _create_files, _using, _bulk_create, **attrs)
     78     """
     79     _save_kwargs = _save_kwargs or {}
---> 80     baker = Baker.create(
     81         _model, make_m2m=make_m2m, create_files=_create_files, _using=_using
     82     )

.../site-packages/model_bakery/baker.py in create(cls, _model, make_m2m, create_files, _using)
    273         """Create the baker class defined by the `BAKER_CUSTOM_CLASS` setting."""
    274         baker_class = _custom_baker_class() or cls
--> 275         return baker_class(_model, make_m2m, create_files, _using=_using)
    276
    277     def __init__(

.../site-packages/model_bakery/baker.py in __init__(self, _model, make_m2m, create_files, _using)
    296             self.model = _model
    297
--> 298         self.init_type_mapping()
    299
    300     def init_type_mapping(self) -> None:

.../site-packages/model_bakery/baker.py in init_type_mapping(self)
    299
    300     def init_type_mapping(self) -> None:
--> 301         self.type_mapping = generators.get_type_mapping()
    302         generators_from_settings = getattr(settings, "BAKER_CUSTOM_FIELDS_GEN", {})
    303         for k, v in generators_from_settings.items():

.../site-packages/model_bakery/generators.py in get_type_mapping()
    192
    193 def get_type_mapping() -> Dict[Type, Callable]:
--> 194     from django.contrib.contenttypes.models import ContentType
    195
    196     from .gis import default_gis_mapping

.../site-packages/django/contrib/contenttypes/models.py in <module>
    131
    132
--> 133 class ContentType(models.Model):
    134     app_label = models.CharField(max_length=100)
    135     model = models.CharField(_('python model class name'), max_length=100)

.../site-packages/django/db/models/base.py in __new__(cls, name, bases, attrs, **kwargs)
    111             if app_config is None:
    112                 if not abstract:
--> 113                     raise RuntimeError(
    114                         "Model class %s.%s doesn't declare an explicit "
    115                         "app_label and isn't in an application in "

RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

Reproduction Steps

As above.

Versions

Python: 3.9.9 Django: 3.2.9 Model Bakery: 1.3.3

adamchainz avatar Nov 26 '21 11:11 adamchainz