Django: My models.py file seems to have trouble syncing to the database.
Asked Answered
B

1

7

I tried setting up a simple models.py file as part of this tutorial that I was following online. When I tried the syncdb command, I got the following errors:

      File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/core/management/base.py", line 231, in execute
    self.validate()
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/core/management/base.py", line 266, in validate
    num_errors = get_validation_errors(s, app)
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/core/management/validation.py", line 30, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/db/models/loading.py", line 158, in get_app_errors
    self._populate()
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/db/models/loading.py", line 64, in _populate
    self.load_app(app_name, True)
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/db/models/loading.py", line 88, in load_app
    models = import_module('.models', app_name)
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Users/Mike/Desktop/Main/Django-Development/BBN/Knights/models.py", line 3, in <module>
    class Users(models.Model):
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/db/models/base.py", line 99, in __new__
    new_class.add_to_class(obj_name, obj)
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/db/models/base.py", line 219, in add_to_class
    value.contribute_to_class(cls, name)
TypeError: Error when calling the metaclass bases
    unbound method contribute_to_class() must be called with EmailField instance as first argument (got ModelBase instance instead)

This is my models.py file:

from django.db import models

class Users(models.Model):
    pen_name = models.CharField(max_length=30)
    email = models.EmailField

class Works(models.Model):
    user = models.ForeignKey(Users)
    date_published = models.DateField()

class Reviews(models.Model):
    work = models.ForeignKey(Works)
    date_published = models.DateField()

class Works_Subscriptions(models.Model):
    user = models.ForeignKey(Users)
    to_work = models.ForeignKey(Works)

class User_Subscriptions(models.Model):
    user = models.ForeignKey(Users)
    to_user = models.ForeignKey(Users)

class Books(models.Model):
    title = models.CharField(max_length=50)
    author = models.CharField(max_length=50)

If it's any help, I'm using sqlite3 and it worked before when I had nothing in the models.py file (so it was just syncing the database with django's usual tables)

Badr answered 23/7, 2012 at 16:13 Comment(1)
EmailField() not EmailField by itselfMonohydroxy
E
15

The problem is

email = models.EmailField

Change it to

email = models.EmailField()

This is because attributes of Django model classes are modified by the metaclass of django.db.models.Model rather than assigned directly as standard attributes, so that they can transparently talk to the database and all that. It's getting confused because you're trying to give it the EmailField class rather than an instance of that class.

Euhemerism answered 23/7, 2012 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.