Django 2.2.2:管理员 "Manager isn't accessible via (Model) instances"
Django 2.2.2: "Manager isn't accessible via (Model) instances" on Admin
你好,
我刚刚创建了两个模型:
models.py
from django.db import models
from django.contrib.auth.models import User
import uuid
from uuid_upload_path import upload_to
from django.core.exceptions import ValidationError
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
class StorageObject(models.Model):
name = models.CharField(max_length=200)
text = models.TextField(blank=True)
slug = models.UUIDField(default=uuid.uuid4)
display = models.BooleanField(default=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
# types
file = models.FileField(upload_to=upload_to, blank=True, null=True)
image = models.ImageField(upload_to=upload_to, blank=True, null=True)
link = models.URLField(blank=True)
@cached_property
def file_type(self):
if self.file:
return 1
elif self.image:
return 2
elif self.link:
return 3
def clean(self):
if self.file and (self.image or self.link):
raise ValidationError(_('Storage Objects can only contain one storage type each'))
elif self.image and (self.link or self.file):
raise ValidationError(_('Storage Objects can only contain one storage type each'))
elif self.link and (self.file or self.image):
raise ValidationError(_('Storage Objects can only contain one storage type each'))
if not (self.link or self.image or self.file):
raise ValidationError(_('Storage Objects must contain exactly one storage type'))
def __str__(self):
return '%s (by %s)' % (self.name, self.owner)
class Collection(models.Model):
name = models.CharField(max_length=200)
active = models.BooleanField(default=True)
objects = models.ManyToManyField(StorageObject)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
正如标题所说,每当我尝试通过 admin-interface 创建 Collection
时,我都会收到 AttributeError
。
然而,创建 StorageObject
工作得很好并且得到了适当的清理。
我用谷歌搜索了这条消息,大多数人似乎忘记了某处的实例括号(如 StorageObject() )。可能是我瞎了,就是找不到错误。
所以我继续删除了我的自定义函数 file_type
和 clean
,但仍然是相同的结果。
我在这里错过了什么?感谢您的宝贵时间!
堆栈
File "C:\Users\[..]\venv\lib\site-packages\django\db\models\manager.py", line 176, in __get__
raise AttributeError("Manager isn't accessible via %s instances" % cls.__name__)
AttributeError: Manager isn't accessible via Collection instances
不要在您的模型上添加字段 objects
,这与也称为 objects
的模型 Manager
冲突。 Collection.objects
应该 return 一个 Manager
除非你决定给它一个不同的名字(见 here)。
你好,
我刚刚创建了两个模型:
models.py
from django.db import models
from django.contrib.auth.models import User
import uuid
from uuid_upload_path import upload_to
from django.core.exceptions import ValidationError
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
class StorageObject(models.Model):
name = models.CharField(max_length=200)
text = models.TextField(blank=True)
slug = models.UUIDField(default=uuid.uuid4)
display = models.BooleanField(default=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
# types
file = models.FileField(upload_to=upload_to, blank=True, null=True)
image = models.ImageField(upload_to=upload_to, blank=True, null=True)
link = models.URLField(blank=True)
@cached_property
def file_type(self):
if self.file:
return 1
elif self.image:
return 2
elif self.link:
return 3
def clean(self):
if self.file and (self.image or self.link):
raise ValidationError(_('Storage Objects can only contain one storage type each'))
elif self.image and (self.link or self.file):
raise ValidationError(_('Storage Objects can only contain one storage type each'))
elif self.link and (self.file or self.image):
raise ValidationError(_('Storage Objects can only contain one storage type each'))
if not (self.link or self.image or self.file):
raise ValidationError(_('Storage Objects must contain exactly one storage type'))
def __str__(self):
return '%s (by %s)' % (self.name, self.owner)
class Collection(models.Model):
name = models.CharField(max_length=200)
active = models.BooleanField(default=True)
objects = models.ManyToManyField(StorageObject)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
正如标题所说,每当我尝试通过 admin-interface 创建 Collection
时,我都会收到 AttributeError
。
然而,创建 StorageObject
工作得很好并且得到了适当的清理。
我用谷歌搜索了这条消息,大多数人似乎忘记了某处的实例括号(如 StorageObject() )。可能是我瞎了,就是找不到错误。
所以我继续删除了我的自定义函数 file_type
和 clean
,但仍然是相同的结果。
我在这里错过了什么?感谢您的宝贵时间!
堆栈
File "C:\Users\[..]\venv\lib\site-packages\django\db\models\manager.py", line 176, in __get__
raise AttributeError("Manager isn't accessible via %s instances" % cls.__name__)
AttributeError: Manager isn't accessible via Collection instances
不要在您的模型上添加字段 objects
,这与也称为 objects
的模型 Manager
冲突。 Collection.objects
应该 return 一个 Manager
除非你决定给它一个不同的名字(见 here)。