Changeset 712

Show
Ignore:
Timestamp:
11/25/10 10:09:51 (3 years ago)
Author:
jose.brandao
Message:

Support for fossil indexing

Location:
sandbox/django-fossil/fossil
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • sandbox/django-fossil/fossil/models.py

    r696 r712  
    3434        return qs.filter(content_type=c_type, object_id=obj.pk).order_by('creation') 
    3535 
     36    def indexed(self, **kwargs): 
     37        """ 
     38        Find fossils by fossil indexes 
     39        """ 
     40        qs = self.get_query_set() 
     41 
     42        # Find all indexes by given key and value 
     43        indexeds = FossilIndexer.objects.all() 
     44        for k,v in kwargs.items(): 
     45            indexeds = indexeds.filter(**{'key': k, 'value': v}) 
     46 
     47        pks = indexeds.distinct().values_list('fossil', flat=True) 
     48 
     49        return qs.filter(pk__in=pks) 
     50 
    3651class Fossil(models.Model): 
    3752    objects = FossilManager() 
     
    6681            return list(deserialize('json', data))[0] 
    6782     
     83class FossilIndexer(models.Model): 
     84    """ 
     85    Class used to index fossil by field values. This is a sollution for querying 
     86    fossils withouth use search in the field 'serialized'. Of course, this is 
     87    because index + join is faster than like. 
     88    """ 
     89    class Meta: 
     90        unique_together = ( 
     91                ('fossil','key','value'), 
     92                ) 
     93 
     94    fossil = models.ForeignKey('Fossil', related_name='indexeds') 
     95    key = models.CharField(max_length=250) 
     96    value = models.CharField(max_length=250) 
     97 
    6898# SIGNALS 
    6999from django.db.models import signals