| 1 | from django.core import serializers
|
|---|
| 2 | from django.test import TestCase
|
|---|
| 3 | from django.contrib.auth.models import User
|
|---|
| 4 | import json
|
|---|
| 5 |
|
|---|
| 6 | class Subuser(User):
|
|---|
| 7 | pass
|
|---|
| 8 |
|
|---|
| 9 | class SerializeSubModelTest(TestCase):
|
|---|
| 10 |
|
|---|
| 11 | def setUp(self):
|
|---|
| 12 | Subuser.objects.create(username="sina")
|
|---|
| 13 |
|
|---|
| 14 | def test_serializer(self):
|
|---|
| 15 | object_list = Subuser.objects.all()
|
|---|
| 16 | jsonstr = serializers.serialize('json', object_list, ensure_ascii=False)
|
|---|
| 17 | print(jsonstr) # got '[{"pk": 1, "model": "tta.subuser", "fields": {"user_permissions": [], "groups": []}}]'
|
|---|
| 18 | data = json.loads(jsonstr)
|
|---|
| 19 | self.assertEqual('sina', data[0]['fields']['username'])
|
|---|