0

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

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

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

有人可以帮我创建可以查询该类别中最喜欢的板的功能,这样我就可以从逻辑上理解我以后如何创建自己的板。谢谢

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)  

我的观点.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 %}

我的表格.py

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

1 回答 1

0

模型.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)

视图.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 %}
于 2013-03-22T08:30:01.507 回答