我是django的新手。我正在按照tangowithdjango.com上给出的教程。我不知道为什么会出现错误......... 错误是
Exception Type: FieldError
Exception Value: Cannot resolve keyword 'likes' into field. Choices are: id, name, page
Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py in names_to_path, line 1397
Python Executable: /usr/bin/python
Python Version: 2.7.9
Python Path:
['/root/workspace/tango_with_django',
'/usr/local/lib/python2.7/dist-packages/virtualenv-13.1.2-py2.7.egg',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/wx-3.0-gtk2']
我的populate_rango.py文件是
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tango_with_django.settings')
import django
django.setup()
from rango.models import Category, Page
def populate():
python_cat = add_cat('Python')
add_page(cat=python_cat,
title="Official Python Tutorial",
url="http://docs.python.org/2/tutorial/"
views=128,
likes=64
)
add_page(cat=python_cat,
title="How to Think like a Computer Scientist",
url="http://www.greenteapress.com/thinkpython/",
views=128,
likes=64)
add_page(cat=python_cat,
title="Learn Python in 10 Minutes",
url="http://www.korokithakis.net/tutorials/python/",
views=128,
likes=64)
django_cat = add_cat("Django")
add_page(cat=django_cat,
title="Official Django Tutorial",
url="https://docs.djangoproject.com/en/1.5/intro/tutorial01/",
views=64,
likes=32)
add_page(cat=django_cat,
title="Django Rocks",
url="http://www.djangorocks.com/",
views=64,
likes=32)
add_page(cat=django_cat,
title="How to Tango with Django",
url="http://www.tangowithdjango.com/",
views=64,
likes=32)
frame_cat = add_cat("Other Frameworks")
add_page(cat=frame_cat,
title="Bottle",
url="http://bottlepy.org/docs/dev/",
views=32,
likes=16)
add_page(cat=frame_cat,
title="Flask",
url="http://flask.pocoo.org",
views=32,
likes=16)
for c in Category.objects.all():
for p in Page.objects.filter(category=c):
print "- {0} - {1}".format(str(c), str(p))
def add_page(cat, title, url, views=0,likes=0):
p = Page.objects.get_or_create(category=cat, title=title)[0]
p.url=url
p.views=views
p.likes=likes
p.save()
return p
def add_cat(name,views=0,likes=0):
c = Category.objects.get_or_create(name=name)[0]
c.views=views
c.likes=likes
c.save()
return c
if __name__ == '__main__':
print "Starting Rango population script..."
populate()
和admin.py文件是
from django.contrib import admin
from rango.models import Category,Page
from rango.models import views,likes
# Register your models here.
class PageAdmin(admin.ModelAdmin):
list_display=('title','category','url')
admin.site.register(Category)
admin.site.register(views)
admin.site.register(likes)
admin.site.register(Page,PageAdmin)
models.py是
from django.db import models
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
def __unicode__(self): #For Python 2, use __str__ on Python 3
return self.name
class Page(models.Model):
category = models.ForeignKey(Category)
title = models.CharField(max_length=128)
url = models.URLField()
def __unicode__(self):
return self.title
class views(models.Model):
number=models.IntegerField()
def __unicode__(self):
return self.number
class likes(models.Model):
num=models.IntegerField()
def __unicode__(self):
return self.num
请帮我解决这个错误。谢谢您花费一些时间......
答案 0 :(得分:0)
likes/views
或Page
模型中没有Category
字段。
您需要首先创建从Page
和Category
到Likes
和Views
模型的关系,然后您可以保存特定页面或类别的视图和喜欢。
我建议你添加likes
& views
& Page
中的Category
字段Like
模型本身。
而不是单独的Views
和class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
views = models.IntegerField(default=0)
likes = models.IntegerField(default=0)
def __unicode__(self): #For Python 2, use __str__ on Python 3
return self.name
class Page(models.Model):
category = models.ForeignKey(Category)
title = models.CharField(max_length=128)
url = models.URLField()
views = models.IntegerField(default=0)
likes = models.IntegerField(default=0)
模型:
"PHOTO":"\/9j\/4AAQSkZJRgABAQAAAQABAAD"
将新字段添加到Page / Category表后,执行makemigrations然后迁移。
如果您使用DROP Like / views表,只需确保在视图中删除对它们的所有引用,admin.py等。