How to solve this django OneToOneField model error? - python

I'm trying to make a model that needs a relationship of one to one with a feedstock that is used by a plenty of formulas. This is my code:
from django.db import models
from dashboard.models import Formulas, Feedstock
class FeedstockFormulas(models.Model):
ratio = models.FloatField()
feedstock = models.OneToOneField(Feedstock, on_delete=models.CASCADE, default="0")
formulas = models.ForeignKey(Formulas, on_delete=models.CASCADE)
This is the error I'm getting:
Traceback (most recent call last):
File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/db/models/fields/related.py", line 798, in __init__
to._meta.model_name
AttributeError: module 'dashboard.models.Feedstock' has no attribute '_meta'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/core/management/__init__.py", line 377, in execute
django.setup()
File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/usr/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 "/mnt/c/Users/isaac/Documents/Github/uniaoracoes/dashboard/models/__init__.py", line 1, in <module>
from .Formulas import Formulas
File "/mnt/c/Users/isaac/Documents/Github/uniaoracoes/dashboard/models/Formulas.py", line 2, in <module>
from dashboard.models import FeedstockFormulas
File "/mnt/c/Users/isaac/Documents/Github/uniaoracoes/dashboard/models/FeedstockFormulas.py", line 4, in <module>
class FeedstockFormulas(models.Model):
File "/mnt/c/Users/isaac/Documents/Github/uniaoracoes/dashboard/models/FeedstockFormulas.py", line 6, in FeedstockFormulas
feedstock = models.OneToOneField(Feedstock, on_delete=models.CASCADE, default="0")
File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/db/models/fields/related.py", line 1049, in __init__
super().__init__(to, on_delete, to_field=to_field, **kwargs)
File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/db/models/fields/related.py", line 800, in __init__
assert isinstance(to, str), (
AssertionError: OneToOneField(<module 'dashboard.models.Feedstock' from '/mnt/c/Users/isaac/Documents/Github/uniaoracoes/dashboard/models/Feedstock.py'>) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string 'self'
I can't really solve it, since the first parameter to ForeignKey is already a model...

It looks like this is a by-product of a circular import somewhere.
Try changing to
feedstock = models.OneToOneField("dashboard.Feedstock", on_delete=models.CASCADE, default="0")
formulas = models.ForeignKey("dashboard.Formulas", on_delete=models.CASCADE)

Related

How do I solve a traceback when runing manage.py commands?

I am pretty new to Django and I was just following along witht the tutorial.
I was making a project from https://github.com/justdjango/getting-started-with-django, and after I added users and a superuser, I started to get a Traceback like this:
Traceback (most recent call last):
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\contrib\auth\__init__.py", line 160, in get_user_model
return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\apps\registry.py", line 204, in get_model
app_label, model_name = app_label.split('.')
ValueError: not enough values to unpack (expected 2, got 1)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Dell\Desktop\prog\manage.py", line 22, in <module>
main()
File "C:\Users\Dell\Desktop\prog\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\core\management\__init__.py", line 395, in execute
django.setup()
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\apps\registry.py", line 122, in populate
app_config.ready()
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\contrib\admin\apps.py", line 27, in ready
self.module.autodiscover()
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\contrib\admin\__init__.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\utils\module_loading.py", line 47, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\contrib\auth\admin.py", line 6, in <module>
from django.contrib.auth.forms import (
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\contrib\auth\forms.py", line 21, in <module>
UserModel = get_user_model()
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\contrib\auth\__init__.py", line 162, in get_user_model
raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form 'app_label.model_name'")
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL must be of the form 'app_label.model_name'
You forgot to specify the model name in the AUTH_USER_MODEL setting. If you defined a model User in the leads app, you thus set this with:
# settings.py
# app name &downarrow;
AUTH_USER_MODEL = 'leads.User'
# model name &uparrow;

Getting TypeError: __init__() got an unexpected keyword argument 'upload_to' in Django Project?

I have just started learning Django frame work and got stuck after getting an unexpected error as below:-(I have tried to post all information, which i find necessary to be here)
TypeError: __init__() got an unexpected keyword argument 'upload_to'
I got this error while running command python manage.py makemigrations
My project contains only one Model class Products
My Model Class:-
from django.db import models
# Create your models here.
class Products(models.Model):
name = models.CharField(max_length=25)
price = models.IntegerField()
description = models.CharField(max_length=50)
pic = models.DateTimeField(upload_to="myimages")
I am added my app in INSTALLED_APPS fields in setting.py
Full Error Track:-
Traceback (most recent call last):
File "C:\Users\Admin\PycharmProjects\ecommerceproject\manage.py", line 22, in <module>
main()
File "C:\Users\Admin\PycharmProjects\ecommerceproject\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 395, in execute
django.setup()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 855, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Users\Admin\PycharmProjects\ecommerceproject\products\models.py", line 5, in <module>
class Products(models.Model):
File "C:\Users\Admin\PycharmProjects\ecommerceproject\products\models.py", line 9, in Products
pic = models.DateTimeField(upload_to="myimages")
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\fields\__init__.py", line 1158, in __init__
super().__init__(verbose_name, name, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'upload_to'
C:\Users\Admin\PycharmProjects\ecommerceproject>cls
C:\Users\Admin\PycharmProjects\ecommerceproject>python manage.py makemigrations
Traceback (most recent call last):
File "C:\Users\Admin\PycharmProjects\ecommerceproject\manage.py", line 22, in <module>
main()
File "C:\Users\Admin\PycharmProjects\ecommerceproject\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 395, in execute
django.setup()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 855, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Users\Admin\PycharmProjects\ecommerceproject\products\models.py", line 5, in <module>
class Products(models.Model):
File "C:\Users\Admin\PycharmProjects\ecommerceproject\products\models.py", line 9, in Products
pic = models.DateTimeField(upload_to="myimages")
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\fields\__init__.py", line 1158, in __init__
super().__init__(verbose_name, name, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'upload_to'
Thanks in advance.
Hope to here from you soon.
It doesn't seem like DateTimeField accepts the upload_to keyword argument.
pic = models.DateTimeField(upload_to="myimages")
See docs.
Were you trying to use another Model Field? e.g., ImageField, FileField
You are getting error because of it. Try with proper field
pic = models.DateTimeField(upload_to="myimages")

How to fix Python circular import error (top level)?

I read about solution for the error (write import instead of from ...) but it doesn't work I think because I have a complex folder structure.
quiz.models
import apps.courses.models as courses_models
courses_models.Lesson # some actions with that class
class Quiz:
pass
courses.models
import apps.quiz.models as quiz_models
quiz_models.Quiz # some actions with that class
class Lesson:
pass
Error:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 377, in execute
django.setup()
File "/usr/local/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/usr/local/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/usr/local/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 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/code/apps/carts/models.py", line 5, in <module>
from ..courses.models import Course
File "/code/apps/courses/models.py", line 12, in <module>
import apps.quiz.models as quiz_models
File "/code/apps/quiz/models.py", line 12, in <module>
class Quiz(TimestampedModel):
File "/code/apps/quiz/models.py", line 15, in Quiz
courses_models.Lesson, default=1, on_delete=models.DO_NOTHING, verbose_name=_('Lesson'), related_name='quizzes')
AttributeError: partially initialized module 'apps.courses.models' has no attribute 'Lesson' (most likely due to a circular import)
Maybe you have other solutions for circular importing or I just wrote the imports incorrectly?
Yes, this is a circular import. course.models is importing quiz.models while quiz.models is also importing course.models.
To avoid this, you could defer the import until you really need it.

Error using Textfield and charfield model in Django

I have been trying to use Textfield in class but the suggestions just does not show up. Instead i receive an error in my Cmd console. This was even the case while using CharField.
Below is my code:
from django.db import models
from django.db.models import CharField
from django.db.models import Textfield
from datetime import datetime
class Posts(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
created_at = models.DateTimeField(default=datetime.now, blank=True)
This is the error i get in my CMD:
Exception in thread django-main-thread:
Traceback (most recent call last):
File "d:\python\python38\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "d:\python\python38\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\USER\Envs\py1\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\USER\Envs\py1\lib\site-packages\django\core\management\commands\runserver.py", line
109, in inner_run
autoreload.raise_last_exception()
File "C:\Users\USER\Envs\py1\lib\site-packages\django\utils\autoreload.py", line 76, in
raise_last_exception
raise _exception[1]
File "C:\Users\USER\Envs\py1\lib\site-packages\django\core\management\__init__.py", line 357, in
execute
autoreload.check_errors(django.setup)()
File "C:\Users\USER\Envs\py1\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\USER\Envs\py1\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\USER\Envs\py1\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\USER\Envs\py1\lib\site-packages\django\apps\config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "d:\python\python38\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 "E:\Python\djangoproject\posts\models.py", line 3, in <module>
from django.db.models import Textfield
ImportError: cannot import name 'Textfield' from 'django.db.models' (C:\Users\USER\Envs\py1\lib\site-
packages\django\db\models\__init__.py)
Actually you don't need to import the fields separately, you could use the django.db.models that you have imported earlier.
And the error is produced because you should use TextField with a capital F

Cannot Import Model Name Django with User Model and Informational Model

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x105de5048>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/autoreload.py", line 248, in raise_last_exception
raise _exception[1]
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 337, in execute
autoreload.check_errors(django.setup)()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
app_config.import_models()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/Library/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/Name/Desktop/ProjectDevCode/Restructured/mysite/seasons/models.py", line 2, in <module>
from users.models import User
File "/Users/Name/Desktop/ProjectDevCode/Restructured/mysite/users/models.py", line 11, in <module>
from seasons.models import Seasons
ImportError: cannot import name 'Seasons'
I randomly got this error in my recent django app. I am not sure why. The models import in other apps/files in my project. Any help is appreciated.
I believe you faced circular model import. Seems like you are importing User into seasons.model and Seasons into users.model.
If you use them as a foreign key you can set it as:
user = models.ForeignKey('users.User')

Categories