【问题标题】:Field error Cannot resolve keyword 'likes' into field. Choices are: id, name, page字段错误无法将关键字“喜欢”解析为字段。选项有:id、name、page
【发布时间】:2015-10-31 07:04:27
【问题描述】:

我是 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

请帮助我解决错误。感谢您花费一些时间...

【问题讨论】:

  • 你有完整的回溯吗?
  • 是的。你要我上传吗?
  • 完整回溯是什么意思?我是 django 新手。所以,我不知道它到底是什么。@IgnacioVazquez-Abrams
  • 你能展示你的模型吗?
  • 我已经上传了models.py @AlexLisovoy

标签: python linux django


【解决方案1】:

PageCategory 模型中没有 likes/views 字段。 您需要先创建从 PageCategoryLikesViews 模型的关系,然后您可以保存特定页面或类别的视图和喜欢。

我建议您在 PageCategory 模型本身中添加 likesviews 字段。

而不是单独的LikeViews 模型这样做:

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)

将新字段添加到 Page/Category 表后,执行 makemigrations 然后迁移。

如果您 DROP Like/views 表,只需确保在您的视图、admin.py 等中删除对它们的所有引用。

【讨论】:

  • 我不太明白。你能上传代码吗? @Anush
  • 为什么他需要在类别模型中点赞?我看不出有任何逻辑。它更适合页面模型
  • @doniyor 在视图中,他也试图保存类别的视图/喜欢,所以我添加了这些字段。
  • 现在,我在运行服务器时收到错误“ImportError: cannot import name views”。
  • @abhaygurrala 从您的 admin.py 中删除 from rango.models import views,likes
猜你喜欢
  • 2012-09-13
  • 2021-11-04
  • 1970-01-01
  • 2019-10-16
  • 2021-10-17
  • 1970-01-01
  • 2011-07-07
  • 2013-04-09
  • 2019-09-08
相关资源
最近更新 更多