Django查询最喜欢的

时间:2013-03-22 05:51:05

标签: django

我一直想弄清楚如何查询特定类别最喜欢的白板

目前我一直在查询特定类别的所有白板对象。 我的解决方案之一是通过董事会查询喜欢的计数,但我想不出一种方法来查询它

目前我可以检索特定类别的所有白板对象的示例,现在我如何检索特定类别中最喜欢的白板。

有人可以帮我创建一个功能,可以查询该类别中最受欢迎的主板,这样我就可以理解我以后如何创建自己的主板。谢谢你

class WhiteBoard(models.Model):
    ENGLISH = 'ENGLISH'
    MATH = 'MATH'
    SCIENCE = 'SCIENCE'
    BIOLOGY = 'BIOLOGY'
    CATEGORY = (
        (ENGLISH , 'English'),
        (MATH, 'Math'),
        (SCIENCE, 'Science'),
        (BIOLOGY, 'Biology'),
    )
    Category =models.CharField(max_length=30,choices=CATEGORY)
    user = models.ForeignKey(User)
    name = models.CharField(max_length=100)
    picture = models.OneToOneField('Picture',related_name='picture',blank=True,null=True)
    def __unicode__(self):
        return self.name

class LikeBoard(models.Model):
    user = models.ForeignKey(User)
    Whiteboard = models.ForeignKey(WhiteBoard)
    created = models.DateTimeField(auto_now_add=True)  

我的views.py

def WhiteBoardFinder(request):
    form = WhiteBoardFinderForm(request.POST)
    fo = WhiteBoardFinderForm()
    if form.is_valid():
        Category = form.cleaned_data['Category']
        Whiteboard = WhiteBoard.objects.filter(Category=Category)
        return render(request,"boardfinder.html",{"board":board,"fo":fo})
    return render(request,"boardfinder.html",{"fo":fo})

boardfinder.html

<form method="POST" enctype="multipart/form-data">
    {% csrf_token %}
    {{ fo.as_p }}
    <input type = "submit" value= "Find Board" />
</form>



{% if board %} 
<ul>  
    {% for b in board %}         
    <li><a href ="{% url world:Boat b.id %}">{{ b.name }}</li>
    {% if b.picture %}
    <br><img src="{{ b.picture.image.url }}">
    {% endif %}
    {% endfor %}
</ul>
{% endif %}

我的forms.py

class BoardFinderForm(forms.ModelForm):
    class Meta:
        model = WhiteBoard
        fields = ('Category',)

1 个答案:

答案 0 :(得分:0)

<强> models.py

class WhiteBoard(models.Model):
    ENGLISH = 'ENGLISH'
    MATH = 'MATH'
    SCIENCE = 'SCIENCE'
    BIOLOGY = 'BIOLOGY'
    CATEGORY = (
        (ENGLISH , 'English'),
        (MATH, 'Math'),
        (SCIENCE, 'Science'),
        (BIOLOGY, 'Biology'),
    )
    Category =models.CharField(max_length=30,choices=CATEGORY)
    user = models.ForeignKey(User)
    name = models.CharField(max_length=100)
    picture = models.OneToOneField('Picture',related_name='picture',blank=True,null=True)
    def __unicode__(self):
        return self.name

    @property    
    def count_likes(self):
        return LikeBoard.objects.filter(whiteboard=self).count()

class LikeBoard(models.Model):
    user = models.ForeignKey(User)
    whiteboard = models.ForeignKey(WhiteBoard) //modified because of conflict
    created = models.DateTimeField(auto_now_add=True)

<强> views.py

def WhiteBoardFinder(request):
    form = WhiteBoardFinderForm(request.POST)
    fo = WhiteBoardFinderForm()
    if form.is_valid():
        Category = form.cleaned_data['Category']
        whiteboard = WhiteBoard.objects.filter(Category=Category)

        categories = WhiteBoard.objects.values_list('Category', flat=True).distinct()
        whites = sorted(whiteboard, key=lambda x: x.count_likes, reverse=True)

        return render(request,"boardfinder.html",{
            "board":board,"fo":fo, "categories": categories, "whites": whites})
    return render(request,"boardfinder.html",{"fo":fo})

<强>模板

{% for category in categories %}
    {{ category }}<br/>
    {% for white in whites %}
        {% if white.Category == category %}
        {{ white }} - {{ white.count_likes }},
        {% endif %}
    {% endfor %}<br/>
{% endfor %}