admin管理员组文章数量:1291122
We are migrating our django app from django==3.2.25
to django==5.1.6
. OneToOneField, ManyToManyField
are giving errors on revers lookup.
Create fresh setup.
python -m venv app_corp_1.0.X
./app_corp_1.0.X/bin/pip install django
mkdir djangotutorial
./app_corp_1.0.X/bin/django-admin startproject mysite djangotutorial
./app_corp_1.0.X/bin/python djangotutorial/manage.py shell
I have models as below.
from django.db import models
class Switch(models.Model):
fqdn = models.CharField(max_length=45, unique=True)
class Meta:
managed = False
db_table = 'Switch'
app_label = 'myapp_models'
class ConfigState(models.Model):
switch = models.OneToOneField(Switch, models.CASCADE, db_column='switch', primary_key=True,
related_name='config_state')
class Meta:
managed = False
db_table = 'ConfigState'
app_label = 'myapp_models'
class EdgeSwitch(models.Model):
switch = models.OneToOneField(Switch, models.CASCADE, db_column='switch', primary_key=True,
related_name='edge_switch')
class Meta:
managed = False
db_table = 'EdgeSwitch'
app_label = 'myapp_models'
When I try to get backward lookup query in DJango==3.X
it works.
>>> print(EdgeSwitch.objects.filter(switch__config_state=1).query)
SELECT `EdgeSwitch`.`switch`, `EdgeSwitch`.`cluster`, `EdgeSwitch`.`sequence`, `EdgeSwitch`.`position`, `EdgeSwitch`.`role`, `EdgeSwitch`.`span`, `EdgeSwitch`.`loopback_v4`, `EdgeSwitch`.`loopback_v6` FROM `EdgeSwitch` INNER JOIN `Switch` ON (`EdgeSwitch`.`switch` = `Switch`.`id`) INNER JOIN `ConfigState` ON (`Switch`.`id` = `ConfigState`.`switch`) WHERE `ConfigState`.`switch` = 1
Same code gives error in DJango==5.X
>>> print(EdgeSwitch.objects.filter(switch__config_state=1).query)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/user/virtualenvs/app_corp_1.0.X/lib/python3.12/site-packages/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/virtualenvs/app_corp_1.0.X/lib/python3.12/site-packages/django/db/models/query.py", line 1476, in filter
return self._filter_or_exclude(False, args, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/virtualenvs/app_corp_1.0.X/lib/python3.12/site-packages/django/db/models/query.py", line 1494, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "/home/user/virtualenvs/app_corp_1.0.X/lib/python3.12/site-packages/django/db/models/query.py", line 1501, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "/home/user/virtualenvs/app_corp_1.0.X/lib/python3.12/site-packages/django/db/models/sql/query.py", line 1609, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/virtualenvs/app_corp_1.0.X/lib/python3.12/site-packages/django/db/models/sql/query.py", line 1641, in _add_q
child_clause, needed_inner = self.build_filter(
^^^^^^^^^^^^^^^^^^
File "/home/user/virtualenvs/app_corp_1.0.X/lib/python3.12/site-packages/django/db/models/sql/query.py", line 1555, in build_filter
condition = self.build_lookup(lookups, col, value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/virtualenvs/app_corp_1.0.X/lib/python3.12/site-packages/django/db/models/sql/query.py", line 1379, in build_lookup
lhs = self.try_transform(lhs, lookup_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/virtualenvs/app_corp_1.0.X/lib/python3.12/site-packages/django/db/models/sql/query.py", line 1423, in try_transform
raise FieldError(
django.core.exceptions.FieldError: Unsupported lookup 'config_state' for OneToOneField or join on the field not permitted.
How to make it working as it was working before?
We are migrating our django app from django==3.2.25
to django==5.1.6
. OneToOneField, ManyToManyField
are giving errors on revers lookup.
Create fresh setup.
python -m venv app_corp_1.0.X
./app_corp_1.0.X/bin/pip install django
mkdir djangotutorial
./app_corp_1.0.X/bin/django-admin startproject mysite djangotutorial
./app_corp_1.0.X/bin/python djangotutorial/manage.py shell
I have models as below.
from django.db import models
class Switch(models.Model):
fqdn = models.CharField(max_length=45, unique=True)
class Meta:
managed = False
db_table = 'Switch'
app_label = 'myapp_models'
class ConfigState(models.Model):
switch = models.OneToOneField(Switch, models.CASCADE, db_column='switch', primary_key=True,
related_name='config_state')
class Meta:
managed = False
db_table = 'ConfigState'
app_label = 'myapp_models'
class EdgeSwitch(models.Model):
switch = models.OneToOneField(Switch, models.CASCADE, db_column='switch', primary_key=True,
related_name='edge_switch')
class Meta:
managed = False
db_table = 'EdgeSwitch'
app_label = 'myapp_models'
When I try to get backward lookup query in DJango==3.X
it works.
>>> print(EdgeSwitch.objects.filter(switch__config_state=1).query)
SELECT `EdgeSwitch`.`switch`, `EdgeSwitch`.`cluster`, `EdgeSwitch`.`sequence`, `EdgeSwitch`.`position`, `EdgeSwitch`.`role`, `EdgeSwitch`.`span`, `EdgeSwitch`.`loopback_v4`, `EdgeSwitch`.`loopback_v6` FROM `EdgeSwitch` INNER JOIN `Switch` ON (`EdgeSwitch`.`switch` = `Switch`.`id`) INNER JOIN `ConfigState` ON (`Switch`.`id` = `ConfigState`.`switch`) WHERE `ConfigState`.`switch` = 1
Same code gives error in DJango==5.X
>>> print(EdgeSwitch.objects.filter(switch__config_state=1).query)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/user/virtualenvs/app_corp_1.0.X/lib/python3.12/site-packages/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/virtualenvs/app_corp_1.0.X/lib/python3.12/site-packages/django/db/models/query.py", line 1476, in filter
return self._filter_or_exclude(False, args, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/virtualenvs/app_corp_1.0.X/lib/python3.12/site-packages/django/db/models/query.py", line 1494, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "/home/user/virtualenvs/app_corp_1.0.X/lib/python3.12/site-packages/django/db/models/query.py", line 1501, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "/home/user/virtualenvs/app_corp_1.0.X/lib/python3.12/site-packages/django/db/models/sql/query.py", line 1609, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/virtualenvs/app_corp_1.0.X/lib/python3.12/site-packages/django/db/models/sql/query.py", line 1641, in _add_q
child_clause, needed_inner = self.build_filter(
^^^^^^^^^^^^^^^^^^
File "/home/user/virtualenvs/app_corp_1.0.X/lib/python3.12/site-packages/django/db/models/sql/query.py", line 1555, in build_filter
condition = self.build_lookup(lookups, col, value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/virtualenvs/app_corp_1.0.X/lib/python3.12/site-packages/django/db/models/sql/query.py", line 1379, in build_lookup
lhs = self.try_transform(lhs, lookup_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/virtualenvs/app_corp_1.0.X/lib/python3.12/site-packages/django/db/models/sql/query.py", line 1423, in try_transform
raise FieldError(
django.core.exceptions.FieldError: Unsupported lookup 'config_state' for OneToOneField or join on the field not permitted.
How to make it working as it was working before?
Share Improve this question edited Feb 14 at 16:29 Neel asked Feb 13 at 17:10 NeelNeel 21.3k17 gold badges100 silver badges162 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 1The app_label
is probably the culprit: you registered the models with an app_label
that is not in INSTALLED_APPS
, and as a result these are not registered.
If I change the app_label
to one in INSTALLED_APPS
, it works. But you don't need to mention app_label
: if you leave it, it will automatically install it in the app the models.py
is in.
本文标签: pythonbackward lookup is not working in django 5xStack Overflow
版权声明:本文标题:python - backward lookup is not working in django 5.x - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741514647a2382809.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
INSTALLED_APPS
. – Chukwujiobi Canon Commented Feb 14 at 17:12