0

There are 6 tables in DB out of it 3 will have same values everytime like static values these values need to be loaded into tables when we do python manage.py runserver. what will be the approach for this do i need to have a script for it Any help would be appreciated.

Thanks.

2 Answers 2

1

I would load them into a CONTEXT_PROCESSOR so that they are available in all templates.

if you need it available as global variables, simple load them in a custom script.py file that you import into the views.py file you need it in.

edit:

you should also consider caching the results from the database and either saving in the SESSION or look into running memcached if the site could see a lot of traffic.

Sign up to request clarification or add additional context in comments.

Comments

0

If you have some tables which are having same values everytime(staic values).
You can write your own [fixture][1] which define all values we are going to store into those tables.
You can create a directory of name fixtures into app folder. Directory example :

|- my_app
|    |- fixtures
|           |- static_content.json

static_content.json can looks like

[
  {
    "model": "myapp.model1",
    "pk": 1,
    "fields": {
      "first_name": "test",
      "last_name": "Test_last"
    }
  },
  {
    "model": "myapp.model2",
    "pk": 1,
    "fields": {
      "age": "21",
      "email": "[email protected]"
    }
  }
]

before starting server by python manage.py runserver we can load our fixtures into our database by using below command.

python manage.py loaddata <fixturename>
Ex.   python manage.py loaddata my_app/static_content.json

above command will work as a startup script for loading values into db tables.

3 Comments

wouldn't this be more for using in a test environment, where you might change the data during a test and want to be able to quickly restore the database state?
You can consider it in that way too but fixtures use more than using in a test environment, but my suggestion for fixtures was because you were looking for a startup script for loading values into db tables.
I may have read his question wrong; my approach solves the, you want to put configuration values in the database and load them on startup/needed. Yours solves the need to pre-populate the database with some values on startup. similar, but different needs really. Thanks for the insights.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.