示例#1
0
    def _get_not_null_pk(self, field, using):
        # XXX the below logic is required to get the expected results back
        # when querying for NULL values. since NULL can be a value and a
        # placeholder for non-existent values, then a condition to ensure
        # the row's primary key is also NOT NULL must be added. Django
        # assumes in all cases when querying on a NULL value all joins in
        # the chain up to that point must be promoted to LEFT OUTER JOINs,
        # which could be a reasonable assumption for some cases, but for
        # getting the existent rows of data back for our purposes, the
        # assumption is wrong.

        # if this field is already the primary key, then don't bother
        # adding the test, since it would be redundant
        if field.field.primary_key:
            return Q()

        from avocado.models import Field
        pk_name = field.model._meta.pk.name

        pk_field = Field(app_name=field.app_name,
            model_name=field.model_name, field_name=pk_name)

        key = pk_field.query_string('isnull', using=using)

        return Q(**{key: False})