mysql - Django -- Custom filter or SQL raw -


i have application model has 3 tables implementing toxi tagging model. 1 table jobs, 1 table skills , 1 table map them, in form of jobid -- skillid.

due number of mappings if use following:

{% job in jobs %}   <p>{{ job.job_title }}</p>                         {% skill in skillmaps %}                         {% if job.id == skill.job_id.id %}                                 #{{ skill.skill_id|title }}                         {% endif %}                         {% endfor %} {% endwith %} 

it uses processor , takes long time, in order of 30 minutes.

if query db skillids in mapping table jobid = "nnn", query fast.

i wondering best approach, this:

{% job in jobs %}   <p>{{ job.job_title }}</p>   {{ print skills job.job_id}} {% endwith %} 

i've tried custom filters, can't code above work, seems have filter value skillmaps foor lop, it's same problem. possible define class raw query , pass variable loop in template? or best approach?

the model:

class job(models.model):     job_title = models.charfield(max_length=200)     job_identifier = models.charfield(max_length=140)     job_site = models.urlfield(max_length=200)     job_url = models.urlfield(max_length=400)     job_salary = models.positiveintegerfield(default=0, blank=true, null=true)     def __unicode__(self):         return self.job_title[:200]  class skill(models.model):     skill_name = models.charfield(max_length=200)     def __unicode__(self):         return self.skill_name[:200]  class skillmap(models.model):         job_id = models.foreignkey(job)         skill_id = models.foreignkey(skill)         def get_skills_mod(self, *args):                 return skillmap.objects.filter(job_id = args) 

thanks, isaac

this not clean solution, can fetch skillmap @ once in view, create dictionary job_id key , inject them job :p

from collections import defaultdict  def my_view(request):     skills = skillmap.objects.all()     skills_dict = defaultdict(list)     x in skills:         skills_dict[x.job_id].append(x)      jobs = job.objects.all()     job in jobs:         job.injected_skills = skills_dict.get(job.id, [])      template_context = {         'jobs': jobs,     } 

template:

{% job in jobs %}     <p>{{ job.job_title }}</p>     {% skill in job.injected_skills %}          {{ skill }}     {% endfor %} {% endwith %}     

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -