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