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

Models with custom PKs not called id throw errors on schema generation. #25

Closed
keyz182 opened this issue Apr 21, 2020 · 4 comments
Closed
Labels
bug Something isn't working

Comments

@keyz182
Copy link

keyz182 commented Apr 21, 2020

When grabbing a models "ID", should probably be using model.pk - https://docs.djangoproject.com/en/dev/ref/models/instances/#the-pk-property.
A models id field is accessable through .pk, as the models PK is always accessible there. When dealing with custom PKs, this causes drf-spectacular to fall over a AttributeError: type object 'MethodCall' has no attribute 'id' error.

For example, I have a model as follows:

class MethodCall(models.Model):
    transaction = models.OneToOneField('Transaction', primary_key=True, on_delete=models.CASCADE)
    ...

As the transaction field has primary_key=True, Django doesn't generate a .id field. However transaction is still accessable via .pk.

This is where it's an issue for me:

It may also be an issue here:

@tfranzel tfranzel added the bug Something isn't working label Apr 21, 2020
@tfranzel
Copy link
Owner

good catch! i'll have a look at it.

@tfranzel
Copy link
Owner

@keyz182 i removed all references to id. it took a moment to get it right as pk is behaving slightly different from id, but on most cases there was a more elegant solution which i didn't see before.

can you confirm the issue is resolved?

@keyz182
Copy link
Author

keyz182 commented Apr 29, 2020

@tfranzel Sorry - not been in a position to test the changes as I've had to work on other things. Thank you for the fix though, I'll let you know if/when I get a chance to test.

@tfranzel
Copy link
Owner

no problem, just trying to clean up the issue page. feel free to reopen if the fix is not sufficient.

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