Skip to content

Commit 478fe0a

Browse files
author
Ryan P Kilby
committed
Test that meta fields cannot be declared as None
1 parent 7b2124b commit 478fe0a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

tests/test_serializer.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import pytest
99

10+
from django.db import models
11+
1012
from rest_framework import fields, relations, serializers
1113
from rest_framework.compat import unicode_repr
1214
from rest_framework.fields import Field
@@ -430,3 +432,25 @@ class Grandchild(Child):
430432
assert len(Parent._declared_fields) == 2
431433
assert len(Child._declared_fields) == 1
432434
assert len(Grandchild._declared_fields) == 1
435+
436+
def test_meta_field_disabling(self):
437+
# Declaratively setting a field on a child class will *not* prevent
438+
# the ModelSerializer from generating a default field.
439+
class MyModel(models.Model):
440+
f1 = models.CharField(max_length=10)
441+
f2 = models.CharField(max_length=10)
442+
443+
class Parent(serializers.ModelSerializer):
444+
class Meta:
445+
model = MyModel
446+
fields = ['f1', 'f2']
447+
448+
class Child(Parent):
449+
f1 = None
450+
451+
class Grandchild(Child):
452+
pass
453+
454+
assert len(Parent().get_fields()) == 2
455+
assert len(Child().get_fields()) == 2
456+
assert len(Grandchild().get_fields()) == 2

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy