2017-06-03 9 views
0

簡単な質問 - これを処理する正しい方法は何か分かりません。基本的に私はいくつかのロジックを扱うカスタムタグを定義し、is_featuredフィールドがTrueに設定されたモデルPostのすべての投稿を返したいと考えています。私はこれを得るためにいくつかの方法を試しましたが、誰も働いていません。私の最後のcoherantは、以下の通りであったが、 "推測":Postモデルの投稿をフィルタリングするDjangoのタグをカスタマイズする

templatetags/blog_tags.py:私は私がする必要があると思うすべてです

class PublishedManager(models.Manager): 

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

class Post(models.Model): 

    STATUS_CHOICES = (
     ('draft', 'Draft'), 
     ('published', 'Published'), 
    ) 

    POST_TYPES = (
     ('news', 'News'), 
     ('feature', 'Feature'), 
     ('review', 'Review'), 
    ) 

    title = models.CharField(max_length=250) 
    slug = models.SlugField(max_length=250, unique_for_date='publish') 

    author = models.ForeignKey(UserProfile, related_name='blog_posts') 

    body = models.TextField() 
    lead_in = models.CharField(max_length=500, default='') 

    #These next items shall contain our development information for game reviews - this is much like the lead_in: 
    platform = models.CharField(max_length=1000, default='') 
    publisher = models.CharField(max_length=1000, default='') 
    developer = models.CharField(max_length=1000, default='') 
    release = models.DateTimeField(default=timezone.now) 
    is_featured = models.BooleanField(default=False) 

    #Out blog layout dictates each featurette has up to three scrolling images associated to it: 
    image_scroll_1 = models.ImageField(storage=site_media_upload_location, null=True, blank=True) 
    image_scroll_2 = models.ImageField(storage=site_media_upload_location, null=True, blank=True) 
    image_scroll_3 = models.ImageField(storage=site_media_upload_location, null=True, blank=True) 

    type = models.CharField(max_length=10,choices=POST_TYPES,default='review') 

    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') 
    rating = models.PositiveSmallIntegerField(default=10) 

    wrap_header = models.CharField(max_length=250, default='') 
    wrap_up = models.TextField(default='') 
    disclaimer = models.TextField(default='') 

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

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

    def __str__(self): 
     """Return the state title of the post""" 
     return self.title 

    def get_absolute_url(self): 
     """Get absolute_url path specific to this post.""" 
     return reverse('blog:post_detail', args = [self.publish.year, self.publish.strftime('%m'), self.publish.strftime('%d'), self.slug]) 

    def get_image(self): 
     """Get upload_to path specific to this photo.""" 
     return self.image.url 

@register.inclusion_tag('blog/post/featured_posts.html') 
def show_featured_posts(count=4): 
    """Return 4 of the most recent posts (of model: 'Post') that has the variable is_featured set to True.""" 
    if Post.is_featured: 
     featured_posts = Post.published.order_by('-publish')[:count] 

return { 'featured_posts': featured_posts} 

models.py(便利なビット) include - 私はfeatured_posts.htmlテンプレートを用意していますので、問題はそれとは言えません。それは純粋にblog_tags.pyファイル内です。

+0

にblog_tags.py

if Post.is_featured: featured_posts = Post.published.order_by('-publish')[:count] 

に次のコードを置き換えるには、そのテンプレートを示すことができました? – zaidfazil

+0

@FazilZaidこんにちはファジル - 心配する必要はなく、ちょうど以下の答えを実装し、すべてが今働いています。それにかかわらず、あなたの考えに感謝します。 –

答えて

1

featured_posts = Post.published.filter(is_featured=True).order_by('-publish')[:count] 
+0

完璧に作業しました。私はビューの前にPostクラスをフィルターに掛けました - だから私は本当にこの質問をしてはいけませんでした!しかし、多くのお手伝いをいただきありがとうございます。 –

関連する問題