def __m2m_generator(model, **attrs): return make(model, _quantity=MAX_MANY_QUANTITY, **attrs) make.required = foreign_key_required prepare.required = foreign_key_required __m2m_generator.required = foreign_key_required default_mapping = { BooleanField: generators.gen_boolean, IntegerField: generators.gen_integer, BigIntegerField: generators.gen_integer, SmallIntegerField: generators.gen_integer, PositiveIntegerField: lambda: generators.gen_integer(0), PositiveSmallIntegerField: lambda: generators.gen_integer(0), FloatField: generators.gen_float, DecimalField: generators.gen_decimal, CharField: generators.gen_string, TextField: generators.gen_text, SlugField: generators.gen_slug, ForeignKey: make, OneToOneField: make, ManyToManyField: __m2m_generator, DateField: generators.gen_date, DateTimeField: generators.gen_datetime,
def __m2m_generator(model, **attrs): return make(model, _quantity=MAX_MANY_QUANTITY, **attrs) make.required = foreign_key_required prepare.required = foreign_key_required __m2m_generator.required = foreign_key_required default_mapping = { BooleanField: generators.gen_boolean, IntegerField: generators.gen_integer, BigIntegerField: generators.gen_integer, SmallIntegerField: generators.gen_integer, PositiveIntegerField: lambda: generators.gen_integer(0), PositiveSmallIntegerField: lambda: generators.gen_integer(0), FloatField: generators.gen_float, DecimalField: generators.gen_decimal, CharField: generators.gen_string, TextField: generators.gen_text, SlugField: generators.gen_slug, ForeignKey: make, OneToOneField: make, ManyToManyField: __m2m_generator, DateField: generators.gen_date, DateTimeField: generators.gen_datetime,
def generate_value(self, field): ''' Calls the generator associated with a field passing all required args. Generator Resolution Precedence Order: -- attr_mapping - mapping per attribute name -- choices -- mapping from avaiable field choices -- type_mapping - mapping from user defined type associated generators -- default_mapping - mapping from pre-defined type associated generators `attr_mapping` and `type_mapping` can be defined easely overwriting the model. ''' if field.name in self.attr_mapping: generator = self.attr_mapping[field.name] elif getattr(field, 'choices'): generator = generators.gen_from_choices(field.choices) elif isinstance(field, ForeignKey) and isinstance(field.rel.to, ContentType): generator = self.type_mapping[ContentType] elif field.__class__ in self.type_mapping: generator = self.type_mapping[field.__class__] elif isinstance(field, ManyToManyField): generator = __m2m_generator elif isinstance(field, BooleanField): generator = generators.gen_boolean elif isinstance(field, ContentType): generator = generators.gen_content_type elif isinstance(field, DateField): generator = generators.gen_date elif isinstance(field, DateTimeField): generator = generators.gen_datetime elif isinstance(field, DecimalField): generator = generators.gen_decimal elif isinstance(field, EmailField): generator = generators.gen_email elif isinstance(field, FileField): generator = generators.gen_file_field elif isinstance(field, FloatField): generator = generators.gen_float elif isinstance(field, ImageField): generator = generators.gen_image_field elif isinstance(field, (BigIntegerField, IntegerField, SmallIntegerField)): generator = generators.gen_integer elif isinstance(field, SlugField): generator = generators.gen_slug elif isinstance(field, CharField): generator = generators.gen_string elif isinstance(field, TextField): generator = generators.gen_text elif isinstance(field, TimeField): generator = generators.gen_time elif isinstance(field, URLField): generator = generators.gen_url elif isinstance(field, (PositiveIntegerField, PositiveSmallIntegerField)): generator = lambda: generators.gen_integer(0) elif isinstance(field, ForeignKey): generator = make elif isinstance(field, OneToOneField): generator = make else: raise TypeError('%s is not supported by mommy.' % field.__class__) # attributes like max_length, decimal_places are take in account when # generating the value. generator_attrs = get_required_values(generator, field) if field.name in self.rel_fields: generator_attrs.update(filter_rel_attrs(field.name, **self.rel_attrs)) return generator(**generator_attrs)