When I was working on a Django project (blog), I had an error(s) while working on the site. Here are the errors I have appeared:
1: When I entered the python command manage.py makemigrations blog(via the console) in the directory C:\mysite\site\miniproject , then there is this:
Traceback (most recent call last):
File "manage.py", line 23, in <module>
main()
File "manage.py", line 19, in main
execute_from_command_line(sys.argv)
File "C:\Program Files\Python36\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:\Program Files\Python36\lib\site-packages\django\core\management\__init__.py", line 395, in execute
django.setup()
File "C:\Program Files\Python36\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Program Files\Python36\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Program Files\Python36\lib\site-packages\django\apps\config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "C:\Program Files\Python36\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "C:\mysite\site\miniproject\blog\models.py", line 5, in <module>
class Post(models.Model):
File "C:\mysite\site\miniproject\blog\models.py", line 12, in Post
author = models.ForeignKey(User, related_name='blog_posts')
TypeError: __init__() missing 1 required positional argument: 'on_delete'
Although I did everything according to the instructions on the website https://pocoz .gitbooks.io/django-v-primerah/content/sozdanie-i-primenenie-migracij.html.
I did everything according to plan, I did everything in order and there was such a mistake. And I do not know how to fix it
Updated all the necessary libraries, entered them in manage.ру (which is located in the directory C:\mysite\site\miniproject ) import django, it didn't help
You have declared a ForeignKey somewhere but not provided the on_delete keyword argument.
If you post the BlogPost model, I can give you an exact answer, but you probably want something like:
models.ForeignKey(..., on_delete=models.CASCADE)
To fix the issue add the key word argument to the BlogPost model in blog/models.py
If you debug your error its pretty self-explanatory:
In line 12 File "C:\mysite\site\miniproject\blog\models.py", line 12, in Post
in your Post model you have a field
author = models.ForeignKey(User, related_name='blog_posts')
You need to change that to:
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
Related
I am learning this Django Framework from this summer. I have get the error <TypeError: 'NoneType' object is not callable>. I am trying to narrow down the problem. However, after I have delete a lot of code, I still cannot figure this out from the most simple code. My code is showed as below.
admin.py
from django.contrib import admin
from rest_framework.renderers import AdminRenderer
from .models import Car
#admin.site.register(Car)
class CarAdmin(admin.ModelAdmin):
list_display = ('id', 'brand', 'model')
models.py
from django.db import models
from django.contrib import admin
class Car(models.Model):
brand = models.TextField(default='Honda')
model = models.TextField(default='EK9')
class Meta:
db_table = 'car'
def __str__(self):
return self.model
the error
LeodeMacBook-Pro:sharky leo$ python ./manage.py runserver
/Users/leo/opt/anaconda3/lib/python3.8/site-packages/environ/environ.py:628: UserWarning: /Users/leo/Documents/python_test/leo_first_python_side_project/sharky/sharky/mysite/.env doesn't exist - if you're not configuring your environment separately, create one.
warnings.warn(
/Users/leo/opt/anaconda3/lib/python3.8/site-packages/environ/environ.py:628: UserWarning: /Users/leo/Documents/python_test/leo_first_python_side_project/sharky/sharky/mysite/.env doesn't exist - if you're not configuring your environment separately, create one.
warnings.warn(
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/Users/leo/opt/anaconda3/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/Users/leo/opt/anaconda3/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/Users/leo/django/django/utils/autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "/Users/leo/django/django/core/management/commands/runserver.py", line 114, in inner_run
autoreload.raise_last_exception()
File "/Users/leo/django/django/utils/autoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "/Users/leo/django/django/core/management/__init__.py", line 375, in execute
autoreload.check_errors(django.setup)()
File "/Users/leo/django/django/utils/autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "/Users/leo/django/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/leo/django/django/apps/registry.py", line 122, in populate
app_config.ready()
File "/Users/leo/django/django/contrib/admin/apps.py", line 27, in ready
self.module.autodiscover()
File "/Users/leo/django/django/contrib/admin/__init__.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/Users/leo/django/django/utils/module_loading.py", line 47, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/Users/leo/opt/anaconda3/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/leo/Documents/python_test/leo_first_python_side_project/sharky/sharky/car/admin.py", line 8, in <module>
class CarAdmin(admin.ModelAdmin):
TypeError: 'NoneType' object is not callable
environment
asgiref==3.4.1; python_version >= '3.6'
django-environ==0.4.5
django==3.2.6
pytz==2021.1
sqlparse==0.4.1; python_version >= '3.5'
what I have down
I have search the web of Django & Python and dozens of Questions on stack overflow.
I have realize that the most common solution is to remove the ().
However, I do not call the func. anywhere.
Therefore,I have raised this question to search for is there some mistake I do not understand about. Thanks for any advice/suggestion that help me to improve.
The error is from the #admin.site.register(Car)
this is because it returns None and wrapping CarAdmin with None is equivalent to doing CarAdmin = None(CarAdmin).
The solution is to use #admin.register(Car) instead
I'm making a test project with python and django (for learning purpose). At some point I have to make a django app called products, then in the models.py file in the "products" app directory I have this code:
class Products (models.Model)
title = models.TextField()
description = models.TextField()
price = models.TextField()
and then after adding the new app in the settings file when I attend to make migrations as shown:
> python manage.py makemigrations
I get is this error:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\django\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
utility.execute()
File "C:\django\lib\site-packages\django\core\management\__init__.py", line 347, in execute
django.setup()
File "C:\django\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\django\lib\site-packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\django\lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:\django\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 724, in exec_module
File "<frozen importlib._bootstrap_external>", line 860, in get_code
File "<frozen importlib._bootstrap_external>", line 791, in source_to_code
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\django\mystore\products\models.py", line 5
class Products(models.Model)
^
SyntaxError: invalid syntax
"
and then the same error occurs whenever I try to use the manage.py file. I don't know what I've done wrong please help me fix this.
You're missing a colon after the class declaration.
class Products(models.Model):
Also, I would caution you on your formatting. Your field names should line up on the left. In Python, two lines that don't line up are considered different blocks of code.
You missed a colon after you declared the class
class Products (models.Model) #<<--- no semicolon
title = models.TextField()
description = models.TextField()
price = models.TextField()
Right code:
class Products (models.Model): #<<--- colon
title = models.TextField()
description = models.TextField()
price = models.TextField()
I'm trying to use the dynamic upload path, for django FileFiled.
This is my model:
def use_assignment_path(instance, filename):
return 'assignment/%s/%s' % (instance.name, filename)
class Assignment(models.Model):
admin = models.ForeignKey(Admin)
name = models.CharField(max_length=50, unique=True)
lang = models.CharField(max_length=5, default='c', choices=(('c', 'c'), ('java', 'java')))
pointsRecommended = models.IntegerField()
file0 = models.FileField(upload_to=use_assignment_path)
file1 = models.FileField(upload_to=use_assignment_path, default='', blank=True)
When I try to migrate the models I get this errors:
Traceback (most recent call last):
File "manage.py", line 10, in <module> execute_from_command_line(sys.argv)
File "/home/mb/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/home/mb/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/mb/.local/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/mb/.local/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/mb/.local/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 83, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/home/mb/.local/lib/python3.5/site-packages/django/db/migrations/executor.py", line 20, in __init__
self.loader = MigrationLoader(self.connection)
File "/home/mb/.local/lib/python3.5/site-packages/django/db/migrations/loader.py", line 52, in __init__
self.build_graph()
File "/home/mb/.local/lib/python3.5/site-packages/django/db/migrations/loader.py", line 203, in build_graph
self.load_disk()
File "/home/mb/.local/lib/python3.5/site-packages/django/db/migrations/loader.py", line 114, in load_disk
migration_module = import_module("%s.%s" % (module_name, migration_name))
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/media/sf_Websites/HM/trunk/assignments/models/migrations/0001_initial.py", line 11, in <module>
class Migration(migrations.Migration):
File "/media/sf_Websites/HM/trunk/assignments/models/migrations/0001_initial.py", line 23, in Migration
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
AttributeError: module 'models' has no attribute 'AutoField'
If have no idea why it occurs, when I'm using a static upload path it works perfectly fine.
I have have read several posts with similar questions but nothing helped till know.
Tanke you!
It looks as if your app name models is clashing with django.db.models. Try renaming your app, delete the initial migration, then rerun makemigrations and migrate.
delete "/media/sf_Websites/HM/trunk/assignments/models/migrations/0001_initial.py" file and try again
I have imported the following header file
from django.contrib.postgres.fields import ArrayField
Used the following in the model
question_array = ArrayField(models.IntegerField, blank=True,)
i get the following error
Traceback (most recent call last)
File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\apps\registry.py", line 108, in populate
app_config.import_models(all_models)
File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\apps\config.py", line 199, in import_models
self.models_module = import_module(models_module_name)
File "C:\Program Files (x86)\Python\Python35-32\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "C:\Users\DELL\Desktop\cstrom\comp\models.py", line 24, in <module>
class User(models.Model):
File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\db\models\base.py", line 157, in __new__
new_class.add_to_class(obj_name, obj)
File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\db\models\base.py", line 316, in add_to_class
value.contribute_to_class(cls, name)
File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\db\models\fields\__init__.py", line 689, in contribute_to_class
self.set_attributes_from_name(name)
File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\contrib\postgres\fields\array.py", line 75, in set_attributes_from_name
self.base_field.set_attributes_from_name(name)
TypeError: set_attributes_from_name() missing 1 required positional argument: 'name'
As per the docs
You still need to fully define the field inside the array field
question_array = ArrayField(models.IntegerField(null=True, blank=True), blank=True,)
models.IntegerField is a function not a property, so you will be required to call it like a function. () at the end
question_array = ArrayField(models.IntegerField(blank=True), blank=True,)
Read more about it here.
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'
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.
Below line of code is missing
id = models.IntegerField(primary_key= True)
You are missing two () for your id integerfield.
id = models.IntegerField()