2017-03-15 136 views
1

我是新來的Django和我目前使用例如Django的通過我得到這個錯誤FieldError:無法解析「publish_year」到現場FieldError:無法解析「publish_year」到現場

這裏是如何我的模型是:

# Abstract Model 

class Post(models.Model): 
    STATUS_CHOICES = (('draft', 'Draft'),('published', 'Published'),)  
    title = models.CharField(max_length=250) 
    slug = models.SlugField(max_length=250, unique_for_date='publish') 
    author = models.ForeignKey(User, related_name='blog_posts') 
    body = models.TextField() 
    publish = models.DateTimeField(default=timezone.now) 
    created = models.DateTimeField(auto_now_add=True) 
    updated = models.DateTimeField(auto_now=True) 
    status = models.CharField(max_length=10, 
           choices= STATUS_CHOICES, 
           default='draft') 


    class PublishedManager(models.Manager): 
     def get_queryset(self): 
      return super(PublishedManager, self).get_queryset()\ 
      .filter(status='published') 

    objects = models.Manager() 
    published = PublishedManager() 

    class Meta: 
     ordering = ('-publish',) 

    def get_absolute_url(self): 
     return reverse('post_detail', 
          args=[self.publish.year, 
           self.publish.strftime('%m'), 
           self.publish.strftime('%d'), 
           self.slug]) 

    def __str__(self): 
     return self.title 

這是我的看法我試圖編輯,但我不認爲這個問題是我的觀點

def post_list(request): 
    posts = Post.objects.all() 
    return render(request, 'list.html', 
          {'posts':posts}) 

def post_detail(request, year, month, day, post,): 
    post = get_object_or_404(Post, slug=post, 
            status='published', 
            publish_year=year, 
            publish_month=month, 
            publish_day=day) 
    return render(request, 'detail.html', 
          {'post':post}) 
    enter code here 
    enter code here 

當我試圖訪問我的帖子的細節,我有以下電子RROR消息

FieldError at /2017/03/15/help/ 
Cannot resolve keyword 'publish_year' into field. Choices are: author, author_id, body, created, id, publish, slug, status, title, updated 
    Request Method: GET 
    Request URL: http://127.0.0.1:8000/2017/03/15/help/ 
    Django Version: 1.10.6 
    Exception Type: FieldError 
    Exception Value:  
    Cannot resolve keyword 'publish_year' into field. Choices are: author, author_id, body, created, id, publish, slug, status, title, updated 
    Exception Location: C:\Users\Harsley\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\query.py in names_to_path, line 1327 
    Python Executable: C:\Users\Harsley\AppData\Local\Programs\Python\Python36-32\python.exe 
    Python Version: 3.6.0 
    Python Path:  
    ['C:\\Users\\Harsley\\blog', 
    'C:\\Users\\Harsley\\AppData\\Local\\Programs\\Python\\Python36-32\\python36.zip', 
    'C:\\Users\\Harsley\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs', 
    'C:\\Users\\Harsley\\AppData\\Local\\Programs\\Python\\Python36-32\\lib', 
    'C:\\Users\\Harsley\\AppData\\Local\\Programs\\Python\\Python36-32', 
    'C:\\Users\\Harsley\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages'] 
    Server time: Wed, 15 Mar 2017 15:31:16 +0000 
+0

FieldError在/ 2017年/ 03/15 /幫助/ 無法解析關鍵字 'publish_year' 到現場。選擇是:author,author_id,body,created,id,publish,slug,status,title,updated –

+2

@EkhorutomwenHarsley在問題中添加上面的追蹤,並提供你的視圖的代碼 – dnit13

+1

我的猜測是缺少第二個下劃線('publish_year' vs'publish__year'),但您提供的信息太少,無法幫助您。 –

回答

3

您應該使用強調for lookups that span relations,例如publish__year

post = get_object_or_404(
    Post, 
    slug=post, 
    status='published', 
    publish__year=year, 
    publish__month=month, 
    publish__day=day, 
) 
+0

感謝它的工作,當我介紹了雙下劃線 –

相關問題