legend
legend

Reputation: 207

Cannot run django server after integrating database

I am having problems running my server after I try to integrate the database with the application using the "python manage.py inspectdb > /models.py" command.

This is what I have in my models.py file

# 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'

and this is the error message I get

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

It seems I have a null variable some place but I don't know where that is coming from. I would appreciate some help.

Upvotes: 2

Views: 702

Answers (2)

roadrunner66
roadrunner66

Reputation: 7941

I had the same problem using Sublime 3 as editor. It got solved if I resaved the models.py file in my app folder as 'Save with Encoding :: UTF-8'.

Upvotes: 0

Robyn Huffaker
Robyn Huffaker

Reputation: 25

I just had this problem myself. I finally fixed it:

  • open the generated model.py file in Notepad++ (or other)
  • copy/paste the generated code into a new file in IDLE
  • Save over model.py

I'm not sure why this works, but I got an encoding error trying to open the file directly in IDLE. So I copy/pasted the code, and it fixes everything.

Upvotes: 2

Related Questions