AttributeError when I import model from another app - django

I don't know why but I got an error: AttributeError: module 'downloads.models' has no attribute 'AbstractDownload' in this place: class Download(models_downloads.AbstractDownload):
In download app I have already AbstractDownload class
Here is my model from products
products/models.py
from downloads import models as models_downloads
class Download(models_downloads.AbstractDownload):
product = models.ForeignKey('products.Product', related_name='downloads')
file = FilerFileField(related_name="file_products_download")
Here is downloads models
downloads/models.py
class AbstractDownload(models.Model):
title = models.CharField(max_length=500)
subtitle = models.CharField(max_length=1000, blank=True)
file = FilerFileField(related_name="file_abstract_download")
order = models.PositiveIntegerField(default=0)
class Meta:
abstract = True
def __str__(self):
return self.title
Traceback:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/path/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/path/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 337, in execute
django.setup()
File "/path/venv/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/path/venv/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models()
File "/path/venv/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/path/venv/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 "/path/www/downloads/models.py", line 6, in <module>
from products import models as products_models
File "/path/www/products/models.py", line 31, in <module>
class Download(models_downloads.AbstractDownload):
AttributeError: module 'downloads.models' has no attribute 'AbstractDownload'

You have a circular import. Your downloads/models.py has:
from products import models as products_models
But your products/models.py has
from downloads import models as models_downloads
You haven't shown how you use products_models, so we can't really say how to fix it. Here's a few suggestions:
Perhaps that import isn't necessary if you use a string for foreign keys, e.g. models.ForeignKey('downloads.ModelName')
Perhaps you could move AbstractDownload and Download so that they are in the same models, so that you don't need an import
If you use products_models inside a method, you could move the import inside the method. I would avoid this if possible.

Related

Unable to import model form model of another app

This is code of transactions.model
here i am trying to import WalletUser model form WalletUser.models
import uuid as uuid
from django.db import models
from walletApp.WalletUser.models import WalletUser
class Transaction(models.Model):
STATUS_TYPES = (
('C', 'Compleat'),
('S', 'Success'),
('I', 'Incompleat'),
('A', 'aborted'),
)
uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False, db_index=True)
status = models.CharField(choices=STATUS_TYPES, default='N', max_length=3)
created_on = models.DateTimeField(auto_now_add=True, null=False)
user = models.ForeignKey(WalletUser)
amount = models.FloatField()
def __str__(self):
return self.uuid
I am getting this error
/Users/shoaib/Documents/walletTransactionSystem/walletApp/walletApp/settings.py changed,
reloading.
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/Users/shoaib/Documents/walletTransactionSystem/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/shoaib/Documents/walletTransactionSystem/venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run
autoreload.raise_last_exception()
File "/Users/shoaib/Documents/walletTransactionSystem/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
raise _exception[1]
File "/Users/shoaib/Documents/walletTransactionSystem/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
autoreload.check_errors(django.setup)()
File "/Users/shoaib/Documents/walletTransactionSystem/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/shoaib/Documents/walletTransactionSystem/venv/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/shoaib/Documents/walletTransactionSystem/venv/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/Users/shoaib/Documents/walletTransactionSystem/venv/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/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 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/shoaib/Documents/walletTransactionSystem/walletApp/transaction/models.py", line 4, in <module>
from walletApp.WalletUser.models import WalletUser
ModuleNotFoundError: No module named 'walletApp.WalletUser'
I tried walletApp.WalletUser.models and WalletUser.models too both are not working.
WalletUser.models is straight up giving.
PLZ help I am stuck
from WalletUser.models import WalletUser
You are very close! Since your app is in the same directory as the base, you don't need to specify base directory, you can just import the models from the app.

Syntax Error in PostgreSql JSONField in django models

