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_many_to_many_field(self, field, manytomany_field, fixture): """ Set ManyToManyField fields with or without 'trough' option. @field: model field. @manytomany_field: ManyRelatedManager of the field. @fixture: value passed by user. """ next_model = get_related_model(field) if isinstance(fixture, int): amount = fixture for _ in range(amount): next_instance = self.get(next_model) try: manytomany_field.add(next_instance) except AttributeError: # M2M with trough: ManyRelatedManager next_instance.save() elif isinstance(fixture, (list, tuple)): items = fixture for item in items: if isinstance(item, DynamicFixture): next_instance = item.get(next_model, **item.kwargs) # need to pass F.kwargs recursively. else: next_instance = item try: manytomany_field.add(next_instance) except AttributeError: # M2M with trough: ManyRelatedManager next_instance.save() else: raise InvalidManyToManyConfigurationError('Field: %s' % field.name, str(fixture)), None, sys.exc_info()[2]
def _process_many_to_many_field(self, field, manytomany_field, fixture, instance): """ Set ManyToManyField fields with or without 'trough' option. :field: model field. :manytomany_field: ManyRelatedManager of the field. :fixture: value passed by user. """ next_model = get_related_model(field) if isinstance(fixture, int): amount = fixture for _ in range(amount): next_instance = self.get(next_model) self._create_manytomany_relationship(manytomany_field, instance, next_instance) elif isinstance(fixture, (list, tuple)): items = fixture for item in items: if isinstance(item, DynamicFixture): next_instance = item.get( next_model, **item.kwargs) # need to pass F.kwargs recursively. else: next_instance = item self._create_manytomany_relationship(manytomany_field, instance, next_instance) else: raise InvalidManyToManyConfigurationError('Field: %s' % field.name, str(fixture))
def _process_many_to_many_field(self, field, manytomany_field, fixture, instance): """ Set ManyToManyField fields with or without 'trough' option. :field: model field. :manytomany_field: ManyRelatedManager of the field. :fixture: value passed by user. """ next_model = get_related_model(field) if isinstance(fixture, int): amount = fixture for _ in range(amount): next_instance = self.get(next_model) self._create_manytomany_relationship(manytomany_field, instance, next_instance) elif isinstance(fixture, (list, tuple)): items = fixture for item in items: if isinstance(item, DynamicFixture): next_instance = item.get(next_model, **item.kwargs) # need to pass F.kwargs recursively. else: next_instance = item self._create_manytomany_relationship(manytomany_field, instance, next_instance) else: raise InvalidManyToManyConfigurationError('Field: %s' % field.name, str(fixture))
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 _get_data_from_custom_dynamic_fixture(self, field, fixture, persist_dependencies): "return data of a Dynamic Fixture: field=F(...)" next_model = get_related_model(field) 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
def _get_data_from_data_fixture(self, field, fixture): "return data of a Data Fixture: field=DataFixture()" next_model = get_related_model(field) return fixture.generate_data(next_model)