【问题标题】:Multiple Django Model Queries Changing Independently in One View多个 Django 模型查询在一个视图中独立更改
【发布时间】:2013-04-28 11:08:12
【问题描述】:

我有三个模型分别被调用——一个在我的第一列,第二个在我的第二列,第三个在我的第三列。第一个是类别,将保持不变。第二个是帖子,如果选择了一个类别,则需要显示该类别的帖子(如果不是则显示全部),第三个是选定的帖子(如果没有,则不显示)。

我在使用 get_absolute_url 调用时有点工作,但我使用了太多(四个)不同的视图,而且很混乱。此外,这种方式并不总是能正常工作。如果我选择了我所拥有的帖子,帖子列表会更改为该类别,例如,当您仍然可以显示所有帖子时。

我怎样才能使它正常工作?我知道 ajax 在这方面也很不错,但我想先让它在没有 ajax 的情况下工作,然后再实现它,还是只有 ajax 才有可能?

这是我的相关代码:

型号:

class Category(models.Model):
    name = models.CharField(max_length=30, unique=True)

    def get_absolute_url(self):
        return "/category/%i/" % self.id

class Post(models.Model):
    category = models.ForeignKey(Category)
    title = models.CharField(max_length=256)
    author = models.CharField(max_length=256)
    link = models.URLField(max_length=512)
    dt_published = models.DateTimeField()
    content = models.TextField()

    def get_absolute_url(self):
        return "/%i/%i/" % (self.category.id, self.id)

网址:

urlpatterns += patterns('myapp.views',
    url(r'^main/$', 'mainview'),
    url(r'^post/(\d+)/$', 'mainview2'),
    url(r'^category/(\d+)/$', 'mainview3'),
    url(r'^(\d+)/(\d+)/$', 'mainview4'),
)

观看次数:

def mainview(request):
    category_list = Category.objects.all()
    post_list = Post.objects.all()
    entry = None
    return render(request, 'main.html', {'category_list': category_list, 'post_list': post_list, 'entry': entry})

def mainview2(request, postid):
    category_list = Category.objects.all()
    post_list = Post.objects.all()
    entry = Post.objects.filter(id=postid)
    return render(request, 'mainview.html', {'category_list': category_list, 'post_list': post_list, 'entry': entry})

def mainview3(request, catid):
    category_list = Category.objects.all()
    post_list = Post.objects.filter(category=catid)
    entry = None
    return render(request, 'mainview.html', {'category_list': category_list, 'post_list': post_list, 'entry': entry})

def mainview4(request, catid, postid):
    category_list = Category.objects.all()
    cat_selected = Category.objects.filter(id=catid)
    post_list = Post.objects.filter(category=cat_selected)
    entry = Post.objects.filter(id=postid)
    return render(request, 'main view.html', {'category_list': category_list, 'post_list': post_list, 'entry': entry})

【问题讨论】:

    标签: python ajax django django-models django-views


    【解决方案1】:

    你可以只用一个视图来解决这个问题。

    此外,您应该在模型中使用 @permalink decorator get_absolute_url() 方法。这样,如果您应该更改您的网址,您就不会冒链接断开的风险。

    models.py

    class Category(models.Model):
        name = models.CharField(max_length=30, unique=True)
    
        @models.permalink
        def get_absolute_url(self):
            return ('browser', (), {'category_id': self.id})
    
    class Post(models.Model):
        category = models.ForeignKey(Category)
        title = models.CharField(max_length=256)
        author = models.CharField(max_length=256)
        link = models.URLField(max_length=512)
        dt_published = models.DateTimeField()
        content = models.TextField()
    
        @models.permalink
        def get_absolute_url(self):
            return ('browser', (), {'category_id': self.category.id, 'post_id': self.id})
    

    urls.py

    urlpatterns = patterns('whatever.views',
        url(r'^browser/$', 'theview', name="browser"),
        url(r'^browser/(?P<category_id>[0-9]+)/$', 'theview', name="browser"),
        url(r'^browser/(?P<category_id>[0-9]+)/(?P<post_id>[0-9]+)/$', 'theview', name="browser"),
    )
    

    views.py

    from django.shortcuts import get_object_or_404, render
    
    def theview(request, category_id=None, post_id=None):
        categories = Category.objects.all()
        posts = None
        selected_cat = None
        selected_post = None
        if category_id:
            selected_cat = get_object_or_404(Category, pk=category_id)
            posts = selected_cat.post_set.all()
            if post_id:
                selected_post = posts.filter(pk=post_id)
    
        return render(
            request,
            'template.html',
            {
                'categories': categories,
                'selected_cat': selected_cat,
                'posts': posts,
                'selected_post': selected_post
            }
        )
    

    【讨论】:

    • 这仍然给我一个错误:“AttributeError at /browser/ 'NoneType' object has no attribute 'post_set'” 有什么想法吗?
    • 所以我通过放置“posts = Post.objects.filter(category=selected_cat)”而不是“posts = selected_cat.post_set.all()”来让它工作,但我还有一个问题.当没有选择任何类别或帖子时,它不会显示任何帖子,而我希望它显示全部。我将“posts = None”更改为“posts = Post.objects.all()”,但它没有用。有什么想法吗?
    • AttributeError 不可能真的发生在我编写的代码中,因为 get_object_or_404 保证返回实例或引发 404。在调整我的示例代码时是否可能引入错误?
    • 你说得对,当我复制和粘贴你的代码时发生了一些奇怪的事情。它现在正在工作。我还将“posts = Post.objects.all()”放在尚未选择类别的情况下,以便用户可以看到所有帖子,但是当我点击那里的链接时,它会将帖子列表更改为任何内容类别。如果用户只是从未分类的帖子列表中选择,有没有办法让它保持不变?谢谢。
    • 没有分类怎么会有帖子? Post 上的 categroy FK 不为空。
    猜你喜欢
    • 1970-01-01
    • 2016-03-14
    • 2013-06-07
    • 1970-01-01
    • 2017-11-23
    • 2022-01-10
    • 2020-04-09
    • 1970-01-01
    • 2021-09-24
    相关资源
    最近更新 更多