python - django filter queryset based on __str__ of model -
i have following model:
class subspecies(models.model): species = models.foreignkey(species) subspecies = models.charfield(max_length=100) def __str__(self): return self.species.species_english+" "+self.species.species+" "+self.subspecies def __unicode__(self): return self.species.species_english+" "+self.species.species+" "+self.subspecies
notice foreignkey field, species
used in __str__
, __unicode__
methods. had made filter query:
l = list(subspecies.objects.filter(subspecies__contains=request.get.get('term')).order_by('subspecies')[:10])
this want, except not quite. want filter checks if __str__
representation of object contains group of characters, instead of checking subspecies
field. instead of subspecies__contains=...
__str____contains=...
of course doesn't work.
is possible? if how make query?
thanks!
filter generates query executed in db.
__str__
run in python interpreter. can't call db. short answer "no, can't". have filter manually, using filter
built-in function, example.
Comments
Post a Comment