3

I am getting an error when trying to syncdb through the command line.

My goal is a basic music search/delete/update/add application with certain criteria and I am using Python and Django.

The class I'm trying to write:

from django.db import models

# music search
class musicsearch(models.Model):
    id = models.IntegerField
    title = models.CharField(max_length=40)
    artist = models.CharField(max_length=40)
    genre = models.CharField(max_length=20)

The error traceback:

C:\Users\jodie\Desktop\NtokloMH>python manage.py syncdb
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 354, in execute
    django.setup()
  File "C:\Python34\lib\site-packages\django\__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Python34\lib\site-packages\django\apps\registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "C:\Python34\lib\site-packages\django\apps\config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "C:\Users\jodie\Desktop\NtokloMH\musicsearch\models.py", line 4, in <module>
    class musicsearch(models.Model):
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 168, in __new__
    new_class.add_to_class(obj_name, obj)
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 297, in add_to_class
    value.contribute_to_class(cls, name)
TypeError: contribute_to_class() missing 1 required positional argument: 'name'
1
  • Hi Martijn Thank you for giving me the heads up on the Stackoverflow ways :) please see amended! Commented Mar 7, 2015 at 16:54

3 Answers 3

4

I can see two issues with your model.

You didn't create an instance of the IntegerField; you need to call it:

id = models.IntegerField()
#                       ^^

You are creating tuples for the other fields, because you end each field with a comma:

title = models.CharField(max_length=40),
#                                      ^

Remove those commas.

You don't really have to specify your own id field; models are by default given an id field automatically. See Automatic primary fields in the Django models documentation:

By default, Django gives each model the following field:

id = models.AutoField(primary_key=True)

This is an auto-incrementing primary key.

Since your specified your own id field that doesn't use primary_key=True, your model is probably going to run into problems with that too.

Sign up to request clarification or add additional context in comments.

4 Comments

I removed them and I am still getting the same thing (I actually tried to put them in there when I got the first error thinking I had missed them out
Thank you, That has disposed the original error yet created a new one, ERRORS: musicsearch.musicsearch: (models.E004) 'id' can only be used as a field name if the field also sets 'primary_key=True'. C:\Users\jodie\Desktop\NtokloMH>
@akseone: that's already covered in my answer. Just drop the whole id line, you don't need it.
Martijn thank you so much! I can't give you rep yet but I will be sure to come back through
1

Below line of code is missing

id = models.IntegerField(primary_key= True)

Comments

0

You are missing two () for your id integerfield.

id = models.IntegerField()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.