集成数据库后无法运行django服务器

时间:2014-03-12 03:52:04

标签: django django-models

在尝试使用" python manage.py inspectdb>将数据库与应用程序集成后,我在运行服务器时遇到问题/models.py"命令。

这就是我在models.py文件中的内容

# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
#     * Rearrange models' order
#     * Make sure each model has one field with primary_key=True
# Feel free to rename the models, but don't rename db_table values or field names.
#
# Also note: You'll have to insert the output of 'django-admin.py sqlcustom [appname]'
# into your database.
from __future__ import unicode_literals

from django.db import models

class AuthGroup(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=80)
    class Meta:
        db_table = 'auth_group'

class AuthGroupPermissions(models.Model):
    id = models.IntegerField(primary_key=True)
    group = models.ForeignKey(AuthGroup)
    permission = models.ForeignKey('AuthPermission')
    class Meta:
        db_table = 'auth_group_permissions'

class AuthPermission(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=50)
    content_type = models.ForeignKey('DjangoContentType')
    codename = models.CharField(max_length=100)
    class Meta:
        db_table = 'auth_permission'

class AuthUser(models.Model):
    id = models.IntegerField(primary_key=True)
    password = models.CharField(max_length=128)
    last_login = models.DateTimeField()
    is_superuser = models.BooleanField()
    username = models.CharField(max_length=30)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.CharField(max_length=75)
    is_staff = models.BooleanField()
    is_active = models.BooleanField()
    date_joined = models.DateTimeField()
    class Meta:
        db_table = 'auth_user'

class AuthUserGroups(models.Model):
    id = models.IntegerField(primary_key=True)
    user = models.ForeignKey(AuthUser)
    group = models.ForeignKey(AuthGroup)
    class Meta:
        db_table = 'auth_user_groups'

class AuthUserUserPermissions(models.Model):
    id = models.IntegerField(primary_key=True)
    user = models.ForeignKey(AuthUser)
    permission = models.ForeignKey(AuthPermission)
    class Meta:
        db_table = 'auth_user_user_permissions'

class DjangoContentType(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=100)
    app_label = models.CharField(max_length=100)
    model = models.CharField(max_length=100)
    class Meta:
        db_table = 'django_content_type'

class DjangoSession(models.Model):
    session_key = models.CharField(max_length=40)
    session_data = models.TextField()
    expire_date = models.DateTimeField()
    class Meta:
        db_table = 'django_session'

class DjangoSite(models.Model):
    id = models.IntegerField(primary_key=True)
    domain = models.CharField(max_length=100)
    name = models.CharField(max_length=50)
    class Meta:
        db_table = 'django_site'

class DjangoUser(models.Model):
    firstname = models.CharField(max_length=256)
    lastname = models.CharField(max_length=256)
    username = models.CharField(primary_key=True, max_length=256)
    password = models.CharField(max_length=256)
    class Meta:
        db_table = 'django_user'

这是我收到的错误消息

Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.comma
nds.runserver.Command object at 0x0000000002CD1518>>
Traceback (most recent call last):
  File "C:\Python33\lib\site-packages\django\core\management\commands\runserver.py", line 92, in inner_run
    self.validate(display_num_errors=True)
  File "C:\Python33\lib\site-packages\django\core\management\base.py", line 280, in validate
    num_errors = get_validation_errors(s, app)
  File "C:\Python33\lib\site-packages\django\core\management\validation.py", line 35, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "C:\Python33\lib\site-packages\django\db\models\loading.py", line 166, in get_app_errors
    self._populate()
  File "C:\Python33\lib\site-packages\django\db\models\loading.py", line 72, in _populate
    self.load_app(app_name, True)
  File "C:\Python33\lib\site-packages\django\db\models\loading.py", line 96, in load_app
    models = import_module('.models', app_name)
  File "C:\Python33\lib\site-packages\django\utils\importlib.py", line 35, in import_module
    __import__(name)
TypeError: source code string cannot contain null bytes

似乎我在某个地方有一个零变量,但我不知道它来自哪里。我将不胜感激。

2 个答案:

答案 0 :(得分:2)

我自己就是这个问题。我终于解决了它:

  • 在Notepad ++(或其他)
  • 中打开生成的model.py文件
  • 将生成的代码复制/粘贴到IDLE
  • 中的新文件中
  • 保存在model.py

我不确定为什么这是有效的,但是在尝试直接在IDLE中打开文件时出现编码错误。所以我复制/粘贴代码,它修复了所有内容。

答案 1 :(得分:0)

使用Sublime 3作为编辑器,我遇到了同样的问题。如果我将我的应用文件夹中的models.py文件重新保存为“使用编码保存:: UTF-8”,就解决了。