Crashes when django.contrib.contentypes is not installed
adamchainz opened this issue · 5 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
Is there any reason for which this has not been addressed or even commented since 2 years?
Is there any reason for which this has not been addressed or even commented since 2 years?
Hey @paduszyk and @adamchainz
Sorry that it took an insane amount of time to respond.
I am now drafting a version (#476) which is not depending on contenttypes
framework.
While I still need to clean it up and test it, I'd love if you could potentially try it out by installing it via
python -m pip install 'model_bakery @ git+https://github.com/model-bakers/model_bakery@265/optional-contenttypes'
And see if that solves your issue. If that is not possible, or not in your interest, this is totally fine. :)
Best,
Rust
I am afraid I don’t have time or interest to test this, since I am not actively using Model Bakery. But good work. Don’t stress or worry about timelines in open source! You’re doing good work. @paduszyk ’s comment was outright rude and entitled.
Thanks for being thoughtful @adamchainz. It' super annoying when people treats OS as regular product when the developers have to maintain themselves on their jobs and work this on their spare time.
I'm sorry you misunderstood my intentions.
I didn't know that using question marks on GitHub may be perceived as rude.
To be clear: I respect all of you guys doing a great job for the open-source and the Python/Django community!
Peace! ☮️