我目前正在模仿django官方民意调查示例来开发我自己的模型如下:
from django.db import models
class Instance(models.Model):
inst_host = models.CharField(max_length=64)
def __str__(self):
return self.inst_host
class RDMInfo(models.Model):
instance = models.ForeignKey(Instance)
filesystem = models.CharField(max_length=16)
osdevice = models.CharField(max_length=16)
lun_name = models.CharField(max_length=128)
lun_id = models.IntegerField()
wwn = models.CharField(max_length=128, unique=True)
lun_size = models.CharField(max_length=8)
def __str__(self):
return '%s %s %s %s %s %s' % (self.filesystem, self.osdevice, self.lun_name, self.lun_id, self.wwn, self.lun_size)
当我执行以下操作时发生错误:
>>> from rdm.models import Instance, RDMInfo
>>> q = Instance.objects.get(pk=1)
>>> q
<Instance: alps_mtkalpsdbp1>
>>> q.rdminfo_set.all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python3.4/site-packages/Django-1.8.2-py3.4.egg/django/db/models/query.py", line 141, in __repr__
return repr(data)
File "/usr/local/lib/python3.4/site-packages/Django-1.8.2-py3.4.egg/django/db/models/base.py", line 496, in __repr__
u = six.text_type(self)
File "/home/oracle/jrlin/smc/rdm/models.py", line 17, in __str__
return '%s %s %s %s %s %s' % (self.filesystem, self.osdevice, self.lun_name, self.lun_id, self.wwn, self.lun_size)
AttributeError: 'RDMInfo' object has no attribute 'inst_host'
在官方示例中https://docs.djangoproject.com/en/1.8/intro/tutorial01/
其问题和选择模型工作正常,我的结构几乎与示例相同
有谁知道为什么会出错?谢谢!
更新
我还按如下方式配置了django admin,这样的操作会导致错误吗?
from django.contrib import admin
from .models import Instance, RDMInfo
class RDMInline(admin.TabularInline):
model = RDMInfo
extra = 1
class RDMAdmin(admin.ModelAdmin):
fields = ['inst_host']
inlines = [RDMInline]
admin.site.register(Instance, RDMAdmin)
解决
感谢大家的评论,这是我的错误,原来我错字和不受欢迎的属性&#34; self.inst_host&#34;出现在RDMInfo模型的return命令中,在我删除之后,我应该再次执行migrate并重新打开python shell。在进行迁移并重新打开shell后,它可以找到,谢谢!