I am wishing to get all the names of view functions inside a form choice field.
I found an article of doing that in console but it is not working in forms.
The link to article is here
I changed the code slightly to fill my needs. I created a file named get_views_list.py and tried to import all_views_list
from M2.settings import ROOT_URLCONF
from django.core.urlresolvers import RegexURLPattern, RegexURLResolver
all_urlpatterns = __import__(ROOT_URLCONF).urls.urlpatterns
detail_views_list = []
def get_all_view_names(urlpatterns):
for pattern in urlpatterns:
if isinstance(pattern, RegexURLResolver):
get_all_view_names(pattern.url_patterns)
elif isinstance(pattern, RegexURLPattern):
detail_views_list.append(pattern.callback.__name__)
get_all_view_names(all_urlpatterns)
all_views_list = []
# remove redundant entries and specific ones we don't care about
for each in detail_views_list:
if each not in "serve add_view change_view changelist_view history_view delete_view RedirectView":
if each not in all_views_list:
all_views_list.append(each)
but this shows error
Unhandled exception in thread started by <function wrapper at 0x7f93f019cb90>
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 374, in check
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 361, in _run_checks
return checks.run_checks(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py", line 313, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py", line 306, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/niloy/PycharmProjects/M2/M2/urls.py", line 21, in <module>
url(r'^user/', include('UserAccessControl.urls')),
File "/usr/local/lib/python2.7/dist-packages/django/conf/urls/__init__.py", line 50, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/niloy/PycharmProjects/M2/UserAccessControl/urls.py", line 17, in <module>
from .views import *
File "/home/niloy/PycharmProjects/M2/UserAccessControl/views.py", line 13, in <module>
from get_views_list import all_views_list
File "/home/niloy/PycharmProjects/M2/UserAccessControl/get_views_list.py", line 4, in <module>
all_urlpatterns = __import__(ROOT_URLCONF).urls.urlpatterns
AttributeError: 'module' object has no attribute 'urls'
Related
I found this trick, which might be a hack, to set default values when the field is a foreign key to another database:
class House(models.Model):
name = models.CharField(max_length=100, unique=True)
address = models.CharField(max_length=300, null=True, blank=True)
def __str__(self):
return name
#classmethod
def get_default(cls):
return cls.objects.filter(address="5360 Wallaby Lane").first().pk
class Meta:
ordering = ["name"]
When constructing another class that references this field, we can set a default as follows:
class Neighborhood(models.Model):
favourite_house = models.ForeignKey("civilization.House", on_delete=models.PROTECT, default=House.get_default())
name = models.CharField(max_length=100, unique=True)
#classmethod
def get_default(cls):
return cls.objects.filter(name="Upper Snootington").first().pk
This works fine. As long as this function referencing a second layer isn't called by another default field for a table, Django doesn't complain. Once I tie this in as a default to another model, however:
class City(models.Model):
favourite_neighborhood = models.ForeignKey("civilization.Neighborhood", on_delete=models.PROTECT, default=Neighborhood.get_default())
Django throws exceptions "django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.". It seems this is pretty specific to that second layer of default retrieval. The best I can surmise is that Django is trying to interact with the models while it builds the models with that second layer referencing another model.
Something tells me this is a hack and there's a better way...
Error Stack Trace:
Traceback (most recent call last):
2022-10-04T19:47:25.097693165Z File "/usr/local/lib/python3.9/threading.py", line 973, in _bootstrap_inner
2022-10-04T19:47:25.097697202Z self.run()
2022-10-04T19:47:25.097700239Z File "/usr/local/lib/python3.9/threading.py", line 910, in run
2022-10-04T19:47:25.097724934Z self._target(*self._args, **self._kwargs)
2022-10-04T19:47:25.097732858Z File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper
2022-10-04T19:47:25.097736509Z fn(*args, **kwargs)
2022-10-04T19:47:25.097739522Z File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run
2022-10-04T19:47:25.097742666Z autoreload.raise_last_exception()
2022-10-04T19:47:25.097745656Z File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception
2022-10-04T19:47:25.097748771Z raise _exception[1]
2022-10-04T19:47:25.097751654Z File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 375, in execute
2022-10-04T19:47:25.097754680Z autoreload.check_errors(django.setup)()
2022-10-04T19:47:25.097757640Z File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper
2022-10-04T19:47:25.097760614Z fn(*args, **kwargs)
2022-10-04T19:47:25.097763440Z File "/usr/local/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
2022-10-04T19:47:25.097766409Z apps.populate(settings.INSTALLED_APPS)
2022-10-04T19:47:25.097769265Z File "/usr/local/lib/python3.9/site-packages/django/apps/registry.py", line 114, in populate
2022-10-04T19:47:25.097772386Z app_config.import_models()
2022-10-04T19:47:25.097775245Z File "/usr/local/lib/python3.9/site-packages/django/apps/config.py", line 301, in import_models
2022-10-04T19:47:25.097814770Z self.models_module = import_module(models_module_name)
2022-10-04T19:47:25.097822684Z File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
2022-10-04T19:47:25.097827905Z return _bootstrap._gcd_import(name[level:], package, level)
2022-10-04T19:47:25.097833111Z File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
2022-10-04T19:47:25.101634487Z File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
2022-10-04T19:47:25.103934434Z File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
2022-10-04T19:47:25.105839169Z File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
2022-10-04T19:47:25.107814611Z File "<frozen importlib._bootstrap_external>", line 850, in exec_module
2022-10-04T19:47:25.109409635Z File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
2022-10-04T19:47:25.111132100Z File "/code/civilization/models.py", line 102, in <module>
2022-10-04T19:47:25.112666303Z class Neighborhood(models.Model):
2022-10-04T19:47:25.112769078Z File "/code/civilization/models.py", line 105, in City
2022-10-04T19:47:25.114428690Z on_delete=models.PROTECT, default= get_default_neighborhood())
2022-10-04T19:47:25.114479112Z File "/code/civilization/models.py", line 37, in get_default_neighborhood
2022-10-04T19:47:25.115610331Z return Neighborhood.get_default()
2022-10-04T19:47:25.115733686Z File "/code/civilization/models.py", line 29, in get_default
2022-10-04T19:47:25.116830129Z return cls.objects.filter(address="5360 Wallaby Lane").first().pk
2022-10-04T19:47:25.116868823Z File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 674, in first
2022-10-04T19:47:25.119852074Z for obj in (self if self.ordered else self.order_by('pk'))[:1]:
2022-10-04T19:47:25.119907893Z File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 280, in __iter__
2022-10-04T19:47:25.119917143Z self._fetch_all()
2022-10-04T19:47:25.119920693Z File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 1324, in _fetch_all
2022-10-04T19:47:25.119923803Z self._result_cache = list(self._iterable_class(self))
2022-10-04T19:47:25.119926782Z File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 51, in __iter__
2022-10-04T19:47:25.119931046Z results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
2022-10-04T19:47:25.119935039Z File "/usr/local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1162, in execute_sql
2022-10-04T19:47:25.119938474Z sql, params = self.as_sql()
2022-10-04T19:47:25.119941476Z File "/usr/local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 513, in as_sql
2022-10-04T19:47:25.119957520Z extra_select, order_by, group_by = self.pre_sql_setup()
2022-10-04T19:47:25.119960883Z File "/usr/local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 56, in pre_sql_setup
2022-10-04T19:47:25.119963826Z order_by = self.get_order_by()
2022-10-04T19:47:25.119966609Z File "/usr/local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 356, in get_order_by
2022-10-04T19:47:25.119969517Z order_by.extend(self.find_ordering_name(
2022-10-04T19:47:25.119972298Z File "/usr/local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 740, in find_ordering_name
2022-10-04T19:47:25.119975219Z field, targets, alias, joins, path, opts, transform_function = self._setup_joins(pieces, opts, alias)
2022-10-04T19:47:25.119978235Z File "/usr/local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 780, in _setup_joins
2022-10-04T19:47:25.119981145Z field, targets, opts, joins, path, transform_function = self.query.setup_joins(pieces, opts, alias)
2022-10-04T19:47:25.119984116Z File "/usr/local/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1648, in setup_joins
2022-10-04T19:47:25.120132662Z path, final_field, targets, rest = self.names_to_path(
2022-10-04T19:47:25.120632399Z File "/usr/local/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1539, in names_to_path
2022-10-04T19:47:25.121907397Z if field.is_relation and not field.related_model:
2022-10-04T19:47:25.122421805Z File "/usr/local/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
2022-10-04T19:47:25.123109833Z res = instance.__dict__[self.name] = self.func(instance)
2022-10-04T19:47:25.123140347Z File "/usr/local/lib/python3.9/site-packages/django/db/models/fields/related.py", line 95, in related_model
2022-10-04T19:47:25.123568285Z apps.check_models_ready()
2022-10-04T19:47:25.123651924Z File "/usr/local/lib/python3.9/site-packages/django/apps/registry.py", line 141, in check_models_ready
2022-10-04T19:47:25.124090201Z raise AppRegistryNotReady("Models aren't loaded yet.")
2022-10-04T19:47:25.124122833Z django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
this is my error. I don't know what's my error is. And I really do not know what to do right now. I am using PyTorch-lightning with tensorboard. I do not where this error comes from, I can provide more details if you ask. I delete several error information because StackOverflow doesn't allow me to post so much code.
File "/home/jq/PycharmProjects/Unet/Code/Lit_train.py", line 49, in <module>
trainer.fit(model)
File "/home/jq/.local/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 859, in fit
self.single_gpu_train(model)
File "/home/jq/.local/lib/python3.6/site-packages/pytorch_lightning/trainer/distrib_parts.py", line 503, in single_gpu_train
self.run_pretrain_routine(model)
File "/home/jq/.local/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 1015, in run_pretrain_routine
self.train()
File "/home/jq/.local/lib/python3.6/site-packages/pytorch_lightning/trainer/training_loop.py", line 347, in train
self.run_training_epoch()
File "/home/jq/.local/lib/python3.6/site-packages/pytorch_lightning/trainer/training_loop.py", line 451, in run_training_epoch
self.run_evaluation(test_mode=self.testing)
File "/home/jq/.local/lib/python3.6/site-packages/pytorch_lightning/trainer/evaluation_loop.py", line 391, in run_evaluation
self.log_metrics(log_metrics, {})
File "/home/jq/.local/lib/python3.6/site-packages/pytorch_lightning/trainer/logging.py", line 74, in log_metrics
self.logger.save()
File "/home/jq/.local/lib/python3.6/site-packages/pytorch_lightning/utilities/distributed.py", line 10, in wrapped_fn
return fn(*args, **kwargs)
File "/home/jq/.local/lib/python3.6/site-packages/pytorch_lightning/loggers/tensorboard.py", line 161, in save
save_hparams_to_yaml(hparams_file, self.hparams)
File "/home/jq/.local/lib/python3.6/site-packages/pytorch_lightning/core/saving.py", line 151, in save_hparams_to_yaml
yaml.dump(hparams, fp)
File "/home/jq/.local/lib/python3.6/site-packages/yaml/__init__.py", line 290, in dump
return dump_all([data], stream, Dumper=Dumper, **kwds)
File "/home/jq/.local/lib/python3.6/site-packages/yaml/__init__.py", line 278, in dump_all
dumper.represent(data)
File "/home/jq/.local/lib/python3.6/site-packages/yaml/representer.py", line 27, in represent
node = self.represent_data(data)
File "/home/jq/.local/lib/python3.6/site-packages/yaml/representer.py", line 48, in represent_data
node = self.yaml_representers[data_types[0]](self, data)
File "/home/jq/.local/lib/python3.6/site-packages/yaml/representer.py", line 207, in represent_dict
return self.represent_mapping('tag:yaml.org,2002:map', data)
File "/home/jq/.local/lib/python3.6/site-packages/yaml/representer.py", line 118, in represent_mapping
node_value = self.represent_data(item_value)
File "/home/jq/.local/lib/python3.6/site-packages/yaml/representer.py", line 52, in represent_data
node = self.yaml_multi_representers[data_type](self, data)
File "/home/jq/.local/lib/python3.6/site-packages/yaml/representer.py", line 343, in represent_object
'tag:yaml.org,2002:python/object:'+function_name, state)
File "/home/jq/.local/lib/python3.6/site-packages/yaml/representer.py", line 118, in represent_mapping
node_value = self.represent_data(item_value)
File "/home/jq/.local/lib/python3.6/site-packages/yaml/representer.py", line 52, in represent_data
node = self.yaml_multi_representers[data_type](self, data)
File "/home/jq/.local/lib/python3.6/site-packages/yaml/representer.py", line 346, in represent_object
return self.represent_sequence(tag+function_name, args)
File "/home/jq/.local/lib/python3.6/site-packages/yaml/representer.py", line 92, in represent_sequence
node_item = self.represent_data(item)
File "/home/jq/.local/lib/python3.6/site-packages/yaml/representer.py", line 52, in represent_data
node = self.yaml_multi_representers[data_type](self, data)
File "/home/jq/.local/lib/python3.6/site-packages/yaml/representer.py", line 343, in represent_object
File "/home/jq/.local/lib/python3.6/site-packages/yaml/representer.py", line 52, in represent_data
node = self.yaml_multi_representers[data_type](self, data)
File "/home/jq/.local/lib/python3.6/site-packages/yaml/representer.py", line 317, in represent_object
reduce = data.__reduce_ex__(2)
TypeError: can't pickle _thread.lock objects
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
File "/home/jq/.local/lib/python3.6/site-packages/tqdm/std.py", line 1086, in __del__
File "/home/jq/.local/lib/python3.6/site-packages/tqdm/std.py", line 1293, in close
File "/home/jq/.local/lib/python3.6/site-packages/tqdm/std.py", line 1471, in display
File "/home/jq/.local/lib/python3.6/site-packages/tqdm/std.py", line 1089, in __repr__
File "/home/jq/.local/lib/python3.6/site-packages/tqdm/std.py", line 1433, in format_dict
TypeError: 'NoneType' object is not iterable
I am not able to import from rest_framework.exceptions import ApiException
This is my Exception.py:
class APIException(Exception):
status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
default_detail = _('A server error occurred.')
default_code = 'error'
def __init__(self, detail=None, code=None):
if detail is None:
detail = self.default_detail
if code is None:
code = self.default_code
self.detail = _get_error_details(detail, code)
def __str__(self):
return self.detail
def get_codes(self):
return _get_codes(self.detail)
def get_full_details(self):
return _get_full_details(self.detail)
error while running : python manage.py makemigrations networscanners
Traceback (most recent call last):
File "manage.py", line 25, in <module>
execute_from_command_line(sys.argv)
File "D:\Python36-32\lib\site-packages\django\core\management\__init__.py", li
ne 381, in execute_from_command_line
utility.execute()
File "D:\Python36-32\lib\site-packages\django\core\management\__init__.py", li
ne 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\Python36-32\lib\site-packages\django\core\management\base.py", line 3
23, in run_from_argv
self.execute(*args, **cmd_options)
File "D:\Python36-32\lib\site-packages\django\core\management\base.py", line 3
61, in execute
self.check()
File "D:\Python36-32\lib\site-packages\django\core\management\base.py", line 3
90, in check
include_deployment_checks=include_deployment_checks,
File "D:\Python36-32\lib\site-packages\django\core\management\base.py", line 3
77, in _run_checks
return checks.run_checks(**kwargs)
File "D:\Python36-32\lib\site-packages\django\core\checks\registry.py", line 7
2, in run_checks
new_errors = check(app_configs=app_configs)
File "D:\Python36-32\lib\site-packages\django\core\checks\urls.py", line 13, i
n check_url_config
return check_resolver(resolver)
File "D:\Python36-32\lib\site-packages\django\core\checks\urls.py", line 23, i
n check_resolver
return check_method()
File "D:\Python36-32\lib\site-packages\django\urls\resolvers.py", line 400, in
check
for pattern in self.url_patterns:
File "D:\Python36-32\lib\site-packages\django\utils\functional.py", line 80, i
n __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\Python36-32\lib\site-packages\django\urls\resolvers.py", line 585, in
url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "D:\Python36-32\lib\site-packages\django\utils\functional.py", line 80, i
n __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\Python36-32\lib\site-packages\django\urls\resolvers.py", line 578, in
urlconf_module
return import_module(self.urlconf_name)
File "D:\Python36-32\lib\importlib\__init__.py", line 127, 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:\archerysec framework\archerysec-master\archerysecurity\urls.py", line
36, in <module>
from rest_framework_jwt.views import obtain_jwt_token, verify_jwt_token
File "D:\Python36-32\lib\site-packages\rest_framework_jwt\views.py", line 1, i
n <module>
from rest_framework.views import APIView
File "D:\Python36-32\lib\site-packages\rest_framework\views\__init__.py", line
1, in <module>
from .base import BaseApiView
File "D:\Python36-32\lib\site-packages\rest_framework\views\base.py", line 5,
in <module>
from rest_framework.exceptions import ApiException
ImportError: cannot import name 'ApiException'
It's APIException (rest_framework.exceptions.APIException), not ApiException.
When I am executing celery task it is giving me:
ValueError: Related model u'user.User' cannot be resolved
The stacktrace is
Traceback (most recent call last):
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/celery/app/trace.py", line 375, in trace_task
R = retval = fun(*args, **kwargs)
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/celery/app/trace.py", line 632, in __protected_call__
return self.run(*args, **kwargs)
File "/Users/prince/work/magneto/set/facebook_pages/tasks.py", line 23, in analyze_page
connected_facebook_page = get_connected_facebook_page(connected_facebook_page_id)
File "/Users/prince/work/magneto/set/facebook_pages/utils.py", line 49, in get_connected_facebook_page
return ConnectedUserPage.objects.get(id=connected_facebook_page_id)
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/query.py", line 374, in get
num = len(clone)
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/query.py", line 232, in __len__
self._fetch_all()
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/query.py", line 1118, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 876, in execute_sql
sql, params = self.as_sql()
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 428, in as_sql
extra_select, order_by, group_by = self.pre_sql_setup()
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 46, in pre_sql_setup
self.setup_query()
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 37, in setup_query
self.select, self.klass_info, self.annotation_col_map = self.get_select()
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 194, in get_select
for c in self.get_default_columns():
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 569, in get_default_columns
column = field.get_col(alias)
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1008, in get_col
return super(ForeignKey, self).get_col(alias, output_field or self.target_field)
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/fields/related.py", line 909, in target_field
return self.foreign_related_fields[0]
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/fields/related.py", line 653, in foreign_related_fields
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/fields/related.py", line 640, in related_fields
self._related_fields = self.resolve_related_fields()
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/fields/related.py", line 625, in resolve_related_fields
raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
ValueError: Related model u'user.User' cannot be resolved
Here ConnectedUserPage is a model with schema:
class ConnectedUserPage(TimeStampedModel):
user = models.ForeignKey('user.User', on_delete=models.PROTECT)
page = models.ForeignKey(FacebookPage, on_delete=models.PROTECT)
page_details = jsonb.JSONField()
My versions are :
celery==4.1.1
django-celery-beat==1.1.1
django-celery-results==1.0.1
Django==1.11.13
In case I directly import User model from user app I am getting stuck in circular imports.
Any help would be appreciated, stuck in this loop for a quite while now.
Where's User defined? Is this Django's own user model? If that's the case you can use User model directly:
from django.contrib.auth import get_user_model
User = get_user_model()
class ConnectedUserPage(TimeStampedModel):
user = models.ForeignKey(User, unique=True)
or you can also do
from django.conf import settings
class ConnectedUserPage(TimeStampedModel):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
If it is your own supplied User model, are you sure user is in INSTALLED_APPS?
I got this error while running my app in command line.
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 280, in execute
translation.activate('en-us')
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py", line 130, in activate
return _trans.activate(language)
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 188, in activate
_active.value = translation(language)
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 177, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 159, in _fetch
app = import_module(appname)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/__init__.py", line 6, in <module>
from django.contrib.admin.sites import AdminSite, site
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py", line 4, in <module>
from django.contrib.admin.forms import AdminAuthenticationForm
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/forms.py", line 6, in <module>
from django.contrib.auth.forms import AuthenticationForm
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/forms.py", line 17, in <module>
from django.contrib.auth.models import User
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/models.py", line 48, in <module>
class Permission(models.Model):
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 96, in __new__
new_class.add_to_class('_meta', Options(meta, **kwargs))
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 264, in add_to_class
value.contribute_to_class(cls, name)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py", line 124, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 34, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 199, in __getitem__
conn = backend.DatabaseWrapper(db, alias)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/gis/db/backends/postgis/base.py", line 11, in __init__
self.ops = PostGISOperations(self)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/gis/db/backends/postgis/operations.py", line 158, in __init__
if self.spatial_version < (1, 3, 4):
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 49, in __get__
res = instance.__dict__[self.func.__name__] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/gis/db/backends/postgis/operations.py", line 252, in spatial_version
vtup = self.postgis_version_tuple()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/gis/db/backends/postgis/operations.py", line 426, in postgis_version_tuple
version = self.postgis_lib_version()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/gis/db/backends/postgis/operations.py", line 406, in postgis_lib_version
return self._get_postgis_func('postgis_lib_version')
File "/usr/local/lib/python2.7/dist-packages/django/contrib/gis/db/backends/postgis/operations.py", line 396, in _get_postgis_func
with self.connection.temporary_connection() as cursor:
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 515, in temporary_connection
cursor = self.cursor()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 157, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 129, in _cursor
self.ensure_connection()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 124, in ensure_connection
self.connect()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 115, in connect
self.set_autocommit(True)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 333, in set_autocommit
self._set_autocommit(autocommit)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py", line 172, in _set_autocommit
if self.psycopg2_version >= (2, 4, 2):
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 49, in __get__
res = instance.__dict__[self.func.__name__] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py", line 201, in psycopg2_version
return tuple(int(v) for v in version.split('.'))
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py", line 201, in <genexpr>
return tuple(int(v) for v in version.split('.'))
I installed the necessary modules. I am using Django version 1.6. The same app runs smoothly in my windows laptop but when I migrated it in a Linux OS, the errors shows.