Skip to content

Commit 6f21dc7

Browse files
authored
Not require explicitly set ordering in DjangoConnectionField (#1518)
* Revert "feat!: check django model has a default ordering when used in a relay connection (#1495)" This reverts commit 96c09ac. * Fix assert no warning for pytest>=8
1 parent ea45de0 commit 6f21dc7

File tree

5 files changed

+3
-59
lines changed

5 files changed

+3
-59
lines changed

examples/starwars/models.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ def __str__(self):
2424

2525

2626
class Ship(models.Model):
27-
class Meta:
28-
ordering = ["pk"]
29-
3027
name = models.CharField(max_length=50)
3128
faction = models.ForeignKey(Faction, on_delete=models.CASCADE, related_name="ships")
3229

graphene_django/fields.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,13 @@ def type(self):
101101
non_null = True
102102
assert issubclass(
103103
_type, DjangoObjectType
104-
), "DjangoConnectionField only accepts DjangoObjectType types as underlying type"
104+
), "DjangoConnectionField only accepts DjangoObjectType types"
105105
assert _type._meta.connection, "The type {} doesn't have a connection".format(
106106
_type.__name__
107107
)
108108
connection_type = _type._meta.connection
109109
if non_null:
110110
return NonNull(connection_type)
111-
# Since Relay Connections require to have a predictible ordering for pagination,
112-
# check on init that the Django model provided has a default ordering declared.
113-
model = connection_type._meta.node._meta.model
114-
assert (
115-
len(getattr(model._meta, "ordering", [])) > 0
116-
), f"Django model {model._meta.app_label}.{model.__name__} has to have a default ordering to be used in a Connection."
117111
return connection_type
118112

119113
@property

graphene_django/filter/tests/conftest.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626

2727

2828
class Event(models.Model):
29-
class Meta:
30-
ordering = ["pk"]
31-
3229
name = models.CharField(max_length=50)
3330
tags = ArrayField(models.CharField(max_length=50))
3431
tag_ids = ArrayField(models.IntegerField())

graphene_django/tests/models.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,13 @@
55

66

77
class Person(models.Model):
8-
class Meta:
9-
ordering = ["pk"]
10-
118
name = models.CharField(max_length=30)
129
parent = models.ForeignKey(
1310
"self", on_delete=models.CASCADE, null=True, blank=True, related_name="children"
1411
)
1512

1613

1714
class Pet(models.Model):
18-
class Meta:
19-
ordering = ["pk"]
20-
2115
name = models.CharField(max_length=30)
2216
age = models.PositiveIntegerField()
2317
owner = models.ForeignKey(
@@ -37,9 +31,6 @@ class FilmDetails(models.Model):
3731

3832

3933
class Film(models.Model):
40-
class Meta:
41-
ordering = ["pk"]
42-
4334
genre = models.CharField(
4435
max_length=2,
4536
help_text="Genre",
@@ -55,9 +46,6 @@ def get_queryset(self):
5546

5647

5748
class Reporter(models.Model):
58-
class Meta:
59-
ordering = ["pk"]
60-
6149
first_name = models.CharField(max_length=30)
6250
last_name = models.CharField(max_length=30)
6351
email = models.EmailField()

graphene_django/tests/test_fields.py

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
import re
33

44
import pytest
5-
from django.db.models import Count, Model, Prefetch
5+
from django.db.models import Count, Prefetch
66

77
from graphene import List, NonNull, ObjectType, Schema, String
8-
from graphene.relay import Node
98

10-
from ..fields import DjangoConnectionField, DjangoListField
9+
from ..fields import DjangoListField
1110
from ..types import DjangoObjectType
1211
from .models import (
1312
Article as ArticleModel,
@@ -717,34 +716,3 @@ def resolve_articles(root, info):
717716
r'SELECT .* FROM "tests_film" INNER JOIN "tests_film_reporters" .* LEFT OUTER JOIN "tests_filmdetails"',
718717
captured.captured_queries[1]["sql"],
719718
)
720-
721-
722-
class TestDjangoConnectionField:
723-
def test_model_ordering_assertion(self):
724-
class Chaos(Model):
725-
class Meta:
726-
app_label = "test"
727-
728-
class ChaosType(DjangoObjectType):
729-
class Meta:
730-
model = Chaos
731-
interfaces = (Node,)
732-
733-
class Query(ObjectType):
734-
chaos = DjangoConnectionField(ChaosType)
735-
736-
with pytest.raises(
737-
TypeError,
738-
match=r"Django model test\.Chaos has to have a default ordering to be used in a Connection\.",
739-
):
740-
Schema(query=Query)
741-
742-
def test_only_django_object_types(self):
743-
class Query(ObjectType):
744-
something = DjangoConnectionField(String)
745-
746-
with pytest.raises(
747-
TypeError,
748-
match="DjangoConnectionField only accepts DjangoObjectType types as underlying type",
749-
):
750-
Schema(query=Query)

0 commit comments

Comments
 (0)