コード例 #1
0
ファイル: populator.py プロジェクト: doubleo2/django-faker
    def guessFieldFormatters(self, generator, formatters=None):
        if formatters is None:
            formatters = {}
        model = self.model
        nameGuesser = Name(generator)
        fieldTypeGuesser = FieldTypeGuesser(generator)

        for field in model._meta.fields:
        #            yield field.name, getattr(self, field.name)
            fieldName = field.name

            if fieldName in formatters:
                continue

            if isinstance(field, (ForeignKey, ManyToManyField, OneToOneField)):
                rel_populator = RelationPopulator(field)

                formatters[fieldName] = rel_populator.build_relation
                continue

            if isinstance(field, AutoField):
                continue

            formatter = nameGuesser.guessFormat(fieldName)
            if formatter:
                formatters[fieldName] = formatter
                continue

            formatter = fieldTypeGuesser.guessFormat(field)
            if formatter:
                formatters[fieldName] = formatter
                continue

        return formatters
コード例 #2
0
    def guessFieldFormatters(self, generator):

        formatters = {}
        model = self.model
        nameGuesser = Name(generator)
        fieldTypeGuesser = FieldTypeGuesser(generator)

        for field in model._meta.fields:
            # yield field.name, getattr(self, field.name)
            fieldName = field.name
            if isinstance(field, (ForeignKey, ManyToManyField, OneToOneField)):
                relatedModel = field.rel.to

                def build_relation(inserted):

                    if relatedModel in inserted and inserted[relatedModel]:
                        return relatedModel.objects.get(
                            pk=random.choice(inserted[relatedModel]))
                    if not field.null:
                        try:
                            # try to retrieve random object from relatedModel
                            relatedModel.objects.order_by('?')[0]
                        except IndexError:
                            raise Exception(
                                'Relation "%s.%s" with "%s" cannot be null, check order of addEntity list'
                                % (
                                    field.model.__name__,
                                    field.name,
                                    relatedModel.__name__,
                                ))
                    return None

                formatters[fieldName] = build_relation

                continue

            if isinstance(field, AutoField):
                continue

            formatter = nameGuesser.guessFormat(fieldName)
            if formatter:
                formatters[fieldName] = formatter
                continue

            formatter = fieldTypeGuesser.guessFormat(field)
            if formatter:
                formatters[fieldName] = formatter
                continue

        return formatters
コード例 #3
0
ファイル: populator.py プロジェクト: kriestof/django-faker
    def guessFieldFormatters(self, generator, formatters=None):
        if formatters is None:
            formatters = {}
        model = self.model
        nameGuesser = Name(generator)
        fieldTypeGuesser = FieldTypeGuesser(generator)

        for field in model._meta.fields:
            #            yield field.name, getattr(self, field.name)
            fieldName = field.name

            if formatters.has_key(fieldName):
                continue

            if isinstance(field, (ForeignKey, ManyToManyField, OneToOneField)):
                relatedModel = field.rel.to

                def build_relation(inserted):
                    if relatedModel in inserted and inserted[relatedModel]:
                        return relatedModel.objects.get(pk=random.choice(inserted[relatedModel]))
                    if not field.null:
                        try:
                            # try to retrieve random object from relatedModel
                            relatedModel.objects.order_by("?")[0]
                        except IndexError:
                            raise Exception(
                                'Relation "%s.%s" with "%s" cannot be null, check order of addEntity list'
                                % (field.model.__name__, field.name, relatedModel.__name__)
                            )
                    return None

                formatters[fieldName] = build_relation
                continue

            if isinstance(field, AutoField):
                continue

            formatter = nameGuesser.guessFormat(fieldName)
            if formatter:
                formatters[fieldName] = formatter
                continue

            formatter = fieldTypeGuesser.guessFormat(field)
            if formatter:
                formatters[fieldName] = formatter
                continue

        return formatters