-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Fix #4198 #4199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #4198 #4199
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -933,8 +933,8 @@ class TestSerializer(serializers.ModelSerializer): | |
|
||
class Meta: | ||
model = OneFieldModel | ||
read_only_fields = ('char_field', 'non_model_field') | ||
fields = read_only_fields | ||
read_only_fields = ('char_field',) | ||
fields = read_only_fields + ('non_model_field', ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this make a test or two fail because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That particular test is TestMetaInheritance which doesn't try to set the On Wed, Jun 15, 2016 at 5:35 PM, Kevin Brown notifications@github.com
George-Cristian Bîrzan |
||
extra_kwargs = {} | ||
|
||
class ChildSerializer(TestSerializer): | ||
|
@@ -955,3 +955,44 @@ class Meta(TestSerializer.Meta): | |
self.assertEqual(unicode_repr(ChildSerializer()), child_expected) | ||
self.assertEqual(unicode_repr(TestSerializer()), test_expected) | ||
self.assertEqual(unicode_repr(ChildSerializer()), child_expected) | ||
|
||
|
||
class TestDeclaredFieldsConflict(TestCase): | ||
def test_extra_kwargs_conflict(self): | ||
class TestSerializer(serializers.ModelSerializer): | ||
some_field = serializers.CharField() | ||
|
||
class Meta: | ||
model = OneFieldModel | ||
extra_kwargs = {'some_field': {'read_only': True}} | ||
with self.assertRaises(AssertionError): | ||
TestSerializer().get_fields() | ||
|
||
def test_read_only_fields_conflict(self): | ||
class TestSerializer(serializers.ModelSerializer): | ||
some_field = serializers.CharField() | ||
|
||
class Meta: | ||
model = OneFieldModel | ||
read_only_fields = ('some_field', ) | ||
with self.assertRaises(AssertionError): | ||
TestSerializer().get_fields() | ||
|
||
|
||
class TestUniquenessOverride(TestCase): | ||
def test_required_not_overwritten(self): | ||
class TestModel(models.Model): | ||
field_1 = models.IntegerField(null=True) | ||
field_2 = models.IntegerField() | ||
|
||
class Meta: | ||
unique_together = (('field_1', 'field_2'),) | ||
|
||
class TestSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = TestModel | ||
extra_kwargs = {'field_1': {'required': False}} | ||
|
||
fields = TestSerializer().fields | ||
self.assertFalse(fields['field_1'].required) | ||
self.assertTrue(fields['field_2'].required) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On thinking about this: we might need to be a bit clever about this - some folks may want to use subclassing to override a declared field. Unclear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Wed, Jun 15, 2016 at 5:56 PM, Tom Christie notifications@github.com
wrote:
George-Cristian Bîrzan
On Wed, Jun 15, 2016 at 5:56 PM, Tom Christie notifications@github.com
wrote:
George-Cristian Bîrzan
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I use this functionality all the time, to override field attributes on inherited serializers.
I would even argue to leave the current behavior to allow providing extra kwargs, that is handy for providing common kwargs to multiple fields by a simple loop, (though, it can be done by overriding init, I had preferred this one).