All Projects → klen → Mixer

klen / Mixer

Licence: other
Mixer -- Is a fixtures replacement. Supported Django, Flask, SqlAlchemy and custom python objects.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Mixer

Enferno
A Python framework based on Flask microframework, with batteries included, and best practices in mind.
Stars: ✭ 385 (-48.18%)
Mutual labels:  sqlalchemy, flask
Flask Restplus Boilerplate
A boilerplate for flask restful web service
Stars: ✭ 466 (-37.28%)
Mutual labels:  sqlalchemy, flask
Data Driven Web Apps With Flask
Course demo code and other hand-out materials for our data-driven web apps in Flask course
Stars: ✭ 388 (-47.78%)
Mutual labels:  sqlalchemy, flask
Architect
A set of tools which enhances ORMs written in Python with more features
Stars: ✭ 320 (-56.93%)
Mutual labels:  sqlalchemy, django
Pygmy
An open-source, feature rich & extensible url-shortener + analytics written in Python 🍪
Stars: ✭ 569 (-23.42%)
Mutual labels:  django, flask
Flask Sqlalchemy
Adds SQLAlchemy support to Flask
Stars: ✭ 3,658 (+392.33%)
Mutual labels:  sqlalchemy, flask
Full Stack
Full stack, modern web application generator. Using Flask, PostgreSQL DB, Docker, Swagger, automatic HTTPS and more.
Stars: ✭ 451 (-39.3%)
Mutual labels:  sqlalchemy, flask
Flask Sqlacodegen
🍶 Automatic model code generator for SQLAlchemy with Flask support
Stars: ✭ 283 (-61.91%)
Mutual labels:  sqlalchemy, flask
Flask Rest Jsonapi
Flask extension to build REST APIs around JSONAPI 1.0 specification.
Stars: ✭ 566 (-23.82%)
Mutual labels:  sqlalchemy, flask
Minimal Django
A lightweight Django project - because Django can be nearly as simple as a microframework
Stars: ✭ 490 (-34.05%)
Mutual labels:  django, flask
Apm Agent Python
Official Python agent for the Elastic APM
Stars: ✭ 301 (-59.49%)
Mutual labels:  django, flask
Chartkick.py
Create beautiful Javascript charts with minimal code
Stars: ✭ 695 (-6.46%)
Mutual labels:  django, flask
Turkce Python Kaynaklari
Türkçe olarak hazırlanmış Python programlama dili ile ilgili içeriklerin derlendiği sayfa.
Stars: ✭ 295 (-60.3%)
Mutual labels:  django, flask
Django Dynamic Fixture
A complete library to create dynamic model instances for testing purposes.
Stars: ✭ 341 (-54.1%)
Mutual labels:  django, testing-tools
Python Articles
Monthly Series - Top 10 Python Articles
Stars: ✭ 288 (-61.24%)
Mutual labels:  django, flask
Mini Shop Server
基于 Flask 框架开发的微信小程序后端项目,用于构建小程序商城后台 (电商相关;rbac权限管理;附带自动生成Swagger 风格的API 文档;可作「Python 项目毕设」;慕课网系列)---- 相关博客链接:🌟
Stars: ✭ 446 (-39.97%)
Mutual labels:  sqlalchemy, flask
Zappa
Serverless Python
Stars: ✭ 224 (-69.85%)
Mutual labels:  django, flask
Admin Dashboards
Admin Dashboards - Open-Source and Free | AppSeed
Stars: ✭ 275 (-62.99%)
Mutual labels:  django, flask
Potion
Flask-Potion is a RESTful API framework for Flask and SQLAlchemy, Peewee or MongoEngine
Stars: ✭ 484 (-34.86%)
Mutual labels:  sqlalchemy, flask
Python24
网上搜集的自学python语言的资料集合,包括整套代码和讲义集合,这是至今为止所开放网上能够查找到的最新视频教程,网上找不到其他最新的python整套视频了,. 具体的无加密的mp4视频教程和讲义集合可以在更新的Readme文件中找到,下载直接打开就能播放,项目从零基础的Python教程到深度学习,总共30章节,其中包含Python基础中的飞机大战项目,WSGI项目,Flask新经资讯项目, Django的电商项目(本应该的美多商城项目因为使用的是Vue技术,所以替换为Django天天生鲜项目)等等,希望能够帮助大家。资源搜集劳神费力,能帮到你的话是我的福分,望大家多多支持,喜欢本仓库的话,记得Star哦。
Stars: ✭ 650 (-12.52%)
Mutual labels:  django, flask

.. image:: https://raw.github.com/klen/mixer/develop/docs/_static/logo.png :width: 100px

