[Django] #29466: Textual "to" parameter of ForeignKey fails to resolve if placed in abstract model

8 views
Skip to first unread message

Django

unread,
Jun 2, 2018, 10:00:08 PM6/2/18
to django-...@googlegroups.com
#29466: Textual "to" parameter of ForeignKey fails to resolve if placed in abstract
model
-------------------------------------+-------------------------------------
Reporter: Vitalik | Owner: nobody
Verhovodov |
Type: Bug | Status: new
Component: Database | Version: 2.0
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
{{{#!python
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)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29466>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 8, 2018, 4:36:28 PM6/8/18
to django-...@googlegroups.com
#29466: Textual "to" parameter of ForeignKey fails to resolve if placed in abstract
model
-------------------------------------+-------------------------------------
Reporter: Vitalik Verhovodov | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


Comment:

Reproduced at 741792961815cf4a95c5ce8ab590dfc7700c6153.

--
Ticket URL: <https://code.djangoproject.com/ticket/29466#comment:1>

Django

unread,
Jun 10, 2018, 12:22:35 PM6/10/18
to django-...@googlegroups.com
#29466: Textual "to" parameter of ForeignKey fails to resolve if placed in abstract
model
-------------------------------------+-------------------------------------
Reporter: Vitalik Verhovodov | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Alexander Tatarinov):

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 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 would not
change the triage stage as I am not sure about this.

--
Ticket URL: <https://code.djangoproject.com/ticket/29466#comment:2>

Django

unread,
Jun 11, 2018, 2:22:00 AM6/11/18
to django-...@googlegroups.com
#29466: Textual "to" parameter of ForeignKey fails to resolve if placed in abstract
model
-------------------------------------+-------------------------------------
Reporter: Vitalik Verhovodov | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Vitalik Verhovodov):

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?

--
Ticket URL: <https://code.djangoproject.com/ticket/29466#comment:3>

Reply all
Reply to author
Forward
0 new messages