in Djando,When I tried to import my model into my view.py,an error occured - django-models

When I tried to import my model into my view.py:
from django.views import generic
from .models import *
class ProductListView(generic.ListView):
template_name="discover.html"
queryset=Product.objects.all()
it showed an error as follow:
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/Users/yorkli/Desktop/django-projects/gumroad/djgumroad/config/urls.py", line 9, in <module>
from djgumroad.products.views import *
File "/Users/yorkli/Desktop/django-projects/gumroad/djgumroad/djgumroad/products/views.py", line 2, in <module>
from .models import *
File "/Users/yorkli/Desktop/django-projects/gumroad/djgumroad/djgumroad/products/models.py", line 5, in <module>
class Product(models.Model):
File "/Users/yorkli/Desktop/django-projects/gumroad/venv/lib/python3.10/site-packages/django/db/models/base.py", line 132, in __new__
raise RuntimeError(
RuntimeError: Model class djgumroad.products.models.Product doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
but I did register the app in the INSTALLED_APPS:
LOCAL_APPS = [
"djgumroad.users",
# Your stuff: custom apps go here
"tailwind",
"theme",
"django_browser_reload",
"djgumroad.products",
]
# https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS
Is there other configurations I missed?
I tried to remove the "from .models import Product",it works well,when I put it back,the error shows again.

Related

The DataStream.map() function in Flink Python API doesn't work

I'm trying Flink's DataStream API in Python.The env is:
flink: 1.16.0
python package: apache-flink==1.16.0
My code was:
env = StreamExecutionEnvironment.get_execution_environment()
env.set_parallelism(1)
env.enable_checkpointing(1000)
source = KafkaSource.builder() \
.set_bootstrap_servers('xxx') \
.set_topics("xxx") \
.set_group_id("xxx") \
.set_starting_offsets(KafkaOffsetsInitializer.earliest()) \
.set_value_only_deserializer(SimpleStringSchema()) \
.set_property('commit.offsets.on.checkpoint', 'true') \
.build()
stream = env.from_source(source, WatermarkStrategy.no_watermarks(), "team_config_source")
sink = FileSink \
.for_row_format('/opt/result/', Encoder.simple_string_encoder("UTF-8")) \
.with_output_file_config(OutputFileConfig.builder()
.with_part_prefix("team_config")
.with_part_suffix(".json")
.build()) \
.with_rolling_policy(RollingPolicy.default_rolling_policy(part_size=1024 ** 3, rollover_interval=15 * 60 * 1000,
inactivity_interval=5 * 60 * 1000)) \
.build()
def mapping(data):
return data
stream.map(mapping, BasicTypeInfo.STRING_TYPE_INFO()).sink_to(sink)
env.execute()
But Flink gives me this error:
2023-01-18 11:34:34 Traceback (most recent call last):
2023-01-18 11:34:34 File "/usr/local/lib/python3.8/runpy.py", line 194, in _run_module_as_main
2023-01-18 11:34:34 return _run_code(code, main_globals, None,
2023-01-18 11:34:34 File "/usr/local/lib/python3.8/runpy.py", line 87, in _run_code
2023-01-18 11:34:34 exec(code, run_globals)
2023-01-18 11:34:34 File "/tmp/pyflink/00606d52-b6c1-4e13-b7cb-73ee8e196db6/42be79fb-c8bb-4de1-b0fb-c89a7702cddc/flink_driver.py", line 223, in <module>
2023-01-18 11:34:34 process2()
2023-01-18 11:34:34 File "/tmp/pyflink/00606d52-b6c1-4e13-b7cb-73ee8e196db6/42be79fb-c8bb-4de1-b0fb-c89a7702cddc/flink_driver.py", line 218, in process2
2023-01-18 11:34:34 stream.map(mapping, BasicTypeInfo.STRING_TYPE_INFO()).sink_to(sink)
2023-01-18 11:34:34 File "/opt/flink/opt/python/pyflink.zip/pyflink/datastream/data_stream.py", line 312, in map
2023-01-18 11:34:34 File "/opt/flink/opt/python/pyflink.zip/pyflink/datastream/data_stream.py", line 654, in process
2023-01-18 11:34:34 File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2023-01-18 11:34:34 File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
2023-01-18 11:34:34 File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
2023-01-18 11:34:34 File "<frozen importlib._bootstrap>", line 618, in _load_backward_compatible
2023-01-18 11:34:34 File "<frozen zipimport>", line 259, in load_module
2023-01-18 11:34:34 File "/opt/flink/opt/python/pyflink.zip/pyflink/fn_execution/flink_fn_execution_pb2.py", line 38, in <module>
2023-01-18 11:34:34 AttributeError: 'NoneType' object has no attribute 'message_types_by_name'
It seems this exception was thrown in flink_fn_execution_pb2.py file, so I check the code in the package. The code at the beginning of this file was:
DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile('xxx')
_INPUT = DESCRIPTOR.message_types_by_name['Input']
But I also find that the function AddSerializedFile() only returns None. This function in descriptor_pool.py was:
def AddSerializedFile(self, serialized_file_desc_proto):
"""Adds the FileDescriptorProto and its types to this pool.
Args:
serialized_file_desc_proto (bytes): A bytes string, serialization of the
:class:`FileDescriptorProto` to add.
"""
# pylint: disable=g-import-not-at-top
from google.protobuf import descriptor_pb2
file_desc_proto = descriptor_pb2.FileDescriptorProto.FromString(
serialized_file_desc_proto)
self.Add(file_desc_proto)
So the DESCRIPTOR in flink_fn_execution_pb2.py is always None, the map() and flat_map() functions always failed with this exception.
Can anyone help with this problem? Am I using map() in wrong way or it's a bug?

__init__.py for hy modules with relative imports

How do I require all functions in a hy module in an __init__.py? I tried to use a similar model as in the hyrule __init__.py, but am getting the following error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
File "/nix/store/73jg56dr425ycv9p746arzf13m52q2n3-python3-3.10.4/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/nix/store/6gf7zzwc1ipf4pcissjmi6p7na9l0awk-python3.10-oreo-1.0.0.0/lib/python3.10/site-packages/oreo/__init__.py", line 7, in <module>
hy.macros.require('.oreo',
File "/nix/store/p32f25fn29070pphsrfj3pyywqsv1gph-python3.10-hy-0.24.0/lib/python3.10/site-packages/hy/macros.py", line 241, in require
source_module = import_module_from_string(source_module, target_module)
File "/nix/store/p32f25fn29070pphsrfj3pyywqsv1gph-python3.10-hy-0.24.0/lib/python3.10/site-packages/hy/macros.py", line 165, in import_module_from_string
return importlib.import_module(module_name, package)
File "/nix/store/73jg56dr425ycv9p746arzf13m52q2n3-python3-3.10.4/lib/python3.10/importlib/__init__.py", line 121, in import_module
raise TypeError(msg.format(name))
TypeError: the 'package' argument is required to perform a relative import for '.oreo'
This is what's in my __init__.py:
import hy
hy.macros.require('.oreo',
# The Python equivalent of `(require oreo *)`
None, assignments = 'ALL', prefix = '')
hy.macros.require_reader('.oreo', None, assignments = 'ALL')
from .oreo import *
This is my general directory structure:
.
├── oreo
│ ├── __init__.py
│ ├── oreo.hy
├── poetry.lock
├── setup.py
└── pyproject.toml
And this is my pyproject.toml:
[tool.poetry]
name = "oreo"
version = "1.0.0.0"
description = "The Stuffing for Other Functions!"
authors = ["sylvorg <jeet.ray#syvl.org>"]
license = "OREO"
include = [
"oreo/**/*.py",
"oreo/**/*.hy",
]
[tool.poetry.dependencies]
python = "^3.9"
addict = "*"
autoslot = "*"
click = "*"
hy = "^0.24.0"
hyrule = "*"
nixpkgs = "*"
more-itertools = "*"
rich = { git = "https://github.com/syvlorg/rich.git", branch = "master" }
toolz = "*"
[tool.poetry.dev-dependencies]
pytest = "^3.0"
poetry = "*"
poetry2setup = "*"
pytest-hy = "*"
pytest-randomly = "*"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
The real answer is that hy.macros.require and hy.macros.require_reader are private functions, which could disappear or get backwards-incompatibly changed without notice. There's no official way to require macros from Python. For plain macros, the thing to do is just delegate to Hy: hy.eval(hy.read('(require hyrule.hy-init *)')). For reader macros, hy.eval(hy.read('(require hyrule.hy-init :readers *)')) doesn't currently work, but this is probably a bug (#2291).

kiwi-tcms: kiwi_db restarting loop

I am trying to install kiwi-tcms and when I get to step:
docker exec -it kiwi_web /Kiwi/manage.py initial_setup
D:\path\to\kiwi-tcms>docker exec -it kiwi_web /Kiwi/manage.py initial_setup
Applying migrations:
Traceback (most recent call last):
File "/venv/lib64/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/venv/lib64/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/venv/lib64/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "/venv/lib64/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/venv/lib64/python3.8/site-packages/django/db/backends/mysql/base.py", line 234, in get_new_connection
connection = Database.connect(**conn_params)
File "/venv/lib64/python3.8/site-packages/MySQLdb/init.py", line 130, in Connect
return Connection(*args, **kwargs)
File "/venv/lib64/python3.8/site-packages/MySQLdb/connections.py", line 185, in init
super().init(*args, **kwargs2)
MySQLdb._exceptions.OperationalError: (2005, "Unknown MySQL server host 'db' (-2)")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Kiwi/manage.py", line 12, in
execute_from_command_line(sys.argv)
File "/venv/lib64/python3.8/site-packages/django/core/management/init.py", line 419, in execute_from_command_line
utility.execute()
File "/venv/lib64/python3.8/site-packages/django/core/management/init.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/venv/lib64/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "/venv/lib64/python3.8/site-packages/django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
File "/venv/lib64/python3.8/site-packages/tcms/core/management/commands/initial_setup.py", line 11, in handle
call_command("migrate", "--verbosity=%i" % kwargs["verbosity"])
File "/venv/lib64/python3.8/site-packages/django/core/management/init.py", line 181, in call_command
return command.execute(*args, **defaults)
File "/venv/lib64/python3.8/site-packages/django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
File "/venv/lib64/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
File "/venv/lib64/python3.8/site-packages/django/core/management/commands/migrate.py", line 92, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/venv/lib64/python3.8/site-packages/django/db/migrations/executor.py", line 18, in init
self.loader = MigrationLoader(self.connection)
File "/venv/lib64/python3.8/site-packages/django/db/migrations/loader.py", line 53, in init
self.build_graph()
File "/venv/lib64/python3.8/site-packages/django/db/migrations/loader.py", line 220, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/venv/lib64/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
if self.has_table():
File "/venv/lib64/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
with self.connection.cursor() as cursor:
File "/venv/lib64/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/venv/lib64/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
return self._cursor()
File "/venv/lib64/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor
self.ensure_connection()
File "/venv/lib64/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/venv/lib64/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/venv/lib64/python3.8/site-packages/django/db/utils.py", line 90, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/venv/lib64/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/venv/lib64/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/venv/lib64/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "/venv/lib64/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/venv/lib64/python3.8/site-packages/django/db/backends/mysql/base.py", line 234, in get_new_connection
connection = Database.connect(**conn_params)
File "/venv/lib64/python3.8/site-packages/MySQLdb/init.py", line 130, in Connect
return Connection(*args, **kwargs)
File "/venv/lib64/python3.8/site-packages/MySQLdb/connections.py", line 185, in init
super().init(*args, **kwargs2)
django.db.utils.OperationalError: (2005, "Unknown MySQL server host 'db' (-2)")
I'm working windows 10. I also have the kiwi_db that is constantly restarted in docker
Unknown MySQL server host 'db' (-2)"
The error message itself is clear enough. Your DB server doesn't seem to be up and running.
I'm working windows 10. I also have the kiwi_db that is constantly restarted in docker
Kiwi TCMS and MySQL/MariaDB are Linux based containers so maybe your Windows host is not capable of running Linux containers in the first place. Refer to Docker's documentation/support on that matter.
This may be of help but fair warning that it's been written by a 3rd party not affiliated with the Kiwi TCMS team:
https://medium.com/#siriwardhane.yuwin/running-kiwi-tcms-as-a-docker-container-in-windows-10-home-82d74b107202

Configurate MSSQL Server with Django

I have installed
**appdirs==1.4.3
asgiref==3.2.7
distlib==0.3.0
Django==2.1.15
django-mssql==1.8
django-mssql-backend==2.8.1
django-pyodbc-azure==2.1.0.0
filelock==3.0.12
pyodbc==4.0.30
pytz==2019.3
six==1.14.0
sqlparse==0.3.1
virtualenv==20.0.18
virtualenvwrapper-win==1.2.6**
My settings.py file in the database section looks like this
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'localhost',
'HOST': 'localhost',
'USER': 'sa',
'PASSWORD': 'root*+123456*+789',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
}
}
}
I got the following error after run py manage.py migrate
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\utils.py", line 167, in ensure_defaults
conn = self.databases[alias]
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\functional.py", line 37, in get
res = instance.dict[self.name] = self.func(instance)
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\utils.py", line 154, in databases
if self._databases[DEFAULT_DB_ALIAS] == {}:
KeyError: 'default'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 357, in execute
django.setup()
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\z003vuxz\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:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\contrib\auth\models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\contrib\auth\base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\base.py", line 101, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\base.py", line 305, in add_to_class
value.contribute_to_class(cls, name)
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\options.py", line 203, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\__init__.py", line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\utils.py", line 199, in __getitem__
self.ensure_defaults(alias)
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\utils.py", line 169, in ensure_defaults
raise ConnectionDoesNotExist("The connection %s doesn't exist" % alias)
django.db.utils.ConnectionDoesNotExist: The connection default doesn't exist
Can somebody help me?
Your DATABASES-setting is incorrect, see docs for details.
What you need is:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
The configuration for the connection needs to be inside of 'default': {...}, you placed it directly inside of DATABASES.

error FileNotOpenedError

I obtain a error when try to write a file in cloud storage
my code:
my_file = files.gs.create('/gs/foo/myfile')
with files.open(my_file, 'a') as f:
f.write('Hello World!')
f.write('Hello World Again!')
Traceback (most recent call last):
File "C:\Datos\proyectos\dropbox\Proyectos\as3\semtable\remote_api\using_datastore.py", line 63, in <module>
f.write('Hello World Again!')
File "C:\Program Files\Google\google_appengine\google\appengine\api\files\file.py", line 281, in __exit__
self.close()
File "C:\Program Files\Google\google_appengine\google\appengine\api\files\file.py", line 275, in close
self._make_rpc_call_with_retry('Close', request, response)
File "C:\Program Files\Google\google_appengine\google\appengine\api\files\file.py", line 388, in _make_rpc_call_with_retry
_make_call(method, request, response)
File "C:\Program Files\Google\google_appengine\google\appengine\api\files\file.py", line 236, in _make_call
_raise_app_error(e)
File "C:\Program Files\Google\google_appengine\google\appengine\api\files\file.py", line 179, in _raise_app_error
raise FileNotOpenedError()
google.appengine.api.files.file.FileNotOpenedError

Resources