-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Detect underlying field for OneToOne primary key #8371
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
base: master
Are you sure you want to change the base?
Detect underlying field for OneToOne primary key #8371
Conversation
@@ -1234,6 +1234,10 @@ def build_standard_field(self, field_name, model_field): | |||
if model_field.one_to_one and model_field.primary_key: | |||
field_class = self.serializer_related_field | |||
field_kwargs['queryset'] = model_field.related_model.objects | |||
pk_field = field_mapping[model_field.foreign_related_fields[0]] | |||
pk_field_kwargs = get_field_kwargs(field_name, model_field.foreign_related_fields[0]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the field_name
be extracted from the foreign_related_fields[0]
here?
b1d737e
to
1129a8d
Compare
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
bump |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
bump |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Okay - I think this makes sense.(?) Is there an example that's more obvious than a |
|
||
def test_one_to_one_field(self): | ||
class MyModelA(models.Model): | ||
id = models.DecimalField(max_digits=4, decimal_places=2, primary_key=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
decimal field as primary key? what are some actual use case of that?
instead o using decimal field as primary key, can you try something else like UUID field as primary key here in test case? |
Description
When using a OneToOneField as a primary key it does not get serialized in the same way as the "parent" model. E.g.
The following serializations would result
This PR sets the
pk_field
attribute of thePrimaryKeyRelatedField
to ensure serialization is consistent with the "parent" model. This is also in line with the OpenApi schema that would be generated.