开发者

Getting context of parent in Foreign Key - Django

This is supposed to be really simple and it is in the documents. I am trying to get the context of a ListView. For example:

"We can also add the publisher into the context at the same time, so we can use it in the template http://docs.djangoproject.com/en/1.3/topics/class-based-views/#dynamic-filtering/:

class IssuesByTitleView(ListView):
    context_object_name = "issue_list"
    def get_queryset(self):
        self.title = get_object_or_404(Title, slug=self.kwargs['title_slug'])
        return Issue.objects.filter(title=self.title).order_by('-number')

My models.py look something like this:

class Title(models.Model):
    CATEGORY_CHOICES = (
    ('Ongoing', 'Ongoing'),    
    ('Ongoing - Canceled', 'Ongoing - Canceled'),
    ('Limited Series', 'Limited Series'),
    ('One-shot', 'One-shot'),
    ('Other', 'Other'),
    )    
    title = models.CharField(max_length=64)
    vol = models.IntegerField(blank=True, null=True, max_length=3)
    year = models.CharField(blank=True, null=True, max_length=20, help_text="Ex) 1980 - present, 1980 - 1989.")
    category = models.CharField(max_length=30, choices=CATEGORY_CHOICES)    
    is_current = models.BooleanField(help_text="Check if the title is being published where Emma makes regular appearances.")
    slug = models.SlugField()
    class Meta:
        ordering = ['title']
    def get_absolute_url(self):
        return "/titles/%s" % self.slug        
    def __unicode__(self):
        return self.title

    class Issue(models.Model):
        C开发者_运维百科ATEGORY_CHOICES = (
        ('Major', 'Major'),    
        ('Minor', 'Minor'),
        ('Cameo', 'Cameo'),
        ('Other', 'Other'),
        )
        title = models.ForeignKey(Title)
        number = models.IntegerField(help_text="Do not include the '#'.")

        .........

Views.py:

class IssuesByTitleView(ListView):
    context_object_name = "issue_list"
    def get_queryset(self):
        self.title = get_object_or_404(Title, slug=self.kwargs['title_slug'])
        return Issue.objects.filter(title=self.title).order_by('-number')
    def get_context_data(self):
        context = super(IssuesByTitleView, self).get_context_data()
        context['title'] = self.title
        return context

In my list of issues within the Title, I need to return the Title and all the other properties of the Title. That up there does not work and it returns an error:

get_context_data() got an unexpected keyword argument 'object_list'


You should use **kwargs in get_context_data

def get_context_data(self, **kwargs):
    context = super(IssuesByTitleView, self).get_context_data(**kwargs)
    context['title'] = self.title
    return context
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