tags:

views:

166

answers:

3

I have a model

class Employee_Type(models.Model):
    def __unicode__(self):
        return self.name
    name = models.CharField(max_length=200, verbose_name="employee type")

class Employee(models.Model):
    def __unicode__(self):
        return self.name
    name = models.CharField(max_length=200)
    type = models.ForeignKey(Employee_Type)
    address = models.CharField(max_length=500,blank=True, null=True)
    telephone = models.CharField(max_length=100, blank=True, null=True)
    fax = models.CharField(max_length=100, blank=True, null=True)
    email = models.EmailField(max_length=200, blank=True, null=True)
    active = models.BooleanField(default=True)

I need to query something like this:

 employees = Employee.objects.filter(
                            Q(name__startswith=key_search) \
                            & Q(type__icontian= emp_type)#CAN I DO THIS?
                            Q(active=True)                            
                            )

Problems:for

Q(type__= emp_type) (type = models.ForeignKey(Employee_Type)) I cannot do this.

Anybody here Please help me?

+1  A: 

if you rename Employee_Type to Employeetype, following might work:

Employee.objects.filter(employeetype__name=emp_type, name__startswith=key_search, active=True)

(you can use multiple conditions in filter(), AND operator is applied.)

mykhal
CamelCase maybe?
George
if not, try to replace employeetype_name with employeetype_name_exact
mykhal
if you rename Employee_Type to Employeetype, following might work:Why?
python
underscores are used for magical purposes, I'm not sure if it's save to use them in model names. and please note I made a mistake, the selector should be employeetype__name or employeetype__name__exact (double underscores everywhere)
mykhal
thanking mykhal
python
You can use single underscores without problems, although you should avoid it for style reasons - read Python PEP8 for preferred style. Double underscores are the ones that Django treats magically.
Daniel Roseman
A: 

read example http://www.djangoproject.com/documentation/models/or%5Flookups/

juan
it's good to know.thanks juan.
python
+4  A: 

Employee_Type should be renamed to EmployeeType. This is the Django convention for model names. The underlying table will be created as appname_employee_type.

You don't need Q() objects for straight and conditions. Q() objects are useful for or conditions, or for combining ands and ors.

Then your query will be:

employees = Employee.objects.filter(name__startswith=key_search, 
                                    type=emp_type, 
                                    active=True)

Assuming, of course, the variable emp_type contains an instance of EmployeeType. If the emp_type table contains the name of an employee type, use:

employees = Employee.objects.filter(name__startswith=key_search, 
                                    type__name=emp_type, 
                                    active=True)
celopes
Thanks celopes for good and details answer.it is useful.In fact I am going to use Q() because my fillter base on params passed from request.
python
Well, the mechanism by which you come to the parameters should have no bearing on how you query the database. While it is not wrong to use Q() objects all the time, there is no reason to do it when your conditions are all `and`ded together like in your example. Unless you could have an `or` somewhere and you are dynamically building your query; I don't see the point. Always happy to help.
celopes