反向关联报错AttributeError: ‘ForeignKey‘ object has no attribute ‘rel‘从django源码找答案~
程序员文章站
2022-05-30 22:50:48
AttributeError: ‘ForeignKey’ object has no attribute ‘rel’因为django版本不一样旧版本可以调用ForeignKey.rel.model.objects.all()新版本中这里的rel属性被设置为ReverseManyToOneDescriptor的实例才能使用在源码中有如下信息:class ReverseManyToOneDescriptor: """ Accessor to the related objects ma...
AttributeError: ‘ForeignKey’ object has no attribute ‘rel’
因为django版本不一样
旧版本可以调用ForeignKey.rel.model.objects.all()
新版本中
这里的rel属性被设置为ReverseManyToOneDescriptor的实例才能使用在源码中有如下信息:
class ReverseManyToOneDescriptor:
"""
Accessor to the related objects manager on the reverse side of a
many-to-one relation.
In the example::
class Child(Model):
parent = ForeignKey(Parent, related_name='children')
``Parent.children`` is a ``ReverseManyToOneDescriptor`` instance.
"""
def __init__(self, rel):
self.rel = rel
在这里设置了反向多对一的时候只有这个类的实例才能用rel,而ForeignObject想用rel属性只能根据下边的方法去寻找。
from django.db import models
在django中建表用到的外键对象(models.ForeignKey),先看一下ForeignKey的__mro__(方法解析顺序)
models.ForeignKey.__mro__
(<class 'django.db.models.fields.related.ForeignKey'>, <class 'django.db.models.fields.related.ForeignObject'>, <class 'django.db.models.fields.related.RelatedField'>, <class 'django.db.models.fields.mixins.FieldCacheMixin'>, <class 'django.db.models.fields.Field'>, <class 'django.db.models.query_utils.RegisterLookupMixin'>, <class 'object'>)
先去第一个父类中找
class ForeignKey(ForeignObject):
def __init__(self, to, on_delete, related_name=None, related_query_name=None,
limit_choices_to=None, parent_link=False, to_field=None,
db_constraint=True, **kwargs):
...
kwargs['rel'] = self.rel_class(
self, to, to_field,
related_name=related_name,
related_query_name=related_query_name,
limit_choices_to=limit_choices_to,
parent_link=parent_link,
on_delete=on_delete,
)
super().__init__(to, on_delete, from_fields=['self'], to_fields=[to_field], **kwargs)
在这里rel被丢给父级的初始化方法,进去继续寻找
class ForeignObject(RelatedField):
def __init__(self, to, on_delete, from_fields, to_fields, rel=None, related_name=None,
related_query_name=None, limit_choices_to=None, parent_link=False,
swappable=True, **kwargs):
...
if rel is None:
rel = self.rel_class(
self, to,
related_name=related_name,
related_query_name=related_query_name,
limit_choices_to=limit_choices_to,
parent_link=parent_link,
on_delete=on_delete,
)
super().__init__(rel=rel, **kwargs)
在ForeignObject中,因为kwargs中设置了rel所以这里的if rel is None不会被执行,
rel没有改变,继续传递给父级的初始化方法
class Field(RegisterLookupMixin):
def __init__(self, verbose_name=None, name=None, primary_key=False,
max_length=None, unique=False, blank=False, null=False,
db_index=False, rel=None, default=NOT_PROVIDED, editable=True,
serialize=True, unique_for_date=None, unique_for_month=None,
unique_for_year=None, choices=None, help_text='', db_column=None,
db_tablespace=None, auto_created=False, validators=(),
error_messages=None):
...
self.remote_field = rel
终于被赋值了 所以在这里想用的话就要用:
ForeignKey.remote_field.model.objects.all()
本文地址:https://blog.csdn.net/weixin_43832745/article/details/107350861