Skip to content

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

@keyz182

Description

@keyz182

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions