Schema breaks with OneToOneField · Issue #4955 · encode/django-rest-framework (original) (raw)

Checklist

Steps to reproduce

With:

# models.py
class Alpha(models.Model):
    id = models.UUIDField(
        primary_key=True,
        default=uuid.uuid4,
        editable=False,
    )

    public_name = models.CharField(
        max_length=255,
    )


class AlphaPrivate(models.Model):
    alpha = models.OneToOneField(
        'Alpha',
        on_delete=models.CASCADE,
        primary_key=True,
        parent_link=True,
    )

    private_name = models.CharField(
        max_length=255,
    )

Attempt to render a schema with normal schema_view

Expected behavior

Renders schema.

Actual behavior

Raises UnboundLocalError

Investigation

The most direct cause of the error is that there is a Try/Except block at L531 of schemas.py that tries to get a field from the model that doesn't exist. This throws an Exception, but there is a pass in the except block which just allows things to keep going, producing the error when the next line of code attempts to access the uninstantiated variable. (Sorry for the "duh" explanation, but it is odd that there would be a pass there -- perhaps some debugging code that wasn't removed?)

Anyway, the model._meta.get_field(variable) at L532 fails because the variable being checked (which is returned by uritemplate.variables()) is 'id' and there is no field named 'id' in this model; the field being used as the actual id is 'alpha', which is a OneToOneField set as a primary key and parent link.

I'm using AlphaPrivate to maintain some private information with different permissions that would otherwise be part of Alpha -- hence the primary_key=True and parent_link=True in the above. Now I should mention that I'm unsure if I'm misconfiguring things in some way by using the above approach. That's why I haven't supplied a test: it seems just as likely that I am not doing things in the right way as I've found a legit bug.

Happy to expand further if necessary.