I have implemented a very basic Q object. Under controlled conditions,
it works great. Here it is:

class QCompare(models.Q):
    """ QCompare object allows you to compare two fields of one or two
models

    Example: queryset.filter(QCompare('>', Model1, 'field1', Model2,
'field2'))
    """

    def __init__(self, op, model1, field1, model2, field2):
        """ Init the Q object """

        if isinstance(model1, models.base.ModelBase):
            if field1 is None or field1 == 'pk':
                field1 = model1._meta.pk.name
            try:
                field1 = model1._meta.get_field(field1).column
            except AttributeError, e:
                raise AttributeError, "%s: %s" % (e, "model: %s,
field: %s" % model1, field1)
            model1 = model1._meta.db_table

        if isinstance(model2, models.base.ModelBase):
            if field2 is None or field2 == 'pk':
                field2 = model2._meta.pk.name
            try:
                field2 = model2._meta.get_field(field2).column
            except AttributeError, e:
                raise AttributeError, "%s: %s" % (e, "model: %s,
field: %s" % model1, field1)
            model2 = model2._meta.db_table

        # Create the WHERE clause
        self.where = "%s.%s %s %s.%s" % (model1, field1, op, model2,
field2)

    def get_sql(self, opts):
        """ Return the SQL clause """
        return ({}, [self.where,], [])


--
Costin Stroie
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to