I have the following model
from django.db import models
from django.contrib.postgres.fields
import JSONField
class Fixture_lineups(models.Model):
fixture_id =
models.ForeignKey("Fixture",
null=True,
on_delete=models.SET_NULL)
home_formation =
models.CharField()
home_start_xi = JSONField()
home_substitutes = JSONField()
away_formation =
models.CharField()
away_start_xi = JSONField()
away_substitutes = JSONField()
lastUpdate =
models.DateTimeField(null=True)
After creating this model i was trying to do migrations to my database(PostgreSql) but my traceback said me that in my model is syntax error.
away_start_xi = JSONField()
away_substitutes = JSONField()
But i can not find this syntax error. Here is traceback
$ python manage.py makemigrations
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 "/data/data/com.termux/files/home/storage/predictions/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line utility.execute()
File "/data/data/com.termux/files/home/storage/predictions/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute django.setup()
File "/data/data/com.termux/files/home/storage/predictions/env/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
File "/data/data/com.termux/files/home/storage/predictions/env/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate app_config.import_models()
File "/data/data/com.termux/files/home/storage/predictions/env/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models self.models_module = import_module(models_module_name)
File "/data/data/com.termux/files/home/storage/predictions/env/lib/python3.7/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 "/data/data/com.termux/files/home/storage/predictions/forecast/dataflow/models.py", line 81
away_start_xi = JSONField()
away_substitutes = JSONField()
SyntaxError: invalid syntax

Getting an error when trying to set input_formats for Django TimeField

