如何填写数据表?

时间:2014-09-11 08:22:10

标签: python django python-3.x

请帮助解决问题。

python3.4,django1.6。 我正在尝试将数据从文件initial_data.json

放入数据库

命令之后

python manage.py syncdb --all

表已创建,但文件initial_data.json中的数据将不适合表。这是控制台输出:

(ds_d16)kalinins@kalinins-Lenovo-Z580 ~/.virtualenvs/ds_d16/django_projects/drummersaransk_new $ python manage.py syncdb --all
Syncing...
Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table captcha_captchastore
Creating table thumbnail_kvstore
Creating table app_drummersaransk_city
Creating table app_drummersaransk_status
Creating table app_drummersaransk_gender
Creating table app_drummersaransk_teacher
Creating table app_drummersaransk_userprofile
Creating table app_drummersaransk_pathglory
Creating table app_drummersaransk_friends
Creating table app_drummersaransk_message
Creating table south_migrationhistory

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'kalinins'): admin
Email address: prozaik81-2@yandex.ru
Password: 
Password (again): 
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/kalinins/.virtualenvs/ds_d16/lib/python3.4/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/home/kalinins/.virtualenvs/ds_d16/lib/python3.4/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/kalinins/.virtualenvs/ds_d16/lib/python3.4/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/kalinins/.virtualenvs/ds_d16/lib/python3.4/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/home/kalinins/.virtualenvs/ds_d16/lib/python3.4/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/home/kalinins/.virtualenvs/ds_d16/lib/python3.4/site-packages/south/management/commands/syncdb.py", line 92, in handle_noargs
    syncdb.Command().execute(**options)
  File "/home/kalinins/.virtualenvs/ds_d16/lib/python3.4/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/home/kalinins/.virtualenvs/ds_d16/lib/python3.4/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/home/kalinins/.virtualenvs/ds_d16/lib/python3.4/site-packages/django/core/management/commands/syncdb.py", line 162, in handle_noargs
    database=db, skip_validation=True)
  File "/home/kalinins/.virtualenvs/ds_d16/lib/python3.4/site-packages/django/core/management/__init__.py", line 159, in call_command
    return klass.execute(*args, **defaults)
  File "/home/kalinins/.virtualenvs/ds_d16/lib/python3.4/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/home/kalinins/.virtualenvs/ds_d16/lib/python3.4/site-packages/django/core/management/commands/loaddata.py", line 55, in handle
    self.loaddata(fixture_labels)
  File "/home/kalinins/.virtualenvs/ds_d16/lib/python3.4/site-packages/django/core/management/commands/loaddata.py", line 84, in loaddata
    self.load_label(fixture_label)
  File "/home/kalinins/.virtualenvs/ds_d16/lib/python3.4/site-packages/django/core/management/commands/loaddata.py", line 134, in load_label
    for obj in objects:
  File "/home/kalinins/.virtualenvs/ds_d16/lib/python3.4/site-packages/django/core/serializers/json.py", line 65, in Deserializer
    stream_or_string = stream_or_string.read()
  File "/home/kalinins/.virtualenvs/ds_d16/lib/python3.4/codecs.py", line 313, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc2 in position 86: invalid continuation byte

1 个答案:

答案 0 :(得分:0)

根据我的经验,使用初始json数据是一个皮塔饼。使用sql插入更容易创建初始值。从数据库中创建初始值的sql转储,并将它们转换为每个模型单独的.sql文件。将它们放在正确的应用程序的文件夹中。

所以它可能是这样的:
- foo (your app) - sql (the sql folder *with* __init__.py) - bar.sql (your Bar model insert sql's)

试一试。