147 | | label_found = False |
148 | | for combo in product([using, None], formats, compression_formats): |
149 | | database, format, compression_format = combo |
150 | | file_name = '.'.join( |
151 | | p for p in [ |
152 | | fixture_name, database, format, compression_format |
153 | | ] |
154 | | if p |
155 | | ) |
| 148 | label_found = False |
| 149 | for combo in product([using, None], formats, compression_formats): |
| 150 | database, format, compression_format = combo |
| 151 | file_name = '.'.join( |
| 152 | p for p in [ |
| 153 | fixture_name, database, format, compression_format |
| 154 | ] |
| 155 | if p |
| 156 | ) |
157 | | if verbosity >= 3: |
158 | | self.stdout.write("Trying %s for %s fixture '%s'...\n" % \ |
159 | | (humanize(fixture_dir), file_name, fixture_name)) |
160 | | full_path = os.path.join(fixture_dir, file_name) |
161 | | open_method = compression_types[compression_format] |
162 | | try: |
163 | | fixture = open_method(full_path, 'r') |
164 | | except IOError: |
165 | | if verbosity >= 2: |
166 | | self.stdout.write("No %s fixture '%s' in %s.\n" % \ |
167 | | (format, fixture_name, humanize(fixture_dir))) |
168 | | else: |
169 | | if label_found: |
170 | | fixture.close() |
171 | | self.stderr.write(self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting.\n" % |
172 | | (fixture_name, humanize(fixture_dir)))) |
173 | | if commit: |
174 | | transaction.rollback(using=using) |
175 | | transaction.leave_transaction_management(using=using) |
176 | | return |
| 158 | if verbosity >= 3: |
| 159 | self.stdout.write("Trying %s for %s fixture '%s'...\n" % \ |
| 160 | (humanize(fixture_dir), file_name, fixture_name)) |
| 161 | full_path = os.path.join(fixture_dir, file_name) |
| 162 | open_method = compression_types[compression_format] |
| 163 | try: |
| 164 | fixture = open_method(full_path, 'r') |
| 165 | except IOError: |
| 166 | if verbosity >= 2: |
| 167 | self.stdout.write("No %s fixture '%s' in %s.\n" % \ |
| 168 | (format, fixture_name, humanize(fixture_dir))) |
178 | | fixture_count += 1 |
179 | | objects_in_fixture = 0 |
180 | | loaded_objects_in_fixture = 0 |
181 | | if verbosity >= 2: |
182 | | self.stdout.write("Installing %s fixture '%s' from %s.\n" % \ |
183 | | (format, fixture_name, humanize(fixture_dir))) |
184 | | try: |
185 | | objects = serializers.deserialize(format, fixture, using=using) |
| 170 | if label_found: |
| 171 | fixture.close() |
| 172 | self.stderr.write(self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting.\n" % |
| 173 | (fixture_name, humanize(fixture_dir)))) |
| 174 | if commit: |
| 175 | transaction.rollback(using=using) |
| 176 | transaction.leave_transaction_management(using=using) |
| 177 | return |
| 178 | else: |
| 179 | fixture_count += 1 |
| 180 | objects_in_fixture = 0 |
| 181 | loaded_objects_in_fixture = 0 |
| 182 | if verbosity >= 2: |
| 183 | self.stdout.write("Installing %s fixture '%s' from %s.\n" % \ |
| 184 | (format, fixture_name, humanize(fixture_dir))) |
| 185 | try: |
| 186 | objects = serializers.deserialize(format, fixture, using=using) |
209 | | loaded_object_count += loaded_objects_in_fixture |
210 | | fixture_object_count += objects_in_fixture |
211 | | label_found = True |
212 | | except (SystemExit, KeyboardInterrupt): |
213 | | raise |
214 | | except Exception: |
| 205 | loaded_object_count += loaded_objects_in_fixture |
| 206 | fixture_object_count += objects_in_fixture |
| 207 | label_found = True |
| 208 | except (SystemExit, KeyboardInterrupt): |
| 209 | raise |
| 210 | except Exception: |
| 211 | fixture.close() |
| 212 | if commit: |
| 213 | transaction.rollback(using=using) |
| 214 | transaction.leave_transaction_management(using=using) |
| 215 | if show_traceback: |
| 216 | traceback.print_exc() |
| 217 | else: |
| 218 | self.stderr.write( |
| 219 | self.style.ERROR("Problem installing fixture '%s': %s\n" % |
| 220 | (full_path, ''.join(traceback.format_exception(sys.exc_type, |
| 221 | sys.exc_value, sys.exc_traceback))))) |
| 222 | return |
229 | | # If the fixture we loaded contains 0 objects, assume that an |
230 | | # error was encountered during fixture loading. |
231 | | if objects_in_fixture == 0: |
232 | | self.stderr.write( |
233 | | self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)\n" % |
234 | | (fixture_name))) |
235 | | if commit: |
236 | | transaction.rollback(using=using) |
237 | | transaction.leave_transaction_management(using=using) |
238 | | return |
| 236 | # Since we disabled constraint checks, we must manually check for |
| 237 | # any invalid keys that might have been added |
| 238 | table_names = [model._meta.db_table for model in models] |
| 239 | connection.check_constraints(table_names=table_names) |