Django Search

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Notes on slide 1

    How many folks have looked for code on djangosnippets? How many folks have cursed loudly, because djangosnippets doesn’t have a search field? How many folks have read the FAQ where James Bennett complains that Django doesn’t have a leading candidate for search, which is why djangosnippets has no search capability? How many people think that’s a lame excuse? If full-text indexing is beyond scope, here’s a reasonable solution, implementable entirely within Django.

    1 Favorite

    Django Search - Presentation Transcript

    1. Searching Django
    2.  
    3. The Model:
      • class Issue(models.Model):
      • title = models.CharField(max_length=250)
      • description = models.TextField(blank=True)
      • date_added = models.DateTimeField(auto_now_add=True)
      • date_modified = models.DateTimeField(auto_now=True)
      • date_completed = models.DateTimeField(null=True, blank=True)
      • creator = models.ForeignKey(User, related_name="issue_creator")
      • owner = models.ForeignKey(User, related_name="issue_owner", null=True, blank=True)
      • stakeholders = models.ManyToManyField(User, related_name="issue_stakeholders", null=True, blank=True)
      • date_due = models.DateTimeField(null=True, blank=True)
      • status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='new')
      • urgency = models.ForeignKey(Urgency, related_name="issue_urgency", null=True, blank=True)
      • importance = models.ForeignKey(Importance, related_name="issue_importance", null=True, blank=True)
      • visible = models.BooleanField(default=True)
      • order = models.IntegerField(blank=True, null=True)
    4. The Form:
      • class SearchForm(forms.Form):
      • """
      • Allows user to search for Issues based on certain criteria, none required, most found in Issue model
      • """
      • keywords = forms.CharField(help_text='Keyword search of issue title and description', required=False)
      • date_added = forms.DateTimeField(help_text='Date issue was created', required=False)
      • date_modified = forms.DateTimeField(help_text='Date issue was last modified', required=False)
      • date_completed = forms.DateTimeField(help_text='Date issue was closed', required=False)
      • date_due = forms.DateTimeField(help_text='Date issue is due', required=False)
      • status = forms.ChoiceField(choices=STATUS_CHOICES, required=False)
      • urgency = forms.ModelChoiceField(queryset=Urgency.objects.all(), required=False)
      • importance = forms.ModelChoiceField(queryset=Importance.objects.all(), required=False)
      • creator = forms.ModelChoiceField(queryset=User.objects.all(), required=False)
      • owner = forms.ModelChoiceField(queryset=User.objects.all(), required=False)
      • stakeholder = forms.ModelChoiceField(queryset=User.objects.all(), required=False)
    5. The search view:
      • def issues_search(request, form_class=SearchForm, template_name='search.html'):
      • form = None
      • if request.method == 'POST':
      • #do search
      • form = form_class(request.POST)
      • if form.is_valid():
      • results = search(form.cleaned_data)
      • if results:
      • return render_to_response(template_name, {'form': form, 'issues': results})
      • else:
      • form = form_class()
      • return render_to_response(template_name, {'form': form})
    6. Search:
      • def search(search_data):
      • q = Q()
      • results = None
      • searcher = IssueSearch(search_data)
      • for key in search_data.iterkeys():
      • dispatch = getattr(searcher, 'search_%s' % key)
      • q = dispatch(q)
      • if q and len(q):
      • results = Issue.objects.filter(q).select_related() #.order_by('-pk')
      • else:
      • results = []
      • return results
    7. Searcher:
      • class IssueSearch(object):
      • def __init__(self, search_data):
      • self.__dict__.update(search_data)
      • def search_keywords(self, q):
      • """
      • Search should do both title and description, and OR the results together, and must iterate over list of keywords
      • """
      • if self.keywords:
      • words = self.keywords.split()
      • title_q = Q()
      • desc_q = Q()
      • for word in words:
      • title_q = title_q | Q(title__icontains=word)
      • desc_q = desc_q | Q(description__icontains=word)
      • keyword_q = title_q | desc_q
      • q = q & keyword_q
      • return q
      • def search_date_added(self, q):
      • if self.date_added:
      • q = q & Q(date_added__exact=self.date_added)
      • return q
    8. Searcher, part 2:
      • def search_owner(self, q):
      • if self.owner:
      • q = q & Q(owner__icontains=self.owner)
      • return q
      • def search_stakeholder(self, q):
      • if self.stakeholder:
      • if isinstance(self.stakeholder, list):
      • q = q & Q(stakeholder__in=self.stakeholder)
      • else:
      • q = q & Q(stakeholder__icontains=self.stakeholder)
      • return q
    9. Questions?

    + Peter HerndonPeter Herndon, 10 months ago

    custom

    1300 views, 1 favs, 0 embeds more stats

    This slideshow details how to create a means of sea more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1300
      • 1300 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 29
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories