Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 874
sinθ Avatar asked Jul 23 '12 16:07

sinθ


People also ask

What is migration of database model in Django?

In the Python Django framework, the Migration of Database Model is used for making database tables using the model in Django. Django makes the tables of models through python code in Django. When a user creates a direct table in the database without Django code then Django doesn’t give all predefined features to that table.

Does Django use database introspection to edit models?

It doesn't work the other way around: Django does not use database introspection to pick up manual changes in the database and edit your models file. Now, there is a workaround, but it's not a long-term solution.

What is a model in Django?

A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table. Each model is a Python class that subclasses django.db.models.Model. Each attribute of the model represents a database field.

Why can’t I create another model field called author in Django?

In Django, this isn’t usually permitted for model fields. If a non-abstract model base class has a field called author, you can’t create another model field or define an attribute called author in any class that inherits from that base class. This restriction doesn’t apply to model fields inherited from an abstract model.


1 Answers

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.

like image 114
Danica Avatar answered Sep 18 '22 05:09

Danica