The Mixer is a helper to generate instances of Django or SQLAlchemy models. It's useful for testing and fixture replacement. Fast and convenient test-data generation.

Mixer supports:

  • Django_;
  • SQLAlchemy_;
  • Flask-SQLAlchemy_;
  • Peewee_;
  • Pony_;
  • Mongoengine_;
  • Marshmallow_;
  • Custom schemes;

.. _badges:

.. image:: https://github.com/klen/mixer/workflows/tests/badge.svg?style=flat-square :target: https://github.com/klen/mixer/actions :alt: Tests Status

.. image:: http://img.shields.io/pypi/v/mixer.svg?style=flat-square :target: https://pypi.python.org/pypi/mixer :alt: Version

.. image:: http://img.shields.io/pypi/dm/mixer.svg?style=flat-square :target: https://pypi.python.org/pypi/mixer :alt: Downloads

.. image:: http://img.shields.io/pypi/l/mixer.svg?style=flat-square :target: https://pypi.python.org/pypi/mixer :alt: License

.. _documentation:

Docs are available at https://mixer.readthedocs.org/. Pull requests with documentation enhancements and/or fixes are awesome and most welcome.

Описание на русском языке: http://klen.github.io/mixer.html

.. important::

From version 6.2 the Mixer library doesn't support Python 2. The latest version with python<3 support is mixer 6.1.3

.. _contents:

.. contents::

Requirements

  • Python 3.7+
  • Django (3.0, 3.1) for Django ORM support;
  • Flask-SQLALchemy for SQLAlchemy ORM support and integration as Flask application;
  • Faker >= 0.7.3
  • Mongoengine for Mongoengine ODM support;
  • SQLAlchemy for SQLAlchemy ORM support;
  • Peewee ORM support;

Installation

Mixer should be installed using pip: ::

pip install mixer

Usage

| By default Mixer tries to generate fake (human-friendly) data. | If you want to randomize the generated values initialize the Mixer | by manual: Mixer(fake=False)

| By default Mixer saves the generated objects in a database. If you want to disable | this, initialize the Mixer by manual like Mixer(commit=False)

Django workflow

Quick example:

.. code-block:: python

from mixer.backend.django import mixer
from customapp.models import User, UserMessage

# Generate a random user
user = mixer.blend(User)

# Generate an UserMessage
message = mixer.blend(UserMessage, user=user)

# Generate an UserMessage and an User. Set username for generated user to 'testname'.
message = mixer.blend(UserMessage, user__username='testname')

# Generate SomeModel from SomeApp and select FK or M2M values from db
some = mixer.blend('someapp.somemodel', somerelation=mixer.SELECT)

# Generate SomeModel from SomeApp and force a value of money field from default to random
some = mixer.blend('someapp.somemodel', money=mixer.RANDOM)

# Generate 5 SomeModel's instances and take company field's values from custom generator
some_models = mixer.cycle(5).blend('somemodel', company=(name for name in company_names))

Flask, Flask-SQLAlchemy

Quick example:

.. code-block:: python

from mixer.backend.flask import mixer
from models import User, UserMessage

mixer.init_app(self.app)

# Generate a random user
user = mixer.blend(User)

# Generate an userMessage
message = mixer.blend(UserMessage, user=user)

# Generate an UserMessage and an User. Set username for generated user to 'testname'.
message = mixer.blend(UserMessage, user__username='testname')

# Generate SomeModel and select FK or M2M values from db
some = mixer.blend('project.models.SomeModel', somerelation=mixer.SELECT)

# Generate SomeModel from SomeApp and force a value of money field from default to random
some = mixer.blend('project.models.SomeModel', money=mixer.RANDOM)

# Generate 5 SomeModel's instances and take company field's values from custom generator
some_models = mixer.cycle(5).blend('project.models.SomeModel', company=(company for company in companies))

Support for Flask-SQLAlchemy models that have __init__ arguments ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For support this scheme, just create your own mixer class, like this:

.. code-block:: python

from mixer.backend.sqlalchemy import Mixer

class MyOwnMixer(Mixer):

    def populate_target(self, values):
        target = self.__scheme(**values)
        return target

mixer = MyOwnMixer()

SQLAlchemy workflow

Example of initialization:

.. code-block:: python

from mixer.backend.sqlalchemy import Mixer

ENGINE = create_engine('sqlite:///:memory:')
BASE = declarative_base()
SESSION = sessionmaker(bind=ENGINE)

mixer = Mixer(session=SESSION(), commit=True)
role = mixer.blend('package.models.Role')

Also, see Flask, Flask-SQLAlchemy.

Mongoengine workflow

Example usage:

.. code-block:: python

from mixer.backend.mongoengine import mixer