I am using the following model to represent a Class:
class Class(models.Model):
course = models.ForeignKey(Course, on_delete=models.CASCADE)
day = models.CharField(max_length=10, choices=WEEK_DAYS)
timing = models.TimeField(input_formats = ['%I:%M %p', ])
room = models.CharField(max_length=10)
I want to set my TimeField's format to 12 hours with am and pm. Django's documentation mentions using an input_formats argument to accomplish this here. However, when I run makemigrations, I get the following error:
Traceback (most recent call last):
File ".\manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "D:\Applications\Python\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
utility.execute()
File "D:\Applications\Python\lib\site-packages\django\core\management\__init__.py", line 347, in execute
django.setup()
File "D:\Applications\Python\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "D:\Applications\Python\lib\site-packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "D:\Applications\Python\lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "D:\Applications\Python\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 "D:\plannerly\timetable\models.py", line 15, in <module>
class Class(models.Model):
File "D:\plannerly\timetable\models.py", line 18, in Class
timing = models.TimeField(input_formats = ['%I:%M %p', ])
File "D:\Applications\Python\lib\site-packages\django\db\models\fields\__init__.py", line 2146, in __init__
super().__init__(verbose_name, name, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'input_formats'
I'm not really sure whats causing this. Any ideas?
input_formats is for forms.DateField()[1]. It is not a model.DateField()[2] option.
You have to set input_fomats=[] in your form, not in your models.
[1] https://docs.djangoproject.com/en/2.2/ref/forms/fields/#timefield
[2] https://docs.djangoproject.com/en/2.1/ref/models/fields/#django.db.models.DateField

Django has no attribute 'GeoManager' issue

from django.db import models
from django.contrib.gis.db import models
location = models.PointField(srid=4326,null=True,blank=True)
objects = models.GeoManager()
I am hosting my Django project on AWS server. I am unable to run the project because of the error I added below, but I implemented the same project in my Ubuntu system and it's working fine.
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f527104b6a8> Traceback (most recent call last): File "/home/ubuntu/django_env/lib/python3.5/site-packages/django/utils/autoreload.py", line 225, in wrapper fn(*args, **kwargs) File "/home/ubuntu/django_env/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 112, in inner_run autoreload.raise_last_exception() File "/home/ubuntu/django_env/lib/python3.5/site-packages/django/utils/autoreload.py", line 248, in raise_last_exception raise _exception[1] File "/home/ubuntu/django_env/lib/python3.5/site-packages/django/core/management/__init__.py", line 327, in execute autoreload.check_errors(django.setup)() File "/home/ubuntu/django_env/lib/python3.5/site-packages/django/utils/autoreload.py", line 225, in wrapper fn(*args, **kwargs) File "/home/ubuntu/django_env/lib/python3.5/site-packages/django/__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "/home/ubuntu/django_env/lib/python3.5/site-packages/django/apps/registry.py", line 112, in populate app_config.import_models() File "/home/ubuntu/django_env/lib/python3.5/site-packages/django/apps/config.py", line 198, in import_models self.models_module = import_module(models_module_name) File "/home/ubuntu/django_env/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 "/home/ubuntu/Sg_Bus/SgBusTransport/models.py", line 10, in <module> class BusStop(models.Model): File "/home/ubuntu/Sg_Bus/SgBusTransport/models.py", line 17, in BusStop objects = models.GeoManager() AttributeError: module 'django.contrib.gis.db.models' has no attribute 'GeoManager'
Is it possible that you are running Django 2.0?
GeoManager has been removed.
A workaround that appears to function correctly as of Django 2.0:
Import in your models file:
from django.db.models import Manager as GeoManager
In your model class:
objects = GeoManager()
Now you should be able to do spatial lookups!
Credit to this django-cities pull request.
You could try:
from django.contrib.gis.db.models.manager import GeoManager
Also you'll want to avoid situations like this
from django.db import models
from django.contrib.gis.db import models
The problem is this, you've imported two models modules - which one does Python use when you try to use models.Whatever?
Try something like this:
from django.db import models
from django.contrib.gis.db import models as gis_models
Instead of GeoManager you can now just use Manager
from django.db.models.manager import Manager

NameError: name PositiveSmallIntegerField is not defined

Django 1.5 & Python 3.2.3, on Ubuntu 12.04.
Very odd error when trying to syncdb my DB. I've got some fields that don't look wrong but Django's spitting out this error message when I run python3 manage.py sqlall..
NameError: name 'PositiveSmallIntegerField' is not defined
Huh? I tried Googling this, but there's not a single thing about it anywhere. So I'm guessing I've made some stupid mistake but I can't see it. My models.py looks like this:
from django.db import models
class Song(models.Model):
own = models.BooleanField(default=True)
heard = models.DateTimeField(blank=True,null=True)
release_date = models.DateField(blank=True,null=True)
style = models.CharField(max_length=255,blank=True,null=True)
artist = models.CharField(max_length=255,blank=True,null=True)
featuring = models.CharField(max_length=255,blank=True,null=True)
title = models.CharField(max_length=255,blank=True,null=True)
listen = models.URLField(max_length=255,blank=True,null=True)
highest_chart_pos = models.PositiveSmallIntegerField()
note = models.TextField(blank=True,null=True)
That's all there is to it, and I copy-n-pasted PositiveSmallIntegerField straight from the Django documentation so I know there isn't a typo in it that I'm overlooking. So, how is PositiveSmallIntegerField not defined?
Throwing this in here because I found myself with the same error, creating a model for a
song, too!
from django.db import models
class Track(models.Model):
release = models.ForeignKey('Release', on_delete=models.CASCADE)
position = models.PositiveSmallIntegerField()
number = PositiveSmallIntegerField()
name = models.CharField(max_length=100)
length = models.DurationField()
There is a slight difference, here. I left out the namespace of models. before my PositiveSmallIntegerField. It looks like you didn't do that, above, but maybe someone else with a similar error will made my mistake, too.
Here was my stacktrace:
(env) Cam#cambook:~/Sites/juke% python manage.py makemigrations playlists
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/Users/Cam/Sites/juke/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/Users/Cam/Sites/juke/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
django.setup()
File "/Users/Cam/Sites/juke/env/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/Cam/Sites/juke/env/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
app_config.import_models()
File "/Users/Cam/Sites/juke/env/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.5_1/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/Cam/Sites/juke/playlists/models.py", line 60, in <module>
class Track(models.Model):
File "/Users/Cam/Sites/juke/playlists/models.py", line 63, in Track
number = PositiveSmallIntegerField()
NameError: name 'PositiveSmallIntegerField' is not defined