Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in a class when trying to syncDB (Python Django)

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'
like image 338
akseone Avatar asked Mar 24 '26 01:03

akseone


1 Answers

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.

like image 80
Martijn Pieters Avatar answered Mar 26 '26 18:03

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!