def _process_foreign_key(self, model_class, field, persist_dependencies): "Returns auto-generated value for a field ForeignKey or OneToOneField." if field_is_a_parent_link(field): return None next_model = get_related_model(field) occurrences = self.model_path.count(next_model) if occurrences >= self.number_of_laps: data = None else: next_model_path = self.model_path[:] next_model_path.append(model_class) if model_class == next_model: # self reference # propagate ignored_fields only for self references ignore_fields = self.ignore_fields else: ignore_fields = [] # need a new DynamicFixture to control the cycles and ignored fields. fixture = DynamicFixture(data_fixture=self.data_fixture, fill_nullable_fields=self.fill_nullable_fields, ignore_fields=ignore_fields, number_of_laps=self.number_of_laps, use_library=self.use_library, validate_models=self.validate_models, validate_args=self.validate_args, print_errors=self.print_errors, model_path=next_model_path) if persist_dependencies: data = fixture.get(next_model) else: data = fixture.new(next_model, persist_dependencies=persist_dependencies) return data
def set_data_for_a_field(self, model_class, instance, field, persist_dependencies=True, **kwargs): if field.name in kwargs: config = kwargs[field.name] try: data = self._process_field_with_customized_fixture(instance, field, config, persist_dependencies) except PendingField: return # ignore this field for a while. except Exception as e: raise InvalidConfigurationError(get_unique_field_name(field), e), None, sys.exc_info()[2] else: if field_is_a_parent_link(field): return data = self._process_field_with_default_fixture(field, model_class, persist_dependencies) if is_file_field(field) and data: django_file = data if isinstance(django_file, File): setattr(instance, field.name, data.name) # set the attribute if django_file.file.mode != 'rb': django_file.file.close() # this file may be open in another mode, for example, in a+b opened_file = open(django_file.file.name, 'rb') # to save the file it must be open in rb mode django_file.file = opened_file # we update the reference to the rb mode opened file getattr(instance, field.name).save(django_file.name, django_file) # save the file into the file storage system django_file.close() else: # string (saving just a name in the file, without saving the file to the storage file system setattr(instance, field.name, data) # Model.field = data else: if self.debug_mode: LOGGER.debug('%s.%s = %s' % (get_unique_model_name(model_class), field.name, data)) setattr(instance, field.name, data) # Model.field = data self.fields_processed.append(field.name)
def _process_foreign_key(self, model_class, field, persist_dependencies): "Returns auto-generated value for a field ForeignKey or OneToOneField." if field_is_a_parent_link(field): return None next_model = get_related_model(field) occurrences = self.model_path.count(next_model) if occurrences >= self.number_of_laps: data = None else: next_model_path = self.model_path[:] next_model_path.append(model_class) if model_class == next_model: # self reference # propagate ignored_fields only for self references ignore_fields = self.ignore_fields else: ignore_fields = [] # need a new DynamicFixture to control the cycles and ignored fields. fixture = DynamicFixture( data_fixture=self.data_fixture, fill_nullable_fields=self.fill_nullable_fields, ignore_fields=ignore_fields, number_of_laps=self.number_of_laps, use_library=self.use_library, validate_models=self.validate_models, validate_args=self.validate_args, print_errors=self.print_errors, model_path=next_model_path) if persist_dependencies: data = fixture.get(next_model) else: data = fixture.new(next_model, persist_dependencies=persist_dependencies) return data
def _process_foreign_key(self, model_class, field, persist_dependencies): ''' Returns auto-generated value for a field ForeignKey or OneToOneField. ''' if field_is_a_parent_link(field): return None next_model = get_related_model(field) # 1. Propagate ignored_fields only for self references if model_class == next_model: # self reference ignore_fields = self.ignore_fields else: ignore_fields = [] # 2. It needs a new DynamicFixture to control the cycles and ignored fields. fixture = DynamicFixture( data_fixture=self.data_fixture, fill_nullable_fields=self.fill_nullable_fields, ignore_fields=ignore_fields, fk_min_depth=self.fk_min_depth - 1, # Depth decreased validate_models=self.validate_models, print_errors=self.print_errors) # 3. Persist it if persist_dependencies: data = fixture.get(next_model) else: data = fixture.new(next_model, persist_dependencies=persist_dependencies) return data
def _process_foreign_key(self, model_class, field, persist_dependencies, locators): "Returns auto-generated value for a field ForeignKey or OneToOneField." persist_dependencies = True if field_is_a_parent_link(field): return None next_model = get_related_model(field) occurrences = self.model_path.count(next_model) if occurrences >= self.number_of_laps: data = self._close_cycle_gracefully(self.model_path, model_class, field) return data else: next_model_path = self.model_path[:] next_model_path.append(model_class) if model_class == next_model: # self reference # propagate ignored_fields only for self references ignore_fields = self.ignore_fields else: ignore_fields = [] # need a new DynamicFixture to control the cycles and ignored fields. fixture = DynamicFixture(data_fixture=self.data_fixture, fill_nullable_fields=self.fill_nullable_fields, ignore_fields=ignore_fields, number_of_laps=self.number_of_laps, use_library=self.use_library, validate_models=self.validate_models, validate_args=self.validate_args, print_errors=self.print_errors, model_path=next_model_path) if persist_dependencies: if self._in_locators(locators, field.name): # use the existing model specified by locators loc = None for i in locators: if field.name == i[0]: loc = i[1] assert(loc != None) data = get_by_locator(loc) else: data = fixture.get(next_model) else: data = fixture.new(next_model, locators, persist_dependencies=persist_dependencies) return data