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

Regression in 3.5 - AttributeError: type object 'Meta' has no attribute 'model' #757

Closed
phillipuniverse opened this issue May 5, 2024 · 6 comments · Fixed by #758
Closed

Comments

@phillipuniverse
Copy link
Contributor

Looks to be caused by #722.

Django - 4.2.11
DRF - 3.14.0

  File "/Users/phillip/Library/Caches/pypoetry/virtualenvs/shipwell-backend-core-cpwchxt0-py3.11/lib/python3.11/site-packages/rest_framework/serializers.py", line 426, in run_validation
    value = self.to_internal_value(data)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/phillip/Library/Caches/pypoetry/virtualenvs/shipwell-backend-core-cpwchxt0-py3.11/lib/python3.11/site-packages/rest_framework/serializers.py", line 481, in to_internal_value
    primitive_value = field.get_value(data)
                      ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/phillip/Library/Caches/pypoetry/virtualenvs/shipwell-backend-core-cpwchxt0-py3.11/lib/python3.11/site-packages/djmoney/contrib/django_rest_framework/fields.py", line 70, in get_value
    model_meta = self.parent.Meta.model._meta
                 ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: type object 'Meta' has no attribute 'model'

I think this is an edge case when using a MoneyField with a Meta and outside of a ModelSerializer. ModelSerializer is the only case where a model is used in DRF.

This should be the reproduction case:

class NonModelSerializer(serializers.Serializer):
    invoice_amount = MoneyField(
        max_digits=constants.DIGIT_SIZE_10,
        decimal_places=constants.DIGIT_SIZE_2,
        required=False,
        allow_null=True,
    )

    class Meta:
        fields = (
            "invoice_amount"
        )
@phillipuniverse
Copy link
Contributor Author

The fix should be pretty easy by gating both self.parent.Meta and self.parent.Meta.model.

@benjaoming
Copy link
Contributor

Makes sense, and sorry about the regression. Seems that despite the test coverage, it's actually hard to change this code much.

I don't use DRF, so I hope that someone else will fix this. Would be happy to do a patch release.

@benjaoming
Copy link
Contributor

And thanks for reporting so quickly 👍

phillipuniverse added a commit to phillipuniverse/django-money that referenced this issue May 5, 2024
@phillipuniverse
Copy link
Contributor Author

@benjaoming loved how easy it was to contribute!! Candidate fix at #758.

benjaoming pushed a commit that referenced this issue May 5, 2024
@stianjensen
Copy link
Contributor

Nice to avoid this crashing with a simple change, but is a Meta class outside of using a model serializer even valid/meaningful code in drf? Is it not only used to define how to auto-generate a serailizer class from a model?

@phillipuniverse
Copy link
Contributor Author

but is a Meta class outside of using a model serializer even valid/meaningful code in drf

@stianjensen yes, there are multiple examples from the DRF docs like multi-field validators.

Also consider inheritance, where maybe a parent serializer class declares some fields as read only and a subclass declares them as not (or vice-versa).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants