Ticket #16317: get_by_natural_key.patch
File get_by_natural_key.patch, 4.5 KB (added by , 13 years ago) |
---|
-
tests/modeltests/fixtures/fixtures/nodes.json
1 [ 2 { 3 "pk": 1, 4 "model": "fixtures.node", 5 "fields": { 6 "code": 7, 7 "name": "Tree", 8 "parent": null 9 } 10 }, 11 { 12 "pk": 2, 13 "model": "fixtures.node", 14 "fields": { 15 "code": 3, 16 "name": "Sequoia", 17 "parent": [ 18 7, 19 "Tree" 20 ] 21 } 22 }, 23 { 24 "pk": 3, 25 "model": "fixtures.node", 26 "fields": { 27 "code": 10, 28 "name": "Eucalyptus", 29 "parent": [ 30 7, 31 "Tree" 32 ] 33 } 34 }, 35 { 36 "pk": 4, 37 "model": "fixtures.node", 38 "fields": { 39 "code": 16, 40 "name": "Sequoia sempervirens", 41 "parent": [ 42 3, 43 "Sequoia" 44 ] 45 } 46 }, 47 { 48 "pk": 6, 49 "model": "fixtures.node", 50 "fields": { 51 "code": 17, 52 "name": "Picea", 53 "parent": [ 54 7, 55 "Tree" 56 ] 57 } 58 }, 59 { 60 "pk": 5, 61 "model": "fixtures.node", 62 "fields": { 63 "code": 6, 64 "name": "Picea sitchensis", 65 "parent": [ 66 17, 67 "Picea" 68 ] 69 } 70 } 71 ] -
tests/modeltests/fixtures/tests.py
7 7 from django.db import connection 8 8 from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature 9 9 10 from .models import Article, Book, Spy, Tag, Visa 10 from .models import Article, Book, Spy, Tag, Visa, Node 11 11 12 12 13 13 class TestCaseFixtureLoadingTests(TestCase): … … 354 354 '<Article: Poker has no place on ESPN>', 355 355 '<Article: Python program becomes self aware>' 356 356 ]) 357 358 class TestCaseNaturalNodeFixtureLoadingTests(TestCase): 359 fixtures = ['nodes.json',] 360 361 def testClassFixtures(self): 362 "Check that test case has installed 4 fixture objects" 363 self.assertEqual(Node.objects.count(), 6) 364 self.assertQuerysetEqual(Node.objects.all(), [ 365 '<Node: 7 (Tree)>', 366 '<Node: 3 (Sequoia)>', 367 '<Node: 10 (Eucalyptus)>', 368 '<Node: 16 (Sequoia sempervirens)>', 369 '<Node: 6 (Picea sitchensis)>', 370 '<Node: 17 (Picea)>' 371 ]) -
tests/modeltests/fixtures/models.py
97 97 98 98 class Meta: 99 99 ordering = ('name',) 100 101 102 class NodeManager(models.Manager): 103 104 def get_by_natural_key(self, code, name): 105 return self.get(code=code, name=name) 106 107 class Node(models.Model): 108 code = models.IntegerField() 109 name = models.CharField(max_length=100) 110 parent = models.ForeignKey('self', null=True) 111 112 objects = NodeManager() 113 114 def natural_key(self): 115 return (self.code, self.name) 116 117 118 def __unicode__(self): 119 return "%s (%s)" % self.natural_key() -
django/core/management/commands/dumpdata.py
146 146 for field in model._meta.fields: 147 147 if hasattr(field.rel, 'to'): 148 148 rel_model = field.rel.to 149 if hasattr(rel_model, 'natural_key') :149 if hasattr(rel_model, 'natural_key') and rel_model != model: 150 150 deps.append(rel_model) 151 151 for field in model._meta.many_to_many: 152 152 rel_model = field.rel.to 153 if hasattr(rel_model, 'natural_key') :153 if hasattr(rel_model, 'natural_key') and field.rel.to != model: 154 154 deps.append(rel_model) 155 155 model_dependencies.append((model, deps)) 156 156