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

Django 5.1b1: GenericForeignKey exception #483

Closed
james-certn opened this issue Jul 19, 2024 · 3 comments
Closed

Django 5.1b1: GenericForeignKey exception #483

james-certn opened this issue Jul 19, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@james-certn
Copy link

I understand that this is happening with a beta release of Django, but I thought important to report for future compatibility. Maybe something in Django needs to change, maybe something in model-bakery (or maybe our code!).

With a model like this:

class MyModel(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.UUIDField()
    content_object = GenericForeignKey("content_type", "object_id")

... and a test using model-bakery like this:

 my_obj = baker.make(MyModel)

... we are seeing a pytest output that looks like the following:

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../venv/lib/python3.11/site-packages/model_bakery/baker.py:142: in make
    return baker.make(
../venv/lib/python3.11/site-packages/model_bakery/baker.py:401: in make
    return self._make(**params)
../venv/lib/python3.11/site-packages/model_bakery/baker.py:466: in _make
    self.model_attrs[field.name] = self.generate_value(
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <model_bakery.baker.Baker object at 0xffff623e29d0>
field = <django.contrib.contenttypes.fields.GenericForeignKey: content_object>
commit = True

    def generate_value(self, field: Field, commit: bool = True) -> Any:  # noqa: C901
        """Call the associated generator with a field passing all required args.
    
        Generator Resolution Precedence Order:
        -- `field.default` - model field default value, unless explicitly overwritten during baking
        -- `attr_mapping` - mapping per attribute name
        -- `choices` -- mapping from available field choices
        -- `type_mapping` - mapping from user defined type associated generators
        -- `default_mapping` - mapping from pre-defined type associated
           generators
    
        `attr_mapping` and `type_mapping` can be defined easily overwriting the
        model.
        """
        is_content_type_fk = False
        if BAKER_CONTENTTYPES:
            is_content_type_fk = isinstance(field, ForeignKey) and issubclass(
                self._remote_field(field).model, contenttypes_models.ContentType
            )
        # we only use default unless the field is overwritten in `self.rel_fields`
        if field.has_default() and field.name not in self.rel_fields:
            if callable(field.default):
                return field.default()
            return field.default
        elif field.name in self.attr_mapping:
            generator = self.attr_mapping[field.name]
        elif field.choices:
            generator = random_gen.gen_from_choices(field.choices)
        elif is_content_type_fk:
            generator = self.type_mapping[contenttypes_models.ContentType]
        elif generators.get(field.__class__):
            generator = generators.get(field.__class__)
        elif field.__class__ in self.type_mapping:
            generator = self.type_mapping[field.__class__]
        else:
>           raise TypeError(
                f"field {field.name} type {field.__class__} is not supported by baker."
            )
E           TypeError: field content_object type <class 'django.contrib.contenttypes.fields.GenericForeignKey'> is not supported by baker.

../venv/lib/python3.11/site-packages/model_bakery/baker.py:725: TypeError

Versions

  • Python: 3.11
  • Django: 5.1b1
  • Model Bakery 1.18.2

To be clear, the following works with Django 5.0.x, same python/bakery versions.

@amureki
Copy link
Collaborator

amureki commented Jul 26, 2024

Hey @james-certn !

Thank you for the report!
It is indeed to keep the future compatibility, I will take a look at this.

@amureki amureki added the bug Something isn't working label Jul 26, 2024
@amureki
Copy link
Collaborator

amureki commented Aug 8, 2024

Okay, I've found a suspect commit:
django/django@6002df0

The signature changed a bit, we would need to adapt our detection of GFK.

amureki added a commit that referenced this issue Aug 8, 2024
amureki added a commit that referenced this issue Aug 8, 2024
@amureki amureki self-assigned this Aug 8, 2024
amureki added a commit that referenced this issue Aug 8, 2024
amureki added a commit that referenced this issue Aug 9, 2024
@amureki amureki closed this as completed in b4fb894 Aug 9, 2024
@amureki
Copy link
Collaborator

amureki commented Aug 9, 2024

Once again, thank you for the report.
This unveiled an older bug in GFK handling.
It was fixed and official Django 5.1 support was released in 1.19.0:
https://pypi.org/project/model-bakery/1.19.0/

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

2 participants