Skip to content

Commit fede977

Browse files
committed
Revert "feat!: check django model has a default ordering when used in a relay connection (#1495)"
This reverts commit 96c09ac.
1 parent ea45de0 commit fede977

File tree

6 files changed

+16
-69
lines changed

6 files changed

+16
-69
lines changed

Diff for: examples/starwars/models.py

-3
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

Diff for: graphene_django/fields.py

+1-7
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

Diff for: graphene_django/filter/tests/conftest.py

-3
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())

Diff for: graphene_django/tests/models.py

-12
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()

Diff for: graphene_django/tests/test_fields.py

+2-34
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)

Diff for: graphene_django/tests/test_types.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import warnings
21
from collections import OrderedDict, defaultdict
32
from textwrap import dedent
43
from unittest.mock import patch
@@ -400,16 +399,15 @@ class Meta:
400399
with pytest.warns(
401400
UserWarning,
402401
match=r"Field name .* matches an attribute on Django model .* but it's not a model field",
403-
):
402+
) as record:
404403

405404
class Reporter2(DjangoObjectType):
406405
class Meta:
407406
model = ReporterModel
408407
fields = ["first_name", "some_method", "email"]
409408

410409
# Don't warn if selecting a custom field
411-
with warnings.catch_warnings():
412-
warnings.simplefilter("error")
410+
with pytest.warns(None) as record:
413411

414412
class Reporter3(DjangoObjectType):
415413
custom_field = String()
@@ -418,6 +416,8 @@ class Meta:
418416
model = ReporterModel
419417
fields = ["first_name", "custom_field", "email"]
420418

419+
assert len(record) == 0
420+
421421

422422
@with_local_registry
423423
def test_django_objecttype_exclude_fields_exist_on_model():
@@ -445,14 +445,15 @@ class Meta:
445445
exclude = ["custom_field"]
446446

447447
# Don't warn on exclude fields
448-
with warnings.catch_warnings():
449-
warnings.simplefilter("error")
448+
with pytest.warns(None) as record:
450449

451450
class Reporter4(DjangoObjectType):
452451
class Meta:
453452
model = ReporterModel
454453
exclude = ["email", "first_name"]
455454

455+
assert len(record) == 0
456+
456457

457458
@with_local_registry
458459
def test_django_objecttype_neither_fields_nor_exclude():
@@ -466,22 +467,24 @@ class Reporter(DjangoObjectType):
466467
class Meta:
467468
model = ReporterModel
468469

469-
with warnings.catch_warnings():
470-
warnings.simplefilter("error")
470+
with pytest.warns(None) as record:
471471

472472
class Reporter2(DjangoObjectType):
473473
class Meta:
474474
model = ReporterModel
475475
fields = ["email"]
476476

477-
with warnings.catch_warnings():
478-
warnings.simplefilter("error")
477+
assert len(record) == 0
478+
479+
with pytest.warns(None) as record:
479480

480481
class Reporter3(DjangoObjectType):
481482
class Meta:
482483
model = ReporterModel
483484
exclude = ["email"]
484485

486+
assert len(record) == 0
487+
485488

486489
def custom_enum_name(field):
487490
return f"CustomEnum{field.name.title()}"

0 commit comments

Comments
 (0)