Git Product home page Git Product logo

django-easy-fixture's Introduction

django-easy-fixture

python2.7, python3.5

1.8<= Django <= 1.10

install: pip install django-easy-fixture

That is a easy, simple tool to help you to fill your fixture dict with some spam datas

Make your fixture dict to be a completely available django fixture that you do not have to worry about any unqiue, unqie_together, just pk

The pk must be defined by you!

Just support all the fields defined by Django, and do not support any customization field.

Maybe support the customization field in the future.

1. get a fixture dict

# in template.py
fixtures_template={'auth.User': [{'pk': 1}]}

# in other.py
from easy_fixture.easy_fixture import EasyFixture
from template import fixtures_template

ef = EasyFixture(fixtures_template)
fixtures_dict = ef.output()

2. use as a django app command

# in template.py
fixtures_template = {'auth.User': [{'pk': 1}]}

# in your settings.py
INSTALLED_APPS = ('other apps',
                  'easy_fixture',
                  )

run make_fixture command

python manage.py make_fixture template.fixtures_template > /path/to/fixture.json

Must pass the module path and fixture variable, like module.to.fixture.Variable_name

do not support old style

python manage.py make_fixture template > /path/to/fixture.json

3. use in test

In your testCase, call EasyFixture.load_into_testcase in your setUpTestData, setUpClass, setUp or anywhere you want to load your fixture data.

FIXTURE_DICT = {'auth.User': [{'pk': 1}]}

class MyTestCase(TestCase):

    @classmethod
    def setUpTestData(cls):
        ef = EasyFixture(FIXTURE_DICT)
        ef.load_into_testcase()
        TestCase.setUpTestData()

    def tes_what_you_want(self):
        pass

deprecated

from easy_fixture.easy_fixture import FixtureFileGen

class MyCase(TestCase):
   fixtures = FixtureFileGen(['my.fixture.template.module'])

4. how to loaddata to db

Creating a temp file, and call loaddata command for now.

And, maybe we could simply

  1. disable database constraint check
  2. call serializers.deserialize to deserialize fixture datas, and save all objects
  3. check constraint after we have save all objects
  4. finally, reset database sequence
# disable database constraint check
with connection.constraint_checks_disabled():
    # save objects
    objects = serializers.deserialize(ser_fmt, fixture_data, using=self.using, ignorenonexistent=self.ignore)
    for obj in objects:
        obj.save()

# check constraints
connection.check_constraints(table_names=table_names)

# reset sequence
sequence_sql = connection.ops.sequence_reset_sql(no_style(), self.models)

django-easy-fixture's People

Contributors

allenling avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.