Django属性错误

时间:2013-10-24 19:41:13

标签: python mysql django apache sites

我在使用Django项目时遇到了麻烦。 Python 2.7,apache 2.2,mysql数据库; 这是models.py的代码

from django.db import models
from django.contrib import admin

class Item(models.Model):
    name = models.CharField(max_length=150)
    description = models.TextField()

    class Meta:
        ordering = ['name']

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return 'item_detail', None, {'object_id': self.id}


class Photo(models.Model):
    item = models.ForeignKey('Item')
    title = models.CharField(max_length=100)
    image = models.ImageField(upload_to='photos')
    caption = models.CharField(max_length=250, blank=True)

    class Meta:
        ordering = ['title']

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        return 'photo_detail', None, {'object_id': self.id}


class PhotoInline(admin.StackedInline):
    model = Photo


class ItemAdmin(admin.ModelAdmin):
    inlines = [PhotoInline]


admin.site.register(Item, ItemAdmin)
admin.site.register(Photo)

当我尝试在管理面板中创建对象项时,它会给我一个错误

POST
Request URL:    http://127.0.0.1/admin/item/item/add/
Django Version: 1.4.2
Exception Type: Warning
Exception Value:    
'Photo' object has no attribute 'name'
Exception Location: C:\wamp\python27\lib\site-packages\MySQLdb\cursors.py in   _warning_check, line 117
Python Executable:  C:\wamp\bin\apache\apache2.2.22\bin\httpd.exe
Python Version: 2.7.0
Python Path:    
['C:\\wamp\\python27\\lib\\site-packages\\setuptools-1.1.6-py2.7.egg',
 'C:\\wamp\\python27\\lib\\site-packages\\jaraco.develop-2.3-py2.7.egg',
 'C:\\wamp\\python27\\lib\\site-packages\\jaraco.windows-2.15-py2.7.egg',
 'C:\\wamp\\python27\\lib\\site-packages\\path.py-4.3-py2.7.egg',
 'C:\\wamp\\python27\\lib\\site-packages\\keyring-3.1-py2.7.egg',
 'C:\\wamp\\python27\\lib\\site-packages\\jaraco.util-8.5-py2.7.egg',
 'C:\\wamp\\python27\\lib\\site-packages\\six-1.4.1-py2.7.egg',
 'C:\\wamp\\python27\\lib\\site-packages\\ipython-1.1.0-py2.7.egg',
 'C:\\wamp\\python27\\lib\\site-packages\\pyreadline-2.0-py2.7-win-amd64.egg',
 'C:\\WINDOWS\\SYSTEM32\\python27.zip',
 'C:\\wamp\\python27\\Lib',
 'C:\\wamp\\python27\\DLLs',
 'C:\\wamp\\python27\\Lib\\lib-tk',
 'C:\\wamp\\bin\\apache\\apache2.2.22',
 'C:\\wamp\\bin\\apache\\apache2.2.22\\bin',
 'C:\\wamp\\python27',
 'C:\\wamp\\python27\\lib\\site-packages',
 'c:/DjangoProjects/photogallery/']
Server time:    Thu, 24 Oct 2013 23:29:33 +0400

我应该怎么处理这个错误?

1 个答案:

答案 0 :(得分:0)

我刚刚在我的一个项目中尝试了你的代码片段,它就像一个魅力,我没有看到任何问题,你确定,你仍然得到这个错误。如果是这样,请检查您的数据库,看看您在设置文件中为媒体设置了python路径。