MCPcopy
hub / github.com/django/django / loaddata

Method loaddata

django/core/management/commands/loaddata.py:140–196  ·  view source on GitHub ↗
(self, fixture_labels)

Source from the content-addressed store, hash-verified

138 cursor.execute(line)
139
140 def loaddata(self, fixture_labels):
141 connection = connections[self.using]
142
143 # Keep a count of the installed objects and fixtures
144 self.fixture_count = 0
145 self.loaded_object_count = 0
146 self.fixture_object_count = 0
147 self.models = set()
148
149 self.serialization_formats = serializers.get_public_serializer_formats()
150
151 # Django's test suite repeatedly tries to load initial_data fixtures
152 # from apps that don't have any fixtures. Because disabling constraint
153 # checks can be expensive on some database (especially MSSQL), bail
154 # out early if no fixtures are found.
155 for fixture_label in fixture_labels:
156 if self.find_fixtures(fixture_label):
157 break
158 else:
159 return
160
161 self.objs_with_deferred_fields = []
162 with connection.constraint_checks_disabled():
163 for fixture_label in fixture_labels:
164 self.load_label(fixture_label)
165 for obj in self.objs_with_deferred_fields:
166 obj.save_deferred_fields(using=self.using)
167
168 # Since we disabled constraint checks, we must manually check for
169 # any invalid keys that might have been added
170 table_names = [model._meta.db_table for model in self.models]
171 try:
172 connection.check_constraints(table_names=table_names)
173 except Exception as e:
174 e.args = ("Problem installing fixtures: %s" % e,)
175 raise
176
177 # If we found even one object in a fixture, we need to reset the
178 # database sequences.
179 if self.loaded_object_count > 0:
180 self.reset_sequences(connection, self.models)
181
182 if self.verbosity >= 1:
183 if self.fixture_object_count == self.loaded_object_count:
184 self.stdout.write(
185 "Installed %d object(s) from %d fixture(s)"
186 % (self.loaded_object_count, self.fixture_count)
187 )
188 else:
189 self.stdout.write(
190 "Installed %d object(s) (of %d) from %d fixture(s)"
191 % (
192 self.loaded_object_count,
193 self.fixture_object_count,
194 self.fixture_count,
195 )
196 )
197

Callers 1

handleMethod · 0.95

Calls 7

find_fixturesMethod · 0.95
load_labelMethod · 0.95
reset_sequencesMethod · 0.95
save_deferred_fieldsMethod · 0.80
check_constraintsMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected