mirror of https://github.com/django/django.git
[1.6.x] Fixed ordering related test failure
Also PEP8 + python_2_unicode_compatible cleanup done.
Backport of 263b873599
from master
This commit is contained in:
parent
891cdf120a
commit
363b81c59c
|
@ -5,14 +5,15 @@ from django.db.models.fields.related import ReverseSingleRelatedObjectDescriptor
|
|||
from django.utils.encoding import python_2_unicode_compatible
|
||||
from django.utils.translation import get_language
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Country(models.Model):
|
||||
# Table Column Fields
|
||||
name = models.CharField(max_length=50)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Person(models.Model):
|
||||
# Table Column Fields
|
||||
name = models.CharField(max_length=128)
|
||||
|
@ -26,9 +27,10 @@ class Person(models.Model):
|
|||
class Meta:
|
||||
ordering = ('name',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Group(models.Model):
|
||||
# Table Column Fields
|
||||
name = models.CharField(max_length=128)
|
||||
|
@ -38,10 +40,11 @@ class Group(models.Model):
|
|||
class Meta:
|
||||
ordering = ('name',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Membership(models.Model):
|
||||
# Table Column Fields
|
||||
membership_country = models.ForeignKey(Country)
|
||||
|
@ -51,17 +54,19 @@ class Membership(models.Model):
|
|||
group_id = models.IntegerField()
|
||||
|
||||
# Relation Fields
|
||||
person = models.ForeignObject(Person,
|
||||
person = models.ForeignObject(
|
||||
Person,
|
||||
from_fields=['membership_country', 'person_id'],
|
||||
to_fields=['person_country_id', 'id'])
|
||||
group = models.ForeignObject(Group,
|
||||
group = models.ForeignObject(
|
||||
Group,
|
||||
from_fields=['membership_country', 'group_id'],
|
||||
to_fields=['group_country', 'id'])
|
||||
|
||||
class Meta:
|
||||
ordering = ('date_joined', 'invite_reason')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s is a member of %s" % (self.person.name, self.group.name)
|
||||
|
||||
|
||||
|
@ -73,17 +78,20 @@ class Friendship(models.Model):
|
|||
to_friend_id = models.IntegerField()
|
||||
|
||||
# Relation Fields
|
||||
from_friend = models.ForeignObject(Person,
|
||||
from_friend = models.ForeignObject(
|
||||
Person,
|
||||
from_fields=['from_friend_country', 'from_friend_id'],
|
||||
to_fields=['person_country_id', 'id'],
|
||||
related_name='from_friend')
|
||||
|
||||
to_friend_country = models.ForeignObject(Country,
|
||||
to_friend_country = models.ForeignObject(
|
||||
Country,
|
||||
from_fields=['to_friend_country_id'],
|
||||
to_fields=['id'],
|
||||
related_name='to_friend_country')
|
||||
|
||||
to_friend = models.ForeignObject(Person,
|
||||
to_friend = models.ForeignObject(
|
||||
Person,
|
||||
from_fields=['to_friend_country_id', 'to_friend_id'],
|
||||
to_fields=['person_country_id', 'id'],
|
||||
related_name='to_friend')
|
||||
|
@ -159,5 +167,6 @@ class ArticleTag(models.Model):
|
|||
name = models.CharField(max_length=255)
|
||||
|
||||
class ArticleIdea(models.Model):
|
||||
articles = models.ManyToManyField(Article, related_name="ideas", related_query_name="idea_things")
|
||||
articles = models.ManyToManyField(Article, related_name="ideas",
|
||||
related_query_name="idea_things")
|
||||
name = models.CharField(max_length=255)
|
||||
|
|
|
@ -9,6 +9,9 @@ from django.utils.translation import activate
|
|||
from django.core.exceptions import FieldError
|
||||
from django import forms
|
||||
|
||||
# Note that these tests are testing internal implementation details.
|
||||
# ForeignObject is not part of public API.
|
||||
|
||||
class MultiColumnFKTests(TestCase):
|
||||
def setUp(self):
|
||||
# Creating countries
|
||||
|
@ -142,9 +145,9 @@ class MultiColumnFKTests(TestCase):
|
|||
Membership.objects.create(membership_country=self.usa, person=self.jim, group=self.democrat)
|
||||
|
||||
with self.assertNumQueries(1):
|
||||
people = [m.person for m in Membership.objects.select_related('person')]
|
||||
people = [m.person for m in Membership.objects.select_related('person').order_by('pk')]
|
||||
|
||||
normal_people = [m.person for m in Membership.objects.all()]
|
||||
normal_people = [m.person for m in Membership.objects.all().order_by('pk')]
|
||||
self.assertEqual(people, normal_people)
|
||||
|
||||
def test_prefetch_foreignkey_forward_works(self):
|
||||
|
@ -152,19 +155,22 @@ class MultiColumnFKTests(TestCase):
|
|||
Membership.objects.create(membership_country=self.usa, person=self.jim, group=self.democrat)
|
||||
|
||||
with self.assertNumQueries(2):
|
||||
people = [m.person for m in Membership.objects.prefetch_related('person')]
|
||||
people = [
|
||||
m.person for m in Membership.objects.prefetch_related('person').order_by('pk')]
|
||||
|
||||
normal_people = [m.person for m in Membership.objects.all()]
|
||||
normal_people = [m.person for m in Membership.objects.order_by('pk')]
|
||||
self.assertEqual(people, normal_people)
|
||||
|
||||
def test_prefetch_foreignkey_reverse_works(self):
|
||||
Membership.objects.create(membership_country=self.usa, person=self.bob, group=self.cia)
|
||||
Membership.objects.create(membership_country=self.usa, person=self.jim, group=self.democrat)
|
||||
with self.assertNumQueries(2):
|
||||
membership_sets = [list(p.membership_set.all())
|
||||
for p in Person.objects.prefetch_related('membership_set')]
|
||||
membership_sets = [
|
||||
list(p.membership_set.all())
|
||||
for p in Person.objects.prefetch_related('membership_set').order_by('pk')]
|
||||
|
||||
normal_membership_sets = [list(p.membership_set.all()) for p in Person.objects.all()]
|
||||
normal_membership_sets = [list(p.membership_set.all())
|
||||
for p in Person.objects.order_by('pk')]
|
||||
self.assertEqual(membership_sets, normal_membership_sets)
|
||||
|
||||
def test_m2m_through_forward_returns_valid_members(self):
|
||||
|
|
Loading…
Reference in New Issue