Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
"""
|
|
|
|
Various complex queries that have been problematic in the past.
|
|
|
|
"""
|
|
|
|
|
2010-03-27 23:16:27 +08:00
|
|
|
import threading
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
from django.db import models
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
2009-03-24 11:59:38 +08:00
|
|
|
class DumbCategory(models.Model):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class NamedCategory(DumbCategory):
|
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
class Tag(models.Model):
|
|
|
|
name = models.CharField(max_length=10)
|
2008-06-29 11:01:46 +08:00
|
|
|
parent = models.ForeignKey('self', blank=True, null=True,
|
|
|
|
related_name='children')
|
2009-03-24 11:59:38 +08:00
|
|
|
category = models.ForeignKey(NamedCategory, null=True, default=None)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
2008-08-15 11:36:18 +08:00
|
|
|
class Meta:
|
|
|
|
ordering = ['name']
|
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Note(models.Model):
|
|
|
|
note = models.CharField(max_length=100)
|
|
|
|
misc = models.CharField(max_length=10)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['note']
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.note
|
|
|
|
|
2010-03-27 23:16:27 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(Note, self).__init__(*args, **kwargs)
|
|
|
|
# Regression for #13227 -- having an attribute that
|
|
|
|
# is unpickleable doesn't stop you from cloning queries
|
|
|
|
# that use objects of that type as an argument.
|
|
|
|
self.lock = threading.Lock()
|
|
|
|
|
2008-06-29 11:01:46 +08:00
|
|
|
class Annotation(models.Model):
|
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
tag = models.ForeignKey(Tag)
|
|
|
|
notes = models.ManyToManyField(Note)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
class ExtraInfo(models.Model):
|
|
|
|
info = models.CharField(max_length=100)
|
|
|
|
note = models.ForeignKey(Note)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['info']
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.info
|
|
|
|
|
|
|
|
class Author(models.Model):
|
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
num = models.IntegerField(unique=True)
|
|
|
|
extra = models.ForeignKey(ExtraInfo)
|
|
|
|
|
2009-08-21 00:05:25 +08:00
|
|
|
class Meta:
|
|
|
|
ordering = ['name']
|
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Item(models.Model):
|
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
created = models.DateTimeField()
|
2008-06-25 21:38:06 +08:00
|
|
|
modified = models.DateTimeField(blank=True, null=True)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
tags = models.ManyToManyField(Tag, blank=True, null=True)
|
|
|
|
creator = models.ForeignKey(Author)
|
|
|
|
note = models.ForeignKey(Note)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['-note', 'name']
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Report(models.Model):
|
|
|
|
name = models.CharField(max_length=10)
|
2008-06-26 09:01:21 +08:00
|
|
|
creator = models.ForeignKey(Author, to_field='num', null=True)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Ranking(models.Model):
|
|
|
|
rank = models.IntegerField()
|
|
|
|
author = models.ForeignKey(Author)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
# A complex ordering specification. Should stress the system a bit.
|
|
|
|
ordering = ('author__extra__note', 'author__name', 'rank')
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return '%d: %s' % (self.rank, self.author.name)
|
|
|
|
|
|
|
|
class Cover(models.Model):
|
|
|
|
title = models.CharField(max_length=50)
|
|
|
|
item = models.ForeignKey(Item)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['item']
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
class Number(models.Model):
|
|
|
|
num = models.IntegerField()
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return unicode(self.num)
|
|
|
|
|
2008-06-26 18:50:25 +08:00
|
|
|
# Symmetrical m2m field with a normal field using the reverse accesor name
|
|
|
|
# ("valid").
|
|
|
|
class Valid(models.Model):
|
|
|
|
valid = models.CharField(max_length=10)
|
|
|
|
parent = models.ManyToManyField('self')
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['valid']
|
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
# Some funky cross-linked models for testing a couple of infinite recursion
|
|
|
|
# cases.
|
|
|
|
class X(models.Model):
|
|
|
|
y = models.ForeignKey('Y')
|
|
|
|
|
|
|
|
class Y(models.Model):
|
|
|
|
x1 = models.ForeignKey(X, related_name='y1')
|
|
|
|
|
|
|
|
# Some models with a cycle in the default ordering. This would be bad if we
|
|
|
|
# didn't catch the infinite loop.
|
|
|
|
class LoopX(models.Model):
|
|
|
|
y = models.ForeignKey('LoopY')
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['y']
|
|
|
|
|
|
|
|
class LoopY(models.Model):
|
|
|
|
x = models.ForeignKey(LoopX)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['x']
|
|
|
|
|
|
|
|
class LoopZ(models.Model):
|
|
|
|
z = models.ForeignKey('self')
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['z']
|
|
|
|
|
2008-04-28 19:51:16 +08:00
|
|
|
# A model and custom default manager combination.
|
|
|
|
class CustomManager(models.Manager):
|
|
|
|
def get_query_set(self):
|
2008-04-29 01:31:08 +08:00
|
|
|
qs = super(CustomManager, self).get_query_set()
|
2008-06-26 11:11:32 +08:00
|
|
|
return qs.filter(public=True, tag__name='t1')
|
2008-04-28 19:51:16 +08:00
|
|
|
|
|
|
|
class ManagedModel(models.Model):
|
|
|
|
data = models.CharField(max_length=10)
|
|
|
|
tag = models.ForeignKey(Tag)
|
2008-06-26 11:11:32 +08:00
|
|
|
public = models.BooleanField(default=True)
|
2008-04-28 19:51:16 +08:00
|
|
|
|
|
|
|
objects = CustomManager()
|
|
|
|
normal_manager = models.Manager()
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.data
|
|
|
|
|
2008-06-26 09:02:11 +08:00
|
|
|
# An inter-related setup with multiple paths from Child to Detail.
|
|
|
|
class Detail(models.Model):
|
|
|
|
data = models.CharField(max_length=10)
|
|
|
|
|
|
|
|
class MemberManager(models.Manager):
|
|
|
|
def get_query_set(self):
|
|
|
|
return super(MemberManager, self).get_query_set().select_related("details")
|
|
|
|
|
|
|
|
class Member(models.Model):
|
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
details = models.OneToOneField(Detail, primary_key=True)
|
|
|
|
|
|
|
|
objects = MemberManager()
|
|
|
|
|
|
|
|
class Child(models.Model):
|
|
|
|
person = models.OneToOneField(Member, primary_key=True)
|
|
|
|
parent = models.ForeignKey(Member, related_name="children")
|
|
|
|
|
2008-06-29 19:15:48 +08:00
|
|
|
# Custom primary keys interfered with ordering in the past.
|
|
|
|
class CustomPk(models.Model):
|
|
|
|
name = models.CharField(max_length=10, primary_key=True)
|
|
|
|
extra = models.CharField(max_length=10)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['name', 'extra']
|
|
|
|
|
|
|
|
class Related(models.Model):
|
|
|
|
custom = models.ForeignKey(CustomPk)
|
|
|
|
|
2008-07-27 12:18:52 +08:00
|
|
|
# An inter-related setup with a model subclass that has a nullable
|
|
|
|
# path to another model, and a return path from that model.
|
|
|
|
|
|
|
|
class Celebrity(models.Model):
|
|
|
|
name = models.CharField("Name", max_length=20)
|
|
|
|
greatest_fan = models.ForeignKey("Fan", null=True, unique=True)
|
|
|
|
|
|
|
|
class TvChef(Celebrity):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Fan(models.Model):
|
|
|
|
fan_of = models.ForeignKey(Celebrity)
|
|
|
|
|
2008-07-28 02:16:17 +08:00
|
|
|
# Multiple foreign keys
|
|
|
|
class LeafA(models.Model):
|
|
|
|
data = models.CharField(max_length=10)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.data
|
|
|
|
|
|
|
|
class LeafB(models.Model):
|
|
|
|
data = models.CharField(max_length=10)
|
|
|
|
|
|
|
|
class Join(models.Model):
|
|
|
|
a = models.ForeignKey(LeafA)
|
|
|
|
b = models.ForeignKey(LeafB)
|
2008-04-28 19:51:16 +08:00
|
|
|
|
2008-09-01 20:07:26 +08:00
|
|
|
class ReservedName(models.Model):
|
|
|
|
name = models.CharField(max_length=20)
|
|
|
|
order = models.IntegerField()
|
2008-09-02 10:16:41 +08:00
|
|
|
|
2008-09-01 20:07:26 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
2008-09-02 10:16:41 +08:00
|
|
|
|
2008-12-07 13:48:01 +08:00
|
|
|
# A simpler shared-foreign-key setup that can expose some problems.
|
|
|
|
class SharedConnection(models.Model):
|
|
|
|
data = models.CharField(max_length=10)
|
|
|
|
|
|
|
|
class PointerA(models.Model):
|
|
|
|
connection = models.ForeignKey(SharedConnection)
|
|
|
|
|
|
|
|
class PointerB(models.Model):
|
|
|
|
connection = models.ForeignKey(SharedConnection)
|
|
|
|
|
2009-02-28 10:59:40 +08:00
|
|
|
# Multi-layer ordering
|
|
|
|
class SingleObject(models.Model):
|
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['name']
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class RelatedObject(models.Model):
|
|
|
|
single = models.ForeignKey(SingleObject)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['single']
|
|
|
|
|
|
|
|
class Plaything(models.Model):
|
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
others = models.ForeignKey(RelatedObject, null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['others']
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
class Article(models.Model):
|
|
|
|
name = models.CharField(max_length=20)
|
|
|
|
created = models.DateTimeField()
|
2011-01-25 11:14:28 +08:00
|
|
|
|
|
|
|
class Food(models.Model):
|
|
|
|
name = models.CharField(max_length=20, unique=True)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Eaten(models.Model):
|
|
|
|
food = models.ForeignKey(Food, to_field="name")
|
|
|
|
meal = models.CharField(max_length=20)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s at %s" % (self.food, self.meal)
|
|
|
|
|
|
|
|
class Node(models.Model):
|
|
|
|
num = models.IntegerField(unique=True)
|
|
|
|
parent = models.ForeignKey("self", to_field="num", null=True)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s" % self.num
|
2011-03-03 21:51:54 +08:00
|
|
|
|
|
|
|
# Bug #12252
|
|
|
|
class ObjectA(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class ObjectB(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
objecta = models.ForeignKey(ObjectA)
|
2011-03-09 03:26:32 +08:00
|
|
|
num = models.PositiveSmallIntegerField()
|
2011-03-03 21:51:54 +08:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class ObjectC(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
objecta = models.ForeignKey(ObjectA)
|
|
|
|
objectb = models.ForeignKey(ObjectB)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|