| 1 | from django.db import models
|
|---|
| 2 | from django.core import serializers
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 | class TestField(models.TextField):
|
|---|
| 6 | def get_attname(self):
|
|---|
| 7 | return '%s_attr' % self.name
|
|---|
| 8 |
|
|---|
| 9 | class TestModel(models.Model):
|
|---|
| 10 | test = TestField()
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 | TestModel(test='blah')
|
|---|
| 15 | # TypeError: 'test' is an invalid keyword argument for this function
|
|---|
| 16 |
|
|---|
| 17 | TestModel(test_attr='blah')
|
|---|
| 18 | # <TestModel: TestModel object>
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | test = TestModel(test_attr='blah')
|
|---|
| 22 | json = serializers.serialize('json', [test])
|
|---|
| 23 | # OK
|
|---|
| 24 | # json == '[{"pk": null, "model": "testapp.testmodel", "fields": {"test": "blah"}}]'
|
|---|
| 25 |
|
|---|
| 26 | [obj for obj in serializers.deserialize('json', json)]
|
|---|
| 27 | # TypeError: 'test' is an invalid keyword argument for this function
|
|---|