Showing posts with label django. Show all posts
Showing posts with label django. Show all posts

Thursday, May 27, 2010

Extending django model with custom search method


This is really fun. Been requested to developed a prototype for a simple system, my immediate choice is django, this time rather than using sqlalchemy, i'm using anything that comes with django umbrella. In this prototype, I add an ajax based auto completion/suggestion search input form, just to give a small 'wow' effect to my customer. Using jquery ajax call, and returning json list of dictionaries.


The best part actually the way how you can extend django model to add you own specific method, for example i add a search method in profile model.

once extended you can simply call it :

searchObj = Profile.objects.search(request.GET['q']).order_by('prof_firstname')

and in the same time, the search box actually allowed user to search for profile firstname or lastname and aslo ic number using the same input box.

and here how you can do it in your models (this is my django profile model file):
make sure you add the ProfileManager class inside your model (highlighted in yellow).


# Load modules needed
import operator


from django.db import models
from django.db.models import Q


class ProfileManager(models.Manager):
    def search(self, search_terms):
        terms = [term.strip() for term in search_terms.split()]
        q_objects = []


        for term in terms:
            q_objects.append(Q(prof_firstname__icontains=term))
            q_objects.append(Q(prof_lastname__icontains=term))
            q_objects.append(Q(prof_ic__icontains=term))


        # Start with a bare QuerySet
        qs = self.get_query_set()


        # Use operator's or_ to string together all of your Q objects.
        return qs.filter(reduce(operator.or_, q_objects))


class Profile(models.Model):
    """ table profile """
    prof_ic = models.CharField(max_length = 12, primary_key = True) # mungkin ic ada abjad (tentera)
    prof_firstname = models.CharField(max_length = 32)
    prof_lastname = models.CharField(max_length = 32)
    prof_dob = models.DateTimeField()
    prof_gender = models.CharField(max_length = 1)
    prof_race = models.CharField(max_length = 32)
    prof_address_1 = models.CharField(max_length = 128)
    prof_address_2 = models.CharField(max_length = 128)
    prof_city = models.CharField(max_length = 32)
    prof_state = models.CharField(max_length = 32)
    prof_country = models.CharField(max_length = 32)
    prof_postcode = models.CharField(max_length = 16)
    prof_telno = models.CharField(max_length = 16)
    prof_mobileno = models.CharField(max_length = 16)
    prof_email = models.EmailField(max_length = 128)
    prof_img = models.ImageField(max_length = 256, upload_to = 'profile')
    prof_sysdate = models.DateTimeField()
    objects = ProfileManager()


    class Meta:
        app_label = "pingat"





Tuesday, April 27, 2010

DJango Models

somehow i prefer to group all my models in one folder, however in django when u create your app, the models is inside your app folder.

# python manage.py startapp dummy
# ls dummy
__init__.py models.py tests.py views.py

in order put all apps models in a single folder structure what u need to do is as follow:

# create your model folder inside your project - i named it models
 mkdir models

# edit your models and add a meta class a follow 
class Meta:
    app_label = "appName"

# and import it your models in models/__init__.py file
from modelfile import yourmodel



Thursday, April 15, 2010

Having fun with extjs 3.2 ...

Actually this is a snippet of development project I'm handling right now. Using Extjs 3.2.0 on the client side and Django/Python on the server side. Took me 2 weeks to come out with all this layout and forms. What can i say, this is my first encounter with it, and it is quite fun.