0

Okay, so I have a 'Keyword' model that is storing my keywords for my 4 other models, each of which has a 'key_list' field which is a ManyToManyField pointing back to my 'Keyword' model. Each of my models have multiple keywords, and I am searching through them and finding them successfully like so:

keys_selected='term1$term2$term3$'
keys_selected = keys_selected.rstrip('$')
keys_selected = keys_selected.split('$')
goal = len(keys_selected)
remaining = list()
remaining += Keyword.objects.filter(employee__key_list__key__in=keys_selected).annotate(num_keys=Count('key')).filter(num_keys=goal).distinct()
remaining += Keyword.objects.filter(vendor__key_list__key__in=keys_selected).annotate(num_keys=Count('key')).filter(num_keys=goal).distinct()
remaining += Keyword.objects.filter(application__key_list__key__in=keys_selected).annotate(num_keys=Count('key')).filter(num_keys=goal).distinct()
remaining += Keyword.objects.filter(machine__key_list__key__in=keys_selected).annotate(num_keys=Count('key')).filter(num_keys=goal).distinct()
key_list = list()
for x in remaining:
    if x not in key_list:
        key_list.append(x)

This returns a dictionary of all of the keywords that are assigned to all of the entries in my 4 models that contain my chosen terms. The idea here is to create a filter that visually displays the word frequency of all keywords in the key_list of the objects that match my search query. I want it to append that to the dictionary being output to context so that I can then call the value and use it for the font size like so:

{% for key in key_list %}
<a href="{% url 'keysearch:index' %}?keys_selected={{ key }}${{ keys_selected }}" style="font-size: {{ key.num_keys }}px;">({{ key }})</a>
{% endfor %}

In other words this should create a keyword 'cloud' of my keywords that filters down and visually shows how frequent a given keyword is among my model results, but I am totally lost as to how to get that to function ONLY within the results of my filter. I am at a loss as to how I would accomplish this across multiple models.

My models for reference:

class Keyword(models.Model):
    key = models.CharField(max_length=2000, unique=True)

    def __str__(self):
        return self.key

    class Meta:
        ordering = ('key',)


class Entries(models.Model):
    name = models.CharField("Name", max_length=200)
    updated = models.DateTimeField("Last Updated", auto_now=True)
    key_list = models.ManyToManyField(Keyword, blank=True, verbose_name="Keywords")
    description = models.TextField("Description", blank=True)

    class Meta:
        abstract = True
        ordering = ('name',)


class Employee(Entries):
    uid = models.SlugField("Employee User ID", max_length=6, unique=True, blank=True)
    manager = models.SlugField("Manager's User ID", max_length=6)

    def __str__(self):
        return self.name


class Vendor(Entries):
    company = models.CharField("Vendor Company", max_length=200)
    email = models.EmailField("Vendor Company Email Address", max_length=254, unique=True)
    vend_man_name = models.CharField("Manager's Name", max_length=200)
    vend_man_email = models.EmailField("Manager's Email Address", max_length=254)

    def __str__(self):
        return self.name


class Application(Entries):
    app_url = models.URLField("Application URL", max_length=800, unique=True)

    def __str__(self):
        return self.name


class Machine(Entries):
    address = models.CharField("Machine Address", max_length=800, unique=True)
    phys_loc = models.TextField("Physical Location", blank=True)

    def __str__(self):
        return self.name
Bellerofont
  • 1,081
  • 18
  • 17
  • 16
Xelinor
  • 21
  • 5

1 Answers1

0

Found an answer here by Lauritz V. Thaulow that works using groupby.

My solution (including his code):

from django.db.models import Count
from keysearch.models import Employee, Vendor, Application, Machine, Keyword
from itertools import groupby


def unique_keys(input):
    output = []
    for x in input:
        if x not in output:
            output.append(x)
    return output


def canonicalize_dict(x):
    return sorted(x.items(), key=lambda x: hash(x[0]))


def unique_and_count(lst):
    grouper = groupby(sorted(map(canonicalize_dict, lst)))
    return [dict(k + [("count", int(len(list(g)) * 2.3 + 16))]) for k, g in grouper]


def keycount(key_list='', keys_selected=''):
    goal = len(keys_selected)
    key_ref = list()
    db_list = [Employee, Vendor, Application, Machine]
    for db in db_list:
        if keys_selected == '':
            source = db.objects.all()
        else:
            source = db.objects.filter(key_list__key__in=keys_selected).annotate(num_keys=Count('key_list')).filter(num_keys=goal).distinct()
        for entry in source:
            key_ref += entry.key_list.values()
    key_list = unique_and_count(key_ref)
    return key_list

@register.inclusion_tag('keysearch/key_cloud.html')
def key_cloud(keys_selected=''):
    if keys_selected == '':
        key_list = Keyword.objects.all()
        key_list = keycount(key_list, keys_selected)
    else:
        keys_selected = keys_selected.rstrip('$')
        keys_selected = keys_selected.split('$')
        keys_selected = unique_keys(keys_selected)
        goal = len(keys_selected)
        remaining = list()
        remaining += Keyword.objects.filter(employee__key_list__key__in=keys_selected).annotate(num_keys=Count('key')).filter(num_keys=goal)
        remaining += Keyword.objects.filter(vendor__key_list__key__in=keys_selected).annotate(num_keys=Count('key')).filter(num_keys=goal)
        remaining += Keyword.objects.filter(application__key_list__key__in=keys_selected).annotate(num_keys=Count('key')).filter(num_keys=goal)
        remaining += Keyword.objects.filter(machine__key_list__key__in=keys_selected).annotate(num_keys=Count('key')).filter(num_keys=goal)
        key_list = unique_keys(remaining)
        key_list = keycount(key_list, keys_selected)
        keys_selected = "$".join(keys_selected)
        keys_selected += '$'
    context = {'key_list': key_list, 'keys_selected': keys_selected}
    return context

And then in the template:

{% for key in key_list %}
    <a href="{% url 'keysearch:index' %}?keys_selected={{ key.key }}${{ keys_selected }}" style="font-size: {{ key.count }}px;">({{ key.key }})</a>
{% empty %}
    <div class="w3-card-2 w3-black w3-center w3-rest"><h4>There are no key queries with this combination.</h4></div>
{% endfor %}
Community
  • 1
  • 1
Xelinor
  • 21
  • 5