组合,过滤ManyToMany Django中的模型/视图

时间:2011-07-11 03:21:37

标签: django django-models django-views

我正在制作漫画书数据库,主要封面和变体封面。我有一个显示所有主要封面的页面,但我也想按照出版日期的顺序组合变体封面。这是我的模型的一部分:

class Image(models.Model):
    CATEGORY_CHOICES = (
    ('Cover', 'Cover'),    
    ('Scan', 'Scan'),
    ('Other', 'Other'),
    )
    title = models.CharField(max_length=128)
    number = models.CharField(max_length=20, help_text="Do not include the '#'.")
    image = models.ImageField(upload_to="images/")
    category = models.CharField(max_length=10, choices=CATEGORY_CHOICES)
    ### The variant cover is determined by the category_choice 'Cover'. ###
    contributor = models.ManyToManyField(Contributor, blank=True, null=True)
    date_added = models.DateField(auto_now_add=True, auto_now=True)    
    def __unicode__(self):
        return self.title        
    class Meta:
        ordering = ['title']

class Issue(models.Model):
    CATEGORY_CHOICES = (
    ('Major', 'Major'),    
    ('Minor', 'Minor'),
    ('Cameo', 'Cameo'),
    ('Other', 'Other'),
    )
    title = models.ForeignKey(Title)
    number = models.CharField(max_length=20, help_text="Do not include the '#'.")
    pub_date = models.DateField(blank=True, null=True)
    cover_image = models.ImageField(upload_to="covers/", blank=True, null=True)
    ### This would be where the main image goes. ^^^ ###
    images = models.ManyToManyField(Image, related_name="images_inc", blank=True, null=True)
    ### This is where the variant covers go.^^^  ### 
    has_emma = models.BooleanField(help_text="Check if Emma appears on the cover.")

主要封面的my views.py如下所示:

def covers(request):
    sort_by = request.GET.get('sort', 'pub_date')
    if sort_by not in ['-date_added', 'date_added', '-pub_date', 'pub_date']:
        sort_by = '-date_added'
    issues = Issue.objects.filter(has_emma=True).order_by(sort_by).select_related(depth=1)
    return render_to_response('comics/covers.html', {'issues': issues}, context_instance=RequestContext(request))

但我想展示变体封面,而不仅仅是cover_image。有没有办法做到这一点?也许使用某些image然后过滤类别(Image模型的封面)?

我当然可以这样做:

def variants(request):
    Issue.objects.filter(has_emma=True).order_by(sort_by).select_related(depth=1)
    images = Image.objects.filter(category='Cover').order_by('id')
    return render_to_response('comics/variants.html', {'images': images}, context_instance=RequestContext(request))

但这并没有像def covers那样给我足够的灵活性,我希望它们合并并按pub_date排序,如def covers

修改

models.py:

class Image(models.Model):
    CATEGORY_CHOICES = (
    ('Cover', 'Cover'),    
    ('Scan', 'Scan'),
    ('Other', 'Other'),
    )
    title = models.CharField(max_length=128)
    image = models.ImageField(upload_to="images/")
    category = models.CharField(max_length=10, choices=CATEGORY_CHOICES)
    date_added = models.DateField(auto_now_add=True, auto_now=True)    
    def __unicode__(self):
        return self.title        
    class Meta:
        ordering = ['title']


class Issue(models.Model):
    title = models.ForeignKey(Title)
    number = models.CharField(max_length=20)
    ######
    has_emma = models.BooleanField(help_text="Check if cover appearance.")    
    cover_image = models.ImageField(upload_to="covers/", blank=True, null=True)
    images = models.ManyToManyField(Image, related_name="images_inc", blank=True, null=True)
    ######
    def get_images(self):
        ''' Returns a list of all cover images combined,
            "main" cover image first.
        '''
        images = [self.cover_image]
        for image in self.images.filter(category='Cover'):
            images.append(image.image)
        return images   

views.py:

def covers(request):
    sort_by = request.GET.get('sort', '-pub_date')
    if sort_by not in ['-date_added', 'date_added', '-pub_date', 'pub_date']:
        sort_by = '-date_added'         
    issues = Issue.objects.filter(has_emma=True).order_by(sort_by)
    return render_to_response('template.html', {'issues': issues,}, context_instance=RequestContext(request))

template.html: {% for issue in issues %}{% for image in issue.get_images %}{{ image.image }}{% endfor %}{% endfor %} - 不显示任何内容,但{% for issue in issues %} {% for image in issue.get_images %} {{ issue.cover_image }} {% endfor %} {% endfor %}重复显示cover_image模型的Issue,如果存在变体封面,则归类于Image模型。

我该怎么做才能解决这个问题,以便正确显示所有内容?再次记录,我希望它显示{{ cover_image }}(来自Issue模型)和{{ image.image }}模型所定义的Image

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,解决问题的一种方法是向Issue类添加一个方法,如下所示:

class Issue(models.Model):
    # fields...

    def get_images(self):
        ''' Returns a list of all cover images combined,
            "main" cover image first.
        '''
        images = [self.cover_image]
        for image in self.images.filter(category='Cover'):
            images.append(image.image)
        return images

然后,在您的模板中,您可以执行{% for image in issue.get_images %}...

(如果它不完全符合您的需求 - 那么,我认为,如果您提供一些模板代码作为您要实现的目标的示例,那会更好。)