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

Crashes when django.contrib.contentypes is not installed #265

Closed
adamchainz opened this issue Nov 26, 2021 · 5 comments
Closed

Crashes when django.contrib.contentypes is not installed #265

adamchainz opened this issue Nov 26, 2021 · 5 comments
Labels
bug Something isn't working

Comments

@adamchainz
Copy link
Contributor

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

@berinhard berinhard added bug Something isn't working high priority and removed high priority labels Mar 31, 2022
@paduszyk
Copy link

Is there any reason for which this has not been addressed or even commented since 2 years?

@amureki
Copy link
Collaborator

amureki commented Apr 17, 2024

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

@adamchainz
Copy link
Contributor Author

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.

@berinhard
Copy link
Member

berinhard commented Apr 18, 2024

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.

@paduszyk
Copy link

paduszyk commented May 8, 2024

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! ☮️

amureki added a commit that referenced this issue Aug 9, 2024
* origin/main:
  Bump 1.19.0
  Fix #483 -- Add Django 5.1 support (#485)
  Bump 1.18.3
  Refs #416 -- Allow combination of GFK and `_fill_optional` (#438)
  Bump 1.18.2
  Update ruff CI syntax (#481)
  Fix #28 -- allow make_recipe to work with _quantity (#480)
  Bump 1.18.1
  Replace expensive `count()` with cheap `exists()` (#478)
  Update CI Python version to 3.12
  Delete hard action requirement for a changelog
  Bump 1.18.0
  Fix #265 -- drop hard dependency on `contenttypes` framework (#476)
  Bump GitHub Actions artifacts to v4 (#470)
  Drop Django 3.2 support (#475)
  Bump actions/setup-python from 4 to 5 (#466)
  Drop Django 4.1 support (reached end of life) (#465)
  Support Django 5.0 (#464)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants