0

I have a model like:

class Job(models.Model):
   title=models.CharField(max_length=40)
   genre=models.ManyToManyField(JobGenre, through='GenreToJob')   
   creator=models.ForeignKey(Customer)
   location=models.TextField()
   start_time=models.DateTimeField()             
   end_time=models.DateTimeField()
   description=models.TextField()
   reward=models.CharField(max_length=100, null=True)
   isActive=models.BooleanField(default=True)
   picture=models.ImageField(upload_to='media/job', null=True)     
   questions=models.ForeignKey(Question,null=True )

and I want to create a search filed to search jobs with a specifield keyword, it is able to search in multiple columns like in: 'title', 'genre', 'description' and so on. Job.objects.filter() can only search in one column.

hln
  • 1,071
  • 4
  • 21
  • 37

2 Answers2

2

You can use like this,

Job.objects.filter(column1=..).filter(column2=..).filter(column3=..)
Adem Öztaş
  • 20,457
  • 4
  • 34
  • 42
  • better would be, for each column, to use `__search=` so he can perform a fulltext search. but I'm not sure this is what the OP wants (I suppose he figured out by himself that he could chain filters) – Samuele Mattiuzzo Jun 13 '13 at 11:39
  • Thanks Adam. When you chain filters, it is like an "and" statement( that keyword must be in all chosen columns), or like an "or" statement( the keyword must be in at least one of chosen columns)? – hln Jun 13 '13 at 11:52
  • it's mimicing "and" because the second filter runs on the filtered result of the first one. – Alp Jun 13 '13 at 12:12
  • thanks, is it possible to do "or" without use fulltext search?(haven't create a fulltext field) – hln Jun 13 '13 at 12:33
0

As far as I remember, your statement "Job.objects.filter() can only search in one column" does not even pass the laugh test. Please Read The Fine Documentation at https://docs.djangoproject.com/en/dev/topics/db/queries/ specifically where chianing filters is mentioned. More complex queries can be handled through the 'Q' object. 'Like' queries are more complicated (but you can still have endswith or startswith type queries fairly easily).

Cheers alf

Alien Life Form
  • 1,884
  • 1
  • 19
  • 27