Syntax Error in PostgreSql JSONField in django models - django

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

Related

Using translate field for 'unique' rises NameError, django-parler

I've recently installed django-parler==2.2 and when I rewrite my model to inherit from TranslatableModel and then try to makemigrations I get the following error:
main_menu_slug = models.SlugField(max_length=120, unique=main_menu_item)
NameError: name 'main_menu_item' is not defined
Everything runs fine when I inherit from models.Model. Here is the code:
class MenuItem(TranslatableModel):
translations = TranslatedFields (
main_menu_item = models.CharField(max_length=60),
main_menu_slug = models.SlugField(max_length=120, unique=main_menu_item)
)
I've just started using this module, so possibly it is something trivial but I couldn't find the solution. What I am doing wrong?
Full Traceback:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\project\alfa\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\project\alfa\lib\site-packages\django\core\management\__init__.py", line 377, in execute
django.setup()
File "C:\project\alfa\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\project\alfa\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\project\alfa\lib\site-packages\django\apps\config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\user\AppData\Local\Programs\Python\Python37\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 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\project\alfa\project\menus\models.py", line 16, in <module>
class MenuItem(TranslatableModel):
File "C:\project\alfa\project\menus\models.py", line 20, in MenuItem
main_menu_slug = models.SlugField(max_length=120, unique=main_menu_item)
NameError: name 'main_menu_item' is not defined
Working code models.Model:
class MenuItem(models.Model):
main_menu_item = models.CharField(max_length=60)
main_menu_slug = models.SlugField(max_length=120, unique=main_menu_item)
unique should be a boolean value as per the docs: https://docs.djangoproject.com/en/3.1/ref/models/fields/#unique

Django - set a model CharField value to the name of a field from another model

I have a model when I want to field_name to be the name of any field from another model.
I wrote this code:
from providers.models import Organisation
FIELD_CHOICES = [(x.name,x.verbose_name) for x in Organisation._meta.get_fields() ]
class Score (models.Model):
field_name = models.CharField(max_length=101, choices=FIELD_CHOICES)
When I run makemigrations I get django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
This is the complete traceback:
Traceback (most recent call last):
File "./src/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/henry/Documents/Sites/Development/autumna-dev/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/henry/Documents/Sites/Development/autumna-dev/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "/home/henry/Documents/Sites/Development/autumna-dev/env/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/henry/Documents/Sites/Development/autumna-dev/env/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
app_config.import_models()
File "/home/henry/Documents/Sites/Development/autumna-dev/env/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/home/henry/Documents/Sites/Development/autumna-dev/env/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/henry/Documents/Sites/Development/autumna-dev/autumna/src/scoring/models.py", line 7, in <module>
FIELD_CHOICES = [(x.name,x.verbose_name) for x in Organisation._meta.get_fields() ]
File "/home/henry/Documents/Sites/Development/autumna-dev/env/lib/python3.6/site-packages/django/db/models/options.py", line 733, in get_fields
return self._get_fields(include_parents=include_parents, include_hidden=include_hidden)
File "/home/henry/Documents/Sites/Development/autumna-dev/env/lib/python3.6/site-packages/django/db/models/options.py", line 793, in _get_fields
all_fields = self._relation_tree
File "/home/henry/Documents/Sites/Development/autumna-dev/env/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/henry/Documents/Sites/Development/autumna-dev/env/lib/python3.6/site-packages/django/db/models/options.py", line 706, in _relation_tree
return self._populate_directed_relation_graph()
File "/home/henry/Documents/Sites/Development/autumna-dev/env/lib/python3.6/site-packages/django/db/models/options.py", line 677, in _populate_directed_relation_graph
all_models = self.apps.get_models(include_auto_created=True)
File "/home/henry/Documents/Sites/Development/autumna-dev/env/lib/python3.6/site-packages/django/apps/registry.py", line 175, in get_models
self.check_models_ready()
File "/home/henry/Documents/Sites/Development/autumna-dev/env/lib/python3.6/site-packages/django/apps/registry.py", line 137, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

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

sync database to python django

I am trying to connect the SQLite3 database to Django. while I try to sync the database with a command
$ python manage.py syncdb
it shows a long trace. I don't know how to fix it. can anyone help me with that
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\__init__.py", line 357, in execute
django.setup()
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python35\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python35\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 985, in _gcd_import
File "<frozen importlib._bootstrap>", line 968, in _find_and_load
File "<frozen importlib._bootstrap>", line 957, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 697, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "F:\django\DatabaseConnectTest\polls\models.py", line 9, in <module>
class Choice(models.Model):
File "F:\django\DatabaseConnectTest\polls\models.py", line 10, in Choice
quesion = models.ForeignKey(Question)
TypeError: __init__() missing 1 required positional argument: 'on_delete'
The on_delete argument is required on all ForeignKey fields since Django 2.0. There's no longer a default value. You have to go through all your models and add this argument to relations.
The traceback will tell you exactly where to find the code that should be fixed.
File "F:\django\DatabaseConnectTest\polls\models.py", line 10, in Choice
quesion = models.ForeignKey(Question)
In most cases on_delete=models.CASCADE is what you want.
question = models.ForeignKey(Question, on_delete=models.CASCADE)
https://docs.djangoproject.com/en/2.1/ref/models/fields/#django.db.models.ForeignKey.on_delete

AttributeError when I import model from another app

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.