Opened 6 years ago

Last modified 6 years ago

#29466 new Bug

Textual "to" parameter of ForeignKey fails to resolve if placed in abstract model

Reported by: Vitalik Verhovodov Owned by: nobody
Component: Database layer (models, ORM) Version: 2.0
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

import django
from django.conf import settings

settings.configure(DEBUG=True)
django.setup()

from django.db import models


SHOULD_I_FAIL = True  # switch this to reproduce the bug


class ReferencedModel(models.Model):
    field = models.FloatField()

    class Meta:
        app_label = 'myapp'


ref = 'ReferencedModel' if SHOULD_I_FAIL else ReferencedModel


class AbstractModel(models.Model):
    # NOTE: only abstract models are affected
    field = models.ForeignKey(ref, on_delete=models.CASCADE)

    class Meta:
        abstract = True
        app_label = 'myapp'


class RealModel(AbstractModel):
    other_field = models.CharField(max_length=100)

    class Meta:
        app_label = 'myapp'


ffield = AbstractModel._meta.get_field('field')
# ValueError: Related model 'ReferencedModel' cannot be resolved
print(ffield.target_field)

Change History (3)

comment:1 by Tim Graham, 6 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Alexandr Tatarinov, 6 years ago

I will assume that this is intended behavior. According to #24215, abstract models does not register pending lookups into Apps registry. I have tried to implement it, but found an issue with recursive relations in abstract model - they cannot be resolved (because abstract models is not registered), but it is valid to have them, because they can be resolved in concrete children classes. So for abstract models lazy-referenced relations should not be resolved. I am not sure about this, need someone more proficient to take a look.

Last edited 6 years ago by Alexandr Tatarinov (previous) (diff)

comment:3 by Vitalik Verhovodov, 6 years ago

Sound reasonable, but what if I'm sure that all models are already loaded (e.g. during Command execution)? How can I manually resolve textual model name and target_field?

Version 0, edited 6 years ago by Vitalik Verhovodov (next)
Note: See TracTickets for help on using tickets.
Back to Top