Djangompt是一种根据u中的当前get参数自动动态更改层次结构的方法

2024-09-29 09:23:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用djangomptt创建类似于the sidemenu in Gumtree的过滤系统。你知道吗

我有这样的等级制度:

g1
  --g11
    --g111
    --g112
  --g12
    --g121
    --g122
g2
  --g21
    --g211
    --g212
  --g22
    --g221
    --g222

当你来到我的网站,你可以看到

g1
g2

它只显示没有父级的顶级类别。我可以这样写:

nodes = Genre.objects.filter(level=0)

然后,例如,单击g1的链接时,层次结构会发生如下变化:

g1
  --g11
  --g12

我可以这样写:

nodes = Genre.objects.filter(tree_id=1).filter(level__lte=1)

然后,如果你点击g11,它显示如下:

g1
  --g11
    --g111
    --g112

但我不知道如何写下代码来表达它。 如果我这样写:

nodes = Genre.objects.filter(tree_id=1)

它显示:

g1
  --g11
    --g111
    --g112
  --g12
    --g121
    --g122

但我不想展示g12,它的孩子:g121和g122。你知道吗

你能教我吗?你知道吗

我想知道如何根据url中当前的get参数自动动态地更改层次结构。 例如,当我们在url中得到参数“g1”时,当前的格式是“?流派=g1“,我们将看到下面我上面说的:

g1
--g11
--g12

如果我们有“?类型=g12“

g1
  --g12
    --g121
    --g122

像这样。你知道吗

其他代码如下:

{% load mptt_tags %}
<ul>
    {% recursetree nodes %}
        <li>
            <a href="?{% url_replace request 'genre' node.pk %}">{{ node.name }}</a>
            {% if not node.is_leaf_node %}
                <ul class="children">
                    <a href="?{% url_replace request 'genre' children.pk %}">{{ children }}</a>
                </ul>
            {% endif %}
        </li>
    {% endrecursetree %}
</ul>

class Genre(MPTTModel):
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name

    class MPTTMeta:
        order_insertion_by = ['name']

我以前做过这样的过滤系统:

class Location(models.Model):
    parent = models.ForeignKey('Location', related_name='children', null=True, blank=True)
    name = models.CharField(max_length=255)

    @property
    def dispatch(self):
        if not self.parent:
            return self
        else:
            return self.parent

    def __str__(self):
        return self.name

location_pk = request.GET.get('location')

if location_pk:
    location = get_object_or_404(Location, pk=location_pk)
    if not location.parent and location.children.exists():
        c.update({
            'state': location.dispatch,
            'regions': location.children.all(),
        })
    elif location.parent and location.children.exists():
        c.update({
            'state': location.dispatch,
            'region': location,
        })
    elif location.parent and not location.children.exists():
        c.update({
            'state': location.dispatch.parent,
            'region': location.parent,
        })
else:
    c.update({'states': Location.objects.filter(parent__isnull=True)})

<p>[ Location ]</p>
<ul>
    {% if request.GET.location %}
       <li><a href="{% url_remove request 'location' %}">All Locations</a></li>
    {% endif %}
    {% if state %}
        <li><a href="?{% url_replace request 'location' state.pk %}">{{ state.name }} | {{ state.pk }}</a></li>
        {% if region %}
            <li>---<a href="?{% url_replace request 'location' region.pk %}">{{ region.name }} | {{ region.pk }}</a></li>
            {% for city in region.children.all %}
            <li>------<a href="?{% url_replace request 'location' city.pk %}">{{ city.name }} | {{ city.pk }}</a></li>
            {% endfor %}
        {% else %}
            {% for region in regions %}
                <li>---<a href="?{% url_replace request 'location' region.pk %}">{{ region.name }} | {{ region.pk }}</a></li>
            {% endfor %}
        {% endif %}
    {% else %}
        {% for state in states %}
            <li><a href="?{% url_replace request 'location' state.pk %}">{{ state.name }} | {{ state.pk }}</a></li>
        {% endfor %}
    {% endif %}

但它很长很复杂。这就是为什么我想让这个更简短,更容易使用django mptt。你知道吗


Tags: nameselfurlrequestlocationliregionreplace