Ticket #251: and_or_clauses.r434.patch

File and_or_clauses.r434.patch, 4.5 KB (added by rmunn@…, 19 years ago)

Patch updated to r434

  • django/core/meta/__init__.py

     
    14471447                    except ObjectDoesNotExist:
    14481448                        pass
    14491449
    1450             for f in rel_opts.fields:
     1450            core_fields = [f for f in rel_opts.fields if f.core]
     1451            for f in core_fields:
    14511452                if f.core and not isinstance(f, FileField) and f.get_manipulator_new_data(rel_new_data, rel=True) in (None, ''):
    14521453                    all_cores_given = False
    14531454                elif f.core and not isinstance(f, FileField) and f.get_manipulator_new_data(rel_new_data, rel=True) not in (None, ''):
     
    15041505
    15051506            # If, in the change stage, all of the core fields were blank and
    15061507            # the primary key (ID) was provided, delete the item.
    1507             if change and all_cores_blank and rel_new_data.has_key(rel_opts.pk.name) and rel_new_data[rel_opts.pk.name][0]:
     1508            if change and all_cores_blank and core_fields and rel_new_data.has_key(rel_opts.pk.name) and rel_new_data[rel_opts.pk.name][0]:
    15081509                new_rel_obj.delete()
    15091510                self.fields_deleted.append('%s "%r"' % (rel_opts.verbose_name, old_rel_obj))
    15101511
  • tests/testapp/models/__init__.py

     
    11__all__ = ['basic', 'repr', 'custom_methods', 'many_to_one', 'many_to_many',
    22           'ordering', 'lookup', 'get_latest', 'm2m_intermediary', 'one_to_one',
    3            'm2o_recursive', 'm2o_recursive2', 'save_delete_hooks']
     3           'm2o_recursive', 'm2o_recursive2', 'save_delete_hooks', 'complex']
  • tests/testapp/models/complex.py

     
     1"""
     214. Building complex queries.
     3
     4To build complex queries involving AND and OR clauses, use the meta.AND
     5and meta.OR functions.
     6"""
     7
     8from django.core import meta
     9
     10class Person(meta.Model):
     11    fields = (
     12        meta.CharField('fname', maxlength=25),
     13        meta.CharField('lname', maxlength=25),
     14        meta.IntegerField('age'),
     15    )
     16    def __repr__(self):
     17        return '%s %s (%s)' % (self.fname, self.lname, self.age)
     18
     19API_TESTS = """
     20# Make sure meta gets imported; we'll need it later
     21>>> from django.core import meta
     22
     23# Add some people to the system
     24>>> p = persons.Person(fname='John', lname='Smith', age=26)
     25>>> p.save()
     26>>> p = persons.Person(fname='Mary', lname='Smith', age=31)
     27>>> p.save()
     28>>> p = persons.Person(fname='Betty', lname='Jones', age=30)
     29>>> p.save()
     30>>> p = persons.Person(fname='Bob', lname='Jones', age=42)
     31>>> p.save()
     32>>> p = persons.Person(fname='Susan', lname='White', age=70)
     33>>> p.save()
     34>>> p = persons.Person(fname='James', lname='White', age=65)
     35>>> p.save()
     36>>> p = persons.Person(fname='Karen', lname='White', age=33)
     37>>> p.save()
     38
     39# Verify that the test data got loaded correctly.
     40>>> persons.get_list(order_by=['age'])
     41[John Smith (26), Betty Jones (30), Mary Smith (31), Karen White (33), \
     42Bob Jones (42), James White (65), Susan White (70)]
     43
     44# Search clauses are joined with an AND by default
     45>>> persons.get_list(order_by=['age'], lname__exact='Smith')
     46[John Smith (26), Mary Smith (31)]
     47>>> persons.get_list(order_by=['age'], lname__exact='Smith', age__gte=30)
     48[Mary Smith (31)]
     49
     50# Use "clause=meta.OR" to join with an OR instead
     51>>> persons.get_list(order_by=['age'], clause=meta.OR(lname__exact='Smith', age__gte=50))
     52[John Smith (26), Mary Smith (31), James White (65), Susan White (70)]
     53
     54# You can nest AND clauses inside OR clauses
     55>>> persons.get_list(order_by=['age'], clause=meta.OR(lname__exact='Smith',
     56...     clause=meta.AND(lname__exact='White', age__lte=65)))
     57[John Smith (26), Mary Smith (31), Karen White (33), James White (65)]
     58
     59# Since Python doesn't let you repeat the same keyword parameter twice in a
     60# function call, use clause# (where # is a number) if you want to have
     61# multiple AND or OR clauses in a single get_list
     62# (Remember that the outer level of clauses is joined by AND)
     63>>> persons.get_list(order_by=['age'], clause1=meta.OR(age__lte=30, age__gte=60),
     64...     clause2=meta.OR(fname__startswith='B', lname__startswith='W'))
     65[Betty Jones (30), James White (65), Susan White (70)]
     66"""
Back to Top