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

ModelSerializer fields with source parameter no longer work (regression in 3.5) #762

Closed
dariusmazeika opened this issue May 7, 2024 · 3 comments · Fixed by #763
Closed

Comments

@dariusmazeika
Copy link
Contributor

field = model_meta.get_field(self.field_name)

Seems to be introduced with #722.

django - 5.0.3
drf - 3.14.0
djmoney - 3.5.1
python - 3.11

ModelSerializer with MoneyField which has source passed in fails to resolve field_name from source

  File "/usr/local/lib/python3.11/site-packages/rest_framework/serializers.py", line 227, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/rest_framework/serializers.py", line 426, in run_validation
    value = self.to_internal_value(data)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/rest_framework/serializers.py", line 481, in to_internal_value
    primitive_value = field.get_value(data)
                      ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/djmoney/contrib/django_rest_framework/fields.py", line 43, in get_value
    field = model_meta.get_field(self.field_name)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/models/options.py", line 683, in get_field
    raise FieldDoesNotExist(
django.core.exceptions.FieldDoesNotExist: Item has no field named 'price_amount'

Can be reproduced with following model and serializer:

class Item(models.Model):
    price = MoneyField(max_digits=16)

class ItemModelSerializer(serializers.ModelSerializer):
    price_amount = MoneyField(source="price",
        max_digits=16,
        decimal_places=2,
    )

    class Meta:
        model = Item
        fields = ["price_amount", "price_amount_currency"]
@dariusmazeika
Copy link
Contributor Author

I have a simple fix with test, just not sure how to contribute, probably need permission.
Line in question should be:
field = model_meta.get_field(self.source)

@benjaoming
Copy link
Contributor

@dariusmazeika that looks like a good fix! There are already quite a bit of test cases, and with your addition, one could hope that it's getting closer to an exhaustive pattern.

If you fork the repository and open a PR, that's sufficient. There are some notes in the README about how to run tests locally.

@dariusmazeika
Copy link
Contributor Author

@benjaoming done. I placed the test under same test case that was introduced with #722 as I guess it's related, hope that's all good :)

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.

2 participants