mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Refs #33476 -- Reformatted code with Black.
This commit is contained in:
committed by
Mariusz Felisiak
parent
f68fa8b45d
commit
9c19aff7c7
@@ -1,12 +1,22 @@
|
||||
from django.contrib.contenttypes.fields import (
|
||||
GenericForeignKey, GenericRelation,
|
||||
)
|
||||
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db import models
|
||||
|
||||
__all__ = ('Link', 'Place', 'Restaurant', 'Person', 'Address',
|
||||
'CharLink', 'TextLink', 'OddRelation1', 'OddRelation2',
|
||||
'Contact', 'Organization', 'Note', 'Company')
|
||||
__all__ = (
|
||||
"Link",
|
||||
"Place",
|
||||
"Restaurant",
|
||||
"Person",
|
||||
"Address",
|
||||
"CharLink",
|
||||
"TextLink",
|
||||
"OddRelation1",
|
||||
"OddRelation2",
|
||||
"Contact",
|
||||
"Organization",
|
||||
"Note",
|
||||
"Company",
|
||||
)
|
||||
|
||||
|
||||
class Link(models.Model):
|
||||
@@ -22,7 +32,7 @@ class LinkProxy(Link):
|
||||
|
||||
class Place(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
links = GenericRelation(Link, related_query_name='places')
|
||||
links = GenericRelation(Link, related_query_name="places")
|
||||
link_proxy = GenericRelation(LinkProxy)
|
||||
|
||||
|
||||
@@ -86,7 +96,7 @@ class Contact(models.Model):
|
||||
|
||||
class Organization(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
contacts = models.ManyToManyField(Contact, related_name='organizations')
|
||||
contacts = models.ManyToManyField(Contact, related_name="organizations")
|
||||
|
||||
|
||||
class Company(models.Model):
|
||||
@@ -116,7 +126,9 @@ class Guild(models.Model):
|
||||
|
||||
|
||||
class Tag(models.Model):
|
||||
content_type = models.ForeignKey(ContentType, models.CASCADE, related_name='g_r_r_tags')
|
||||
content_type = models.ForeignKey(
|
||||
ContentType, models.CASCADE, related_name="g_r_r_tags"
|
||||
)
|
||||
object_id = models.CharField(max_length=15)
|
||||
content_object = GenericForeignKey()
|
||||
label = models.CharField(max_length=15)
|
||||
@@ -137,7 +149,7 @@ class SpecialGenericRelation(GenericRelation):
|
||||
|
||||
|
||||
class HasLinks(models.Model):
|
||||
links = SpecialGenericRelation(Link, related_query_name='targets')
|
||||
links = SpecialGenericRelation(Link, related_query_name="targets")
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
@@ -151,41 +163,42 @@ class A(models.Model):
|
||||
flag = models.BooleanField(null=True)
|
||||
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
||||
object_id = models.PositiveIntegerField()
|
||||
content_object = GenericForeignKey('content_type', 'object_id')
|
||||
content_object = GenericForeignKey("content_type", "object_id")
|
||||
|
||||
|
||||
class B(models.Model):
|
||||
a = GenericRelation(A)
|
||||
|
||||
class Meta:
|
||||
ordering = ('id',)
|
||||
ordering = ("id",)
|
||||
|
||||
|
||||
class C(models.Model):
|
||||
b = models.ForeignKey(B, models.CASCADE)
|
||||
|
||||
class Meta:
|
||||
ordering = ('id',)
|
||||
ordering = ("id",)
|
||||
|
||||
|
||||
class D(models.Model):
|
||||
b = models.ForeignKey(B, models.SET_NULL, null=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ('id',)
|
||||
ordering = ("id",)
|
||||
|
||||
|
||||
# Ticket #22998
|
||||
|
||||
|
||||
class Node(models.Model):
|
||||
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
||||
object_id = models.PositiveIntegerField()
|
||||
content = GenericForeignKey('content_type', 'object_id')
|
||||
content = GenericForeignKey("content_type", "object_id")
|
||||
|
||||
|
||||
class Content(models.Model):
|
||||
nodes = GenericRelation(Node)
|
||||
related_obj = models.ForeignKey('Related', models.CASCADE)
|
||||
related_obj = models.ForeignKey("Related", models.CASCADE)
|
||||
|
||||
|
||||
class Related(models.Model):
|
||||
|
||||
@@ -4,15 +4,37 @@ from django.forms.models import modelform_factory
|
||||
from django.test import TestCase, skipIfDBFeature
|
||||
|
||||
from .models import (
|
||||
A, Address, B, Board, C, Cafe, CharLink, Company, Contact, Content, D,
|
||||
Developer, Guild, HasLinkThing, Link, Node, Note, OddRelation1,
|
||||
OddRelation2, Organization, Person, Place, Related, Restaurant, Tag, Team,
|
||||
A,
|
||||
Address,
|
||||
B,
|
||||
Board,
|
||||
C,
|
||||
Cafe,
|
||||
CharLink,
|
||||
Company,
|
||||
Contact,
|
||||
Content,
|
||||
D,
|
||||
Developer,
|
||||
Guild,
|
||||
HasLinkThing,
|
||||
Link,
|
||||
Node,
|
||||
Note,
|
||||
OddRelation1,
|
||||
OddRelation2,
|
||||
Organization,
|
||||
Person,
|
||||
Place,
|
||||
Related,
|
||||
Restaurant,
|
||||
Tag,
|
||||
Team,
|
||||
TextLink,
|
||||
)
|
||||
|
||||
|
||||
class GenericRelationTests(TestCase):
|
||||
|
||||
def test_inherited_models_content_type(self):
|
||||
"""
|
||||
GenericRelations on inherited classes use the correct content type.
|
||||
@@ -29,34 +51,38 @@ class GenericRelationTests(TestCase):
|
||||
The correct column name is used for the primary key on the
|
||||
originating model of a query. See #12664.
|
||||
"""
|
||||
p = Person.objects.create(account=23, name='Chef')
|
||||
Address.objects.create(street='123 Anywhere Place',
|
||||
city='Conifer', state='CO',
|
||||
zipcode='80433', content_object=p)
|
||||
p = Person.objects.create(account=23, name="Chef")
|
||||
Address.objects.create(
|
||||
street="123 Anywhere Place",
|
||||
city="Conifer",
|
||||
state="CO",
|
||||
zipcode="80433",
|
||||
content_object=p,
|
||||
)
|
||||
|
||||
qs = Person.objects.filter(addresses__zipcode='80433')
|
||||
qs = Person.objects.filter(addresses__zipcode="80433")
|
||||
self.assertEqual(1, qs.count())
|
||||
self.assertEqual('Chef', qs[0].name)
|
||||
self.assertEqual("Chef", qs[0].name)
|
||||
|
||||
def test_charlink_delete(self):
|
||||
oddrel = OddRelation1.objects.create(name='clink')
|
||||
oddrel = OddRelation1.objects.create(name="clink")
|
||||
CharLink.objects.create(content_object=oddrel)
|
||||
oddrel.delete()
|
||||
|
||||
def test_textlink_delete(self):
|
||||
oddrel = OddRelation2.objects.create(name='tlink')
|
||||
oddrel = OddRelation2.objects.create(name="tlink")
|
||||
TextLink.objects.create(content_object=oddrel)
|
||||
oddrel.delete()
|
||||
|
||||
def test_coerce_object_id_remote_field_cache_persistence(self):
|
||||
restaurant = Restaurant.objects.create()
|
||||
CharLink.objects.create(content_object=restaurant)
|
||||
charlink = CharLink.objects.latest('pk')
|
||||
charlink = CharLink.objects.latest("pk")
|
||||
self.assertIs(charlink.content_object, charlink.content_object)
|
||||
# If the model (Cafe) uses more than one level of multi-table inheritance.
|
||||
cafe = Cafe.objects.create()
|
||||
CharLink.objects.create(content_object=cafe)
|
||||
charlink = CharLink.objects.latest('pk')
|
||||
charlink = CharLink.objects.latest("pk")
|
||||
self.assertIs(charlink.content_object, charlink.content_object)
|
||||
|
||||
def test_q_object_or(self):
|
||||
@@ -72,26 +98,27 @@ class GenericRelationTests(TestCase):
|
||||
"""
|
||||
note_contact = Contact.objects.create()
|
||||
org_contact = Contact.objects.create()
|
||||
Note.objects.create(note='note', content_object=note_contact)
|
||||
org = Organization.objects.create(name='org name')
|
||||
Note.objects.create(note="note", content_object=note_contact)
|
||||
org = Organization.objects.create(name="org name")
|
||||
org.contacts.add(org_contact)
|
||||
# search with a non-matching note and a matching org name
|
||||
qs = Contact.objects.filter(Q(notes__note__icontains=r'other note') |
|
||||
Q(organizations__name__icontains=r'org name'))
|
||||
qs = Contact.objects.filter(
|
||||
Q(notes__note__icontains=r"other note")
|
||||
| Q(organizations__name__icontains=r"org name")
|
||||
)
|
||||
self.assertIn(org_contact, qs)
|
||||
# search again, with the same query parameters, in reverse order
|
||||
qs = Contact.objects.filter(
|
||||
Q(organizations__name__icontains=r'org name') |
|
||||
Q(notes__note__icontains=r'other note'))
|
||||
Q(organizations__name__icontains=r"org name")
|
||||
| Q(notes__note__icontains=r"other note")
|
||||
)
|
||||
self.assertIn(org_contact, qs)
|
||||
|
||||
def test_join_reuse(self):
|
||||
qs = Person.objects.filter(
|
||||
addresses__street='foo'
|
||||
).filter(
|
||||
addresses__street='bar'
|
||||
qs = Person.objects.filter(addresses__street="foo").filter(
|
||||
addresses__street="bar"
|
||||
)
|
||||
self.assertEqual(str(qs.query).count('JOIN'), 2)
|
||||
self.assertEqual(str(qs.query).count("JOIN"), 2)
|
||||
|
||||
def test_generic_relation_ordering(self):
|
||||
"""
|
||||
@@ -104,7 +131,7 @@ class GenericRelationTests(TestCase):
|
||||
Link.objects.create(content_object=p1)
|
||||
Link.objects.create(content_object=c)
|
||||
|
||||
places = list(Place.objects.order_by('links__id'))
|
||||
places = list(Place.objects.order_by("links__id"))
|
||||
|
||||
def count_places(place):
|
||||
return len([p for p in places if p.id == place.id])
|
||||
@@ -116,8 +143,8 @@ class GenericRelationTests(TestCase):
|
||||
def test_target_model_is_unsaved(self):
|
||||
"""Test related to #13085"""
|
||||
# Fails with another, ORM-level error
|
||||
dev1 = Developer(name='Joe')
|
||||
note = Note(note='Deserves promotion', content_object=dev1)
|
||||
dev1 = Developer(name="Joe")
|
||||
note = Note(note="Deserves promotion", content_object=dev1)
|
||||
with self.assertRaises(IntegrityError):
|
||||
note.save()
|
||||
|
||||
@@ -126,8 +153,8 @@ class GenericRelationTests(TestCase):
|
||||
Saving a model with a GenericForeignKey to a model instance whose
|
||||
__len__ method returns 0 (Team.__len__() here) shouldn't fail (#13085).
|
||||
"""
|
||||
team1 = Team.objects.create(name='Backend devs')
|
||||
note = Note(note='Deserve a bonus', content_object=team1)
|
||||
team1 = Team.objects.create(name="Backend devs")
|
||||
note = Note(note="Deserve a bonus", content_object=team1)
|
||||
note.save()
|
||||
|
||||
def test_target_model_bool_false(self):
|
||||
@@ -136,17 +163,17 @@ class GenericRelationTests(TestCase):
|
||||
__bool__ method returns False (Guild.__bool__() here) shouldn't fail
|
||||
(#13085).
|
||||
"""
|
||||
g1 = Guild.objects.create(name='First guild')
|
||||
note = Note(note='Note for guild', content_object=g1)
|
||||
g1 = Guild.objects.create(name="First guild")
|
||||
note = Note(note="Note for guild", content_object=g1)
|
||||
note.save()
|
||||
|
||||
@skipIfDBFeature('interprets_empty_strings_as_nulls')
|
||||
@skipIfDBFeature("interprets_empty_strings_as_nulls")
|
||||
def test_gfk_to_model_with_empty_pk(self):
|
||||
"""Test related to #13085"""
|
||||
# Saving model with GenericForeignKey to model instance with an
|
||||
# empty CharField PK
|
||||
b1 = Board.objects.create(name='')
|
||||
tag = Tag(label='VP', content_object=b1)
|
||||
b1 = Board.objects.create(name="")
|
||||
tag = Tag(label="VP", content_object=b1)
|
||||
tag.save()
|
||||
|
||||
def test_ticket_20378(self):
|
||||
@@ -160,8 +187,12 @@ class GenericRelationTests(TestCase):
|
||||
l2 = Link.objects.create(content_object=hs4)
|
||||
self.assertSequenceEqual(HasLinkThing.objects.filter(links=l1), [hs3])
|
||||
self.assertSequenceEqual(HasLinkThing.objects.filter(links=l2), [hs4])
|
||||
self.assertSequenceEqual(HasLinkThing.objects.exclude(links=l2), [hs1, hs2, hs3])
|
||||
self.assertSequenceEqual(HasLinkThing.objects.exclude(links=l1), [hs1, hs2, hs4])
|
||||
self.assertSequenceEqual(
|
||||
HasLinkThing.objects.exclude(links=l2), [hs1, hs2, hs3]
|
||||
)
|
||||
self.assertSequenceEqual(
|
||||
HasLinkThing.objects.exclude(links=l1), [hs1, hs2, hs4]
|
||||
)
|
||||
|
||||
def test_ticket_20564(self):
|
||||
b1 = B.objects.create()
|
||||
@@ -194,12 +225,16 @@ class GenericRelationTests(TestCase):
|
||||
def test_extra_join_condition(self):
|
||||
# A crude check that content_type_id is taken in account in the
|
||||
# join/subquery condition.
|
||||
self.assertIn("content_type_id", str(B.objects.exclude(a__flag=None).query).lower())
|
||||
self.assertIn(
|
||||
"content_type_id", str(B.objects.exclude(a__flag=None).query).lower()
|
||||
)
|
||||
# No need for any joins - the join from inner query can be trimmed in
|
||||
# this case (but not in the above case as no a objects at all for given
|
||||
# B would then fail).
|
||||
self.assertNotIn(" join ", str(B.objects.exclude(a__flag=True).query).lower())
|
||||
self.assertIn("content_type_id", str(B.objects.exclude(a__flag=True).query).lower())
|
||||
self.assertIn(
|
||||
"content_type_id", str(B.objects.exclude(a__flag=True).query).lower()
|
||||
)
|
||||
|
||||
def test_annotate(self):
|
||||
hs1 = HasLinkThing.objects.create()
|
||||
@@ -209,7 +244,7 @@ class GenericRelationTests(TestCase):
|
||||
Link.objects.create(content_object=hs2)
|
||||
link = Link.objects.create(content_object=hs1)
|
||||
Link.objects.create(content_object=b)
|
||||
qs = HasLinkThing.objects.annotate(Sum('links')).filter(pk=hs1.pk)
|
||||
qs = HasLinkThing.objects.annotate(Sum("links")).filter(pk=hs1.pk)
|
||||
# If content_type restriction isn't in the query's join condition,
|
||||
# then wrong results are produced here as the link to b will also match
|
||||
# (b and hs1 have equal pks).
|
||||
@@ -237,13 +272,13 @@ class GenericRelationTests(TestCase):
|
||||
self.assertSequenceEqual(HasLinkThing.objects.filter(links=link.pk), [hs2])
|
||||
|
||||
def test_editable_generic_rel(self):
|
||||
GenericRelationForm = modelform_factory(HasLinkThing, fields='__all__')
|
||||
GenericRelationForm = modelform_factory(HasLinkThing, fields="__all__")
|
||||
form = GenericRelationForm()
|
||||
self.assertIn('links', form.fields)
|
||||
form = GenericRelationForm({'links': None})
|
||||
self.assertIn("links", form.fields)
|
||||
form = GenericRelationForm({"links": None})
|
||||
self.assertTrue(form.is_valid())
|
||||
form.save()
|
||||
links = HasLinkThing._meta.get_field('links')
|
||||
links = HasLinkThing._meta.get_field("links")
|
||||
self.assertEqual(links.save_form_data_calls, 1)
|
||||
|
||||
def test_ticket_22998(self):
|
||||
@@ -257,8 +292,8 @@ class GenericRelationTests(TestCase):
|
||||
related.delete()
|
||||
|
||||
def test_ticket_22982(self):
|
||||
place = Place.objects.create(name='My Place')
|
||||
self.assertIn('GenericRelatedObjectManager', str(place.links))
|
||||
place = Place.objects.create(name="My Place")
|
||||
self.assertIn("GenericRelatedObjectManager", str(place.links))
|
||||
|
||||
def test_filter_on_related_proxy_model(self):
|
||||
place = Place.objects.create()
|
||||
@@ -270,7 +305,7 @@ class GenericRelationTests(TestCase):
|
||||
Filtering with a reverse generic relation, where the GenericRelation
|
||||
comes from multi-table inheritance.
|
||||
"""
|
||||
place = Place.objects.create(name='Test Place')
|
||||
place = Place.objects.create(name="Test Place")
|
||||
link = Link.objects.create(content_object=place)
|
||||
result = Link.objects.filter(places=place)
|
||||
self.assertCountEqual(result, [link])
|
||||
|
||||
Reference in New Issue
Block a user