class User(Document):
    created_at = DateTimeField(default=datetime.datetime.now)
    email = EmailField(required=True)
    first_name = StringField(max_length=50)
    last_name = StringField(max_length=50)
    username = StringField(max_length=50)

class Post(Document):
    title = StringField(max_length=120, required=True)
    author = ReferenceField(User)
    tags = ListField(StringField(max_length=30))

post = mixer.blend(Post, author__username='foo')

Marshmallow workflow

Example usage:

.. code-block:: python

from mixer.backend.marshmallow import mixer
import marshmallow as ma

class User(ma.Schema):
    created_at = ma.fields.DateTime(required=True)
    email = ma.fields.Email(required=True)
    first_name = ma.fields.String(required=True)
    last_name = ma.fields.String(required=True)
    username = ma.fields.String(required=True)

class Post(ma.Schema):
    title = ma.fields.String(required=True)
    author = ma.fields.Nested(User, required=True)

post = mixer.blend(Post, author__username='foo')

Common usage

Quick example:

.. code-block:: python

from mixer.main import mixer

class Test:
    one = int
    two = int
    name = str

class Scheme:
    name = str
    money = int
    male = bool
    prop = Test

scheme = mixer.blend(Scheme, prop__one=1)

DB commits

By default 'django', 'flask', 'mongoengine' backends tries to save objects in database. For preventing this behavior init mixer manually:

.. code-block:: python

from mixer.backend.django import Mixer

mixer = Mixer(commit=False)

Or you can temporary switch context use the mixer as context manager:

.. code-block:: python

from mixer.backend.django import mixer

# Will be save to db
user1 = mixer.blend('auth.user')

# Will not be save to db
with mixer.ctx(commit=False):
    user2 = mixer.blend('auth.user')

.. _custom:

Custom fields

The mixer allows you to define generators for fields by manually. Quick example:

.. code-block:: python

    from mixer.main import mixer

    class Test:
        id = int
        name = str

    mixer.register(Test,
        name=lambda: 'John',
        id=lambda: str(mixer.faker.small_positive_integer())
    )

    test = mixer.blend(Test)
    test.name == 'John'
    isinstance(test.id, str)

    # You could pinned just a value to field
    mixer.register(Test, name='Just John')
    test = mixer.blend(Test)
    test.name == 'Just John'

Also, you can make your own factory for field types:

.. code-block:: python

from mixer.backend.django import Mixer, GenFactory

def get_func(*args, **kwargs):
    return "Always same"

class MyFactory(GenFactory):
    generators = {
        models.CharField: get_func
    }

mixer = Mixer(factory=MyFactory)

Middlewares

You can add middleware layers to process generation:

.. code-block:: python

from mixer.backend.django import mixer

# Register middleware to model
@mixer.middleware('auth.user')
def encrypt_password(user):
    user.set_password('test')
    return user

You can add several middlewares. Each middleware should get one argument (generated value) and return them.

It's also possible to unregister a middleware:

.. code-block:: python

mixer.unregister_middleware(encrypt_password)

Locales

By default mixer uses 'en' locale. You could switch mixer default locale by creating your own mixer:

.. code-block:: python

from mixer.backend.django import Mixer

mixer = Mixer(locale='it')
mixer.faker.name()          ## u'Acchisio Conte'

At any time you could switch mixer current locale:

.. code-block:: python

mixer.faker.locale = 'cz'
mixer.faker.name()          ## u'Miloslava Urbanov\xe1 CSc.'

mixer.faker.locale = 'en'
mixer.faker.name()          ## u'John Black'

# Use the mixer context manager
mixer.faker.phone()         ## u'1-438-238-1116'
with mixer.ctx(locale='fr'):
    mixer.faker.phone()     ## u'08 64 92 11 79'

mixer.faker.phone()         ## u'1-438-238-1116'

.. _bugtracker:

Bug tracker

If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/mixer/issues

Contributing

Development of mixer happens at Github: https://github.com/klen/mixer

Contributors

License

Licensed under a BSD license_.

.. _links:

.. _Django: http://djangoproject.com/ .. _Flask: https://flask.palletsprojects.com/en/1.1.x/ .. _Flask-SQLAlchemy: http://flask-sqlalchemy.pocoo.org/ .. _SQLAlchemy: http://www.sqlalchemy.org/ .. _Marshmallow: http://marshmallow.readthedocs.io/en/latest/ .. _Mongoengine: http://mongoengine.org/ .. _Peewee: http://peewee.readthedocs.org/en/latest/ .. _Pony: http://ponyorm.com/ .. _klen: http://klen.github.io .. _BSD license: http://www.linfo.org/bsdlicense.html

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].