My app is installed correctly and its models.py reads:
from django.db import models
class Album(models.Model):
artist = models.CharField(max_lenght=250)
album_title = models.CharField(max_lenght=500)
genre = models.CharField(max_lenght=100)
album_logo = models.CharField(max_lenght=1000)
class Song(models.Model):
album = models.ForeignKey(Album, on_delete=models.CASCADE)
file_type = models.CharField(max_lenght=10)
song_title = models.CharField(max_lenght=250)
But whenever I run python manage.py migrate, I get the following error:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\dougl\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0-py3.6.egg\django\core\management\__init__.py", line 371, in execute_from_command_line
utility.execute()
File "C:\Users\dougl\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0-py3.6.egg\django\core\management\__init__.py", line 347, in execute
django.setup()
File "C:\Users\dougl\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0-py3.6.egg\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\dougl\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0-py3.6.egg\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\Users\dougl\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0-py3.6.egg\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\dougl\AppData\Local\Programs\Python\Python36-32\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\dougl\desktop\django websites\twenty_one_pilots\downloads\models.py", line 3, in <module>
class Album(models.Model):
File "C:\Users\dougl\desktop\django websites\twenty_one_pilots\downloads\models.py", line 4, in Album
artist = models.CharField(max_lenght=250)
File "C:\Users\dougl\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0-py3.6.egg\django\db\models\fields\__init__.py", line 1042, in __init__
super().__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'max_lenght'
What could be wrong? I'm still a beginner and I'm still learning about databases.
Use this:
As per my comment, length (lenght) splelling is wrong.
from django.db import models
class Album(models.Model):
artist = models.CharField(max_length=250)
album_title = models.CharField(max_length=500)
genre = models.CharField(max_length=100)
album_logo = models.TextField()
class Song(models.Model):
album = models.ForeignKey(Album, on_delete=models.CASCADE)
file_type = models.CharField(max_length=10)
song_title = models.CharField(max_length=250)
You have spelled length wrong here
album_title = models.CharField(max_lenght=500)
happens to the best of us.
Related
I have a set of models:
class DebugConf(models.Model):
is_setup = models.BooleanField(default=False)
debug_setup_date = models.DateTimeField()
def __str__(self):
return self.is_setup
class Currency(models.Model):
currency_name = models.CharField(max_length=100)
currency_value_in_dollars = models.FloatField()
currency_value_in_dollars_date = models.DateTimeField()
def __str__(self):
return self.currency_name
class User(models.Model):
user_name = models.CharField(max_length=200)
user_pass = models.CharField(max_length=200)
join_date = models.DateTimeField()
def __str__(self):
return self.user_name
class Transaction(models.Model):
transaction_type = models.CharField(max_length=200)
transaction_amount = models.FloatField()
transaction_date = models.DateTimeField()
transaction_currency = models.ForeignKey(Currency, on_delete=models.CASCADE)
transaction_users = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.headline
a project 'crypto' and an app 'manage_crypto_currency' nested into it:
The app's views contains some initialization code:
views.py:
if IS_DEBUG_MODE:
print('[!!!INFO!!!] DEBUG MODE SET! USING GENERATED TABLES')
init_debug_tables()
def init_debug_tables():
# Check if debug table has already been initialized
debug_conf = DebugConf.objects.all()
if debug_conf.objects.exists():
return
At the moment, the db is not initialized; when running:
python manage.py makemigrations manage_crypto_currency
in the root (project dir):
I get:
[!!!INFO!!!] DEBUG MODE SET! USING GENERATED TABLES
Traceback (most recent call last):
File "manage.py", line 24, in <module>
main()
File "manage.py", line 20, in main
execute_from_command_line(sys.argv)
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\management\base.py", line 368, in execute
self.check()
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\management\base.py", line 392, in check
all_issues = checks.run_checks(
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\urls\resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\urls\resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\urls\resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\msebi\AppData\Local\Programs\Python\Python38-32\lib\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 "C:\projects\django\crypto-currency-board\crypto\crypto\urls.py", line 21, in <module>
path('', include('manage_crypto_currency.urls')),
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\msebi\AppData\Local\Programs\Python\Python38-32\lib\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 "C:\projects\django\crypto-currency-board\crypto\manage_crypto_currency\urls.py", line 5, in <module>
from . import views
File "C:\projects\django\crypto-currency-board\crypto\manage_crypto_currency\views.py", line 24, in <module>
init_debug_tables()
File "C:\projects\django\crypto-currency-board\crypto\manage_crypto_currency\setup_test_db\setup_debug_tables.py", line 14, in init_debug_tables
if debug_conf.objects.exists():
AttributeError: 'QuerySet' object has no attribute 'objects'
The line:
[!!!INFO!!!] DEBUG MODE SET! USING GENERATED TABLES
is in views. Apparently, the migrations tool feels the need to execute views.py. Why on Earth, I have no clue. Is it possible to make makemigrations NOT execute code that it shouldn't, e.g. just create the models given that the db is not initialized?
EDIT
The answers don't answer WHY migrate runs views.py (I don't know either), but commenting out:
# if IS_DEBUG_MODE:
# print('[!!!INFO!!!] DEBUG MODE SET! USING GENERATED TABLES')
# init_debug_tables()
leads to successful migration. Is there a less cumbersome way to go about it? (Un)comment is ugly.
You don't need to use call the objects of a QuerySet
def init_debug_tables():
# Check if debug table has already been initialized
debug_conf = DebugConf.objects.all()
if debug_conf.exists(): # instead of `debug_conf.objects.exists()`
return
As the error message suggest, debug_conf is the the model class, it is an instance of a QuerySet.
objects is the Manager attached to a model class. This managers creates QuerySet for you (in your code, by the return value of the all method).
So you want to do this:
def init_debug_tables():
# Check if debug table has already been initialized
debug_conf = DebugConf.objects.all()
if debug_conf.exists():
return
Admittedly I’m a newbie to django and I’m getting the following error when trying to run python manage.py makemigrations
I’m trying to add a new field in to my class Product(models.Model):
featured = models.BooleanField()
Code
from django.db import models
# Create your models here.
class Product(models.Model):
title = models.CharField(max_length=120) # max_length = required
description = models.TextField(blank=True, null=True)
price = models.DecimalField(decimal_places=2, max_digits=10000)
summary = models.TextField()
featured = models.BooleenField() #only new added line
Error: AttributeError: module 'django.db.models' has no attribute 'BooleenField'
command:
python manage.py makemigrations <enter>
entire_traceback:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\.....\trydjango\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\.....\trydjango\lib\site-packages\django\core\management\__init__.py", line 357, in execute
django.setup()
File "C:\.....\trydjango\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\.....\trydjango\lib\site-packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\.....\trydjango\lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:\.....\trydjango\lib\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 "C:\.....\src\products\models.py", line 4, in <module>
class Product(models.Model):
File "C:\.....\src\products\models.py", line 9, in Product
featured = models.BooleenField()
AttributeError: module 'django.db.models' has no attribute 'BooleenField'
There is an error in your code for the Product models.
I have corrected it.
from django.db import models
class Product(models.Model):
title = models.CharField(max_length=120) # max_length = required
description = models.TextField(blank=True, null=True)
price = models.DecimalField(decimal_places=2, max_digits=10000)
summary = models.TextField()
featured = models.BooleanField() #only new added line
the default value for a boolean field is always false.
I am trying to use the CsvModel module from django-adaptors. However, when i use from adaptor.model import CsvModel and makemigrations, below errors occured.
Can anyone advise the reason why? my code in the model is as follows:
from django.db import models
from adaptor.model import CsvModel
from adaptor.fields import CharField, DecimalField
class User(models.Model):
user_name = models.CharField(max_length = 50, null=True, unique=True)
def __str__(self):
return self.user_name
class Profile(models.Model):
rubbish = models.DecimalField(max_digits= 1000, decimal_places=3, null=True)
user = models.CharField(max_length = 50, unique = True, null=True)
hobby = models.CharField(max_length = 50, null=True)
job = models.CharField(max_length = 60, null=True)
class Trial(CsvModel):
rubbish = DecimalField()
user = CharField()
hobby = CharField()
class Meta:
dbmodel = Profile
delimiter = ','
has_header = True
Error message is as follows:
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
django.setup()
File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
app_config.import_models()
File "/usr/local/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/usr/local/Cellar/python/3.6.4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/pangkachun/Desktop/smallpro2/small_pro/small/models.py", line 2, in <module>
from adaptor.model import CsvModel
File "/usr/local/lib/python3.6/site-packages/adaptor/model.py", line 269
except ValueError, e:
^ . `
You are using python3.6 but the library looks like it is made for python2.5-
For Python 3 it should be
except ValueError as e:
source
You may have to find a different csv plugin like django-csvimport and possibly change your strategy accordingly
I'm currently trying to build a blog using Django. I've been facing this error for a few hours now. I'm not quite sure if this has anything to do with the directories but the error occurs when I try to register my model in the admin.py file.
from django.contrib import admin
from .models import Post
# Register models
admin.site.register(Post)
The directories look as follows:
blog
models
Post
Category
admin.py
settings
static
templates
Trace:
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7ffb589a67b8>
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 112, in inner_run
autoreload.raise_last_exception()
File "/usr/lib/python3.6/site-packages/django/utils/autoreload.py", line 248, in raise_last_exception
raise _exception[1]
File "/usr/lib/python3.6/site-packages/django/core/management/__init__.py", line 327, in execute
autoreload.check_errors(django.setup)()
File "/usr/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/lib/python3.6/site-packages/django/apps/registry.py", line 120, in populate
app_config.ready()
File "/usr/lib/python3.6/site-packages/django/contrib/admin/apps.py", line 23, in ready
self.module.autodiscover()
File "/usr/lib/python3.6/site-packages/django/contrib/admin/__init__.py", line 26, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/usr/lib/python3.6/site-packages/django/utils/module_loading.py", line 47, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/hallak/Projects/hallak.io/hallak_blog/admin.py", line 5, in <module>
admin.site.register(Post)
File "/usr/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 102, in register
for model in model_or_iterable:
TypeError: 'module' object is not iterable
Post:
from django.db import models
from django.utils import timezone
class Post(models.Model):
# Auto-generated ID
id = models.AutoField(primary_key=True)
# Title
title = models.CharField(max_length=100)
# Slug
slug = models.SlugField(max_length=100)
# Content
body = models.TextField()
# Timestamps
created_on = models.DateField(auto_now_add=True)
published_on = models.DateTimeField(blank=True, null=True)
# Category
category = models.ForeignKey('.Category', on_delete=models.DO_NOTHING)
# Author
author = models.ForeignKey('auth.User', on_delete=models.DO_NOTHING)
# Publish post
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title
Category:
from django.db import models
class Category(models.Model):
# Auto-generated ID
id = models.AutoField(primary_key=True)
# Title
title = models.CharField(max_length=100)
# Slug
slug = models.SlugField(max_length=100)
# Timestamps
created_on = models.DateField(auto_now_add=True)
The error is happening here: https://github.com/django/django/blob/master/django/contrib/admin/sites.py#L100-L101
Whenever I comment the register line everything works fine.
Instead of writing "from .models import Post" you should write "from .models.Post import Post".
First "Post" is a modulename (file name), second one is a class name.
I am new in python.
consider this code:
from django.db import models
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
and this is my project structure:
Why i get this error when to want validating ?
'Module' Object Has no Attribute 'models'
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 4.0.1\helpers\pycharm\django_manage.py", line 41, in <module>
run_module(manage_file, None, '__main__', True)
File "C:\Python34\Lib\runpy.py", line 182, in run_module
return _run_module_code(code, init_globals, run_name, mod_spec)
File "C:\Python34\Lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Python34\Lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:/Users/Shahr_000/PycharmProjects/HelloWorld\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/Shahr_000/PycharmProjects/HelloWorld\HelloWorld\books\models.py", line 4, in <module>
class Publisher(models.Model):
AttributeError: 'module' object has no attribute 'Model'
Copy pasted your code.Worked for me.You should delete your migrations and try again.
cheers :-)