【问题标题】:Filter on variable of ManyToMany-field inside Django Template在 Django 模板中过滤 ManyToMany-field 的变量
【发布时间】:2018-01-12 16:14:35
【问题描述】:

我有一个工作对象和一个内容对象。

当用户想要检索一组内容对象以换取积分时,就会创建一个作业对象。

models.py:

class JobId(models.Model):
    user = models.ForeignKey(User, blank=True, null=True, default=None)
    job_time = models.DateTimeField(auto_now_add=True)

class content(models.Model):
    job_id_list = models.ManyToManyField(JobId , related_name='JobId', blank=True)
    job_id_list_not_provided = models.ManyToManyField(JobId , related_name='JobIdFailed', blank=True)
    free_content = models.CharField(max_length=500, null=True, blank=True)
    paying_content = models.CharField(max_length=500, null=True, blank=True)

对于作业的所有内容对象部分,将 JobId 对象添加到 job_id_list - 不考虑信用等级。不同的用户都可以在内容对象上运行多个作业。

对于超过用户信用额度的太大作业,会将信用级别推低至零以下的内容对象也会将 JobID 对象添加到内容对象的 job_id_list_not_provided 字段中。

对于特定用户,我们可以通过以下查询检索已找到和未找到的内容对象的两个子集:

views.py:

found_results_list     = results_list.filter(job_id_list_not_provided__user= None).distinct()
not_found_results_list = results_list.filter(job_id_list_not_provided__user=request.user).distinct()

我的挑战:

结果列表的大小超过 100 个对象,所以我想使用分页来在我的页面上获得漂亮的视图

如果不考虑分页,我可以简单地传递 2 个列表(找到和未找到)并使用来自 django 的模板循环遍历每个列表:

HTML:

<table>
    <body>
        {% for result in found_results_list %}
            <tr>
                <td>{{result.free_content}}</td>
                <td>{{result.paying_content}}</td>
            </tr>
        {% empty %}
            <tr>no result</tr>
        {% endfor %}
        {% for result in not_found_results_list %}
            <tr>
                <td>{{result.free_content}}</td>
                <td>pay for the content</td>
            </tr>
        {% empty %}
            <tr>no result</tr>
        {% endfor %}
    </tbody>
</table>

但是如果我想使用分页怎么办?看来您只能使用一个结果列表。

views.py (我使用了.distinct(),因为有时对象从同一个用户那里添加了太多的工作)

results_list =  xlinkdatabase_validated.objects.filter(job_id_list__user=request.user).distinct()

主要问题是:

我不知道要检查模板内部是否可以看到 pay_content 如果从整体 result_list 开始,包括 found 和 not_found 对象。

我尝试在 Html 中使用{{result.job_id_list_not_provided}} 模板,但这会返回内容对象的所有作业对象,即使这些与特定用户无关,这当然是逻辑。

我将如何解决这个问题?

谢谢

【问题讨论】:

  • 我想到了两个解决方案,第一个是使用子查询来获取过滤结果,第二个是使用链来组合两个查询。

标签: python django pagination


【解决方案1】:

我最终通过构建自定义模板标签解决了我自己的问题,我在视图中的内容对象上释放了该标签。

额外的标签.py:

from django import template
from frontendapp.models import *

register = template.Library()

@register.filter
def has_viewing_rights(result, user):
    has_viewing_rights = True
    job_id_list_not_providedd = result.job_id_list_not_provided.all()
    for item in job_id_list_not_providedd:
        if item.user == user:
            has_viewing_rights = False
    return has_viewing_rights

html:

{% if result|has_viewing_rights:request.user%}
    provided
{%else%}
    not provided
{%endif%}

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 2011-12-14
    • 2019-09-28
    • 2020-04-09
    • 2012-04-05
    • 1970-01-01
    • 2016-06-16
    • 2015-04-09
    • 1970-01-01
    相关资源
    最近更新 更多