문제

Instead of repeatedly deleting my tables, recreating them and populating with data in my dev env, I decided to create a bash script called reset_db that does this for me. I got it to whack the tables, recreate them. But it's not able to populated the tables with data from the django orm.

I try to do this by calling the django shell from the script and then running ORM commands to populate my tables. But it seems like the django shell commands are not running.

I tried running the django orm commands manually/directly in the shell and they run fine but not from within the bash script.

The errors I get are:

NameError: name 'User' is not defined

NameError: name 'u1' is not defined

NameError: name 'm' is not defined

Here is my script:

#!/bin/bash
set +e

RUN_ON_MYDB="psql -X -U user --set ON_ERROR_STOP=on --set AUTOCOMMIT=off rcamp1"

$RUN_ON_MYDB <<SQL  # Whack tables
DROP TABLE rcamp_merchant CASCADE;
DROP TABLE rcamp_customer CASCADE;
DROP TABLE rcamp_point CASCADE;
DROP TABLE rcamp_order CASCADE;
DROP TABLE rcamp_custmetric CASCADE;
DROP TABLE rcamp_ordermetric CASCADE;
commit;
SQL

python manage.py syncdb   # Recreate tables

python manage.py shell <<ORM   # Start django shell. Problem starts here.
from rcamp.models import Customer, Merchant, Order, Point, CustMetric, OrderMetric
u1 = User.objects.filter(pk=5)
m = Merchant(u1, full_name="Bill Gates")
m
ORM

I'm new to both django and shell scripting. Thanks for your help.

도움이 되었습니까?

해결책

You should look at creating a fixture to populate your db https://docs.djangoproject.com/en/dev/howto/initial-data/

다른 팁

You need to import User explicitly. The django package and a few other things are automatically imported, but not everything you might want.

Also, to avoid not know what to import, there are management commands. This will leverage your Django and Python. You can learn shell scripting later.

clearly seen in your mistakes is not recognized as a model class User django-admin maybe you lack some import or something like this

  from django.db import models
  User import from django.contrib.auth.models

, by the way In line

  User.objects.filter u1 = (pk = 5)

I think I put

  u1 = User.objects.filter (pk = 5). First ()

at the end. Anyway, here I leave some threads that may be of help, https://docs.djangoproject.com/en/dev/ref/django-admin/ http://www.stackoverflow.com/questions/6197256/django-user-model-fields-at-adminmodel https://groups.google.com/forum/?fromgroups = #! topic/django-users/WrVp1DDFrX8 Hope this helps.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top