models.py的片段
class Hardware_type(models.Model):
type = models.CharField(blank = False, max_length = 50, verbose_name="Type")
description = models.TextField(blank = True, verbose_name="Description")
slug = models.SlugField(unique = True, max_length = 255, verbose_name = "Slug")
class Software_type(models.Model):
type = models.CharField(blank = False, max_length = 50, verbose_name="Type")
description = models.TextField(blank = True, verbose_name="Description")
slug = models.SlugField(unique = True, max_length = 255, verbose_name = "Slug")
现在
>>> sw = Software_type.objects.get(slug='unix')
>>> sw
<Software_type: Unix>
>>> hw = Hardware_type.objects.get(slug='printer')
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: type object 'Hardware_type' has no attribute 'objects'
我不明白为什么会这样。有人能帮我吗?
编辑:
很抱歉,没有发送所有代码-问题已解决。 在另一节课上
hardware_type = models.ManyToManyField(Hardware_type, verbose_name="Hardware Type")
从硬件类型更改为硬件类型后-工作正常 我不知道那会引起这个问题。
原来只是在django控制台开始工作
后来我注意到forms.py中有一些旧代码
因此,它不起作用,这是一个糟糕的一天命名类等
如果将自定义管理器添加到模型中,则不会在
objects
处创建默认管理器。要么自己将其添加到类定义中,要么坚持使用自定义管理器。你的代码对我有用:
但是,使用关键字type
可能有点危险,您可能希望避免使用它。相关问题 更多 >
编程相关推荐