Django cannot import name (model) - python

I have two files under this app that are messing up. One updates the cache and the other is my models.py file. For some reason I get ImportError: cannot import name 'Cache' Whenever I try to load the server. Moving the Cache definition above the NavigationItem definition worked for one test boot but has failed every time since.
navbar/models.py
from django.db import models
from navbar.generator import update_navigation
from datetime import datetime
import logging
class NavigationItem(models.Model):
# The title used in the navar
title = models.CharField(max_length=25, blank=False)
# The dir it points to
dir = models.ForeignKey('library.Dir', blank=True, null=True)
# The level above it
previous_level = models.ForeignKey('NavigationItem', on_delete=models.SET_NULL, null=True, blank=True)
# Is it on the top level
top_level = models.BooleanField(default=False)
# Shortcut
show_all_subdirs = models.BooleanField(default=False)
# position on the list
position = models.SmallIntegerField(default=15)
# last modified:
last_modified = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
def __repr__(self):
return "({}) {}".format(self.pk, self.title)
def save(self, *args, **kwargs):
super(NavigationItem, self).save(*args, **kwargs)
logging.INFO("Saved Navbar at " + datetime.now())
update_navigation()
class Cache(models.Model):
key = models.CharField(max_length=10, default="key", unique=True)
data = models.TextField()
timestamp = models.DateTimeField(auto_now=True)
navbar/generator.py
from navbar.models import Cache, NavigationItem
from datetime import datetime
import logging
def render_navigation():
query = Cache.objects.filter(key="nav")
if query.count() > 0:
return query[0].data
else:
return update_navigation()
def update_navigation():
# Strings used in the generation of templates
dropdown = '''<li>
<a class="dropdown-toggle" data-toggle="dropdown" href="{}">{} <span class="caret"></span></a>
<ul class="dropdown-menu">
{}
</ul>
</li>
'''
link = '<li>{}</li>'
# inner functions
def generate_navigation(navigation_items):
# The string to be returned
result = ""
# for every item in the list that we get
for item in navigation_items:
# if the item points somewhere get the path otherwise make a hash
path = item.dir.path if item.dir else "#"
# if it has sub-navigation items
if item.navigationitem_set.count() > 0:
# the query below will get all the navigation items under this one sorted properly
query = item.navigationitem_set.all().order_by('position')
result += dropdown.format(path, item.title, generate_navigation(query))
elif item.show_all_subdirs and item.dir is not None:
result += render_subdirs(item.dir)
else:
result += link.format(path, item.title)
return result
def render_subdirs(directory):
result = ""
for folder in directory.dir_set.all().order_by('title'):
if folder.dir_set.count() > 0:
result += dropdown.format(folder.path, folder.title, render_subdirs(folder))
else:
result += link.format(folder.path, folder.title)
return result
# The meat of the function
# Log the timestamps for debugging and logistics
logging.INFO("Navbar Render Begun at " + datetime.now())
query = NavigationItem.objects.filter(top_level=True).order_by('position')
result = generate_navigation(query)
logging.INFO("Navbar Rendered at " + datetime.now())
if Cache.objects.filter(key="nav").count() > 0:
cache = Cache.objects.get(key="nav")
cache.data = result # WTF HOW DID I FORGET THIS???
else:
cache = Cache(key="nav", data=result)
cache.save()
logging.INFO("Navbar Saved at: " + datetime.now())
return result
Full error:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Program Files\Python 3.5\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
utility.execute()
File "C:\Program Files\Python 3.5\lib\site-packages\django\core\management\__init__.py", line 341, in execute
django.setup()
File "C:\Program Files\Python 3.5\lib\site-packages\django\__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Program Files\Python 3.5\lib\site-packages\django\apps\registry.py", line 108, in populate
app_config.import_models(all_models)
File "C:\Program Files\Python 3.5\lib\site-packages\django\apps\config.py", line 199, in import_models
self.models_module = import_module(models_module_name)
File "C:\Program Files\Python 3.5\lib\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 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "C:\Users\User\project\navbar\models.py", line 2, in <module>
from navbar.generator import update_navigation
File "C:\Users\User\project\navbar\generator.py", line 1, in <module>
from navbar.models import Cache, NavigationItem
ImportError: cannot import name 'Cache'

That's normal as you're having a circular import (models.py importing from generator.py and vise-versa)
Try to use method import inside your models then it should be fixed:
class NavigationItem(models.Model):
...
def save(self, *args, **kwargs):
# Method level import to avoid circular imports
from .navigation import update_navigation
super(NavigationItem, self).save(*args, **kwargs)
logging.INFO("Saved Navbar at " + datetime.now())
update_navigation()

Related

ImportError: cannot import name 'djongo_access_url' from 'djongo'

I am getting the following error using Djongo with Mongodb in a django server:
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
self.run()
File "/usr/lib/python3.10/threading.py", line 946, in run
self._target(*self._args, **self._kwargs)
File "/home/cchilders/.local/lib/python3.10/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/home/cchilders/.local/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "/home/cchilders/.local/lib/python3.10/site-packages/django/core/management/base.py", line 392, in check
all_issues = checks.run_checks(
File "/home/cchilders/.local/lib/python3.10/site-packages/django/core/checks/registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/home/cchilders/.local/lib/python3.10/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/home/cchilders/.local/lib/python3.10/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/home/cchilders/.local/lib/python3.10/site-packages/django/urls/resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "/home/cchilders/.local/lib/python3.10/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/cchilders/.local/lib/python3.10/site-packages/django/urls/resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/cchilders/.local/lib/python3.10/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/cchilders/.local/lib/python3.10/site-packages/django/urls/resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/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 "/home/cchilders/projects/stocks_backend/dividends_project/urls.py", line 23, in <module>
path('users/', include('users.urls', namespace='users')),
File "/home/cchilders/.local/lib/python3.10/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/usr/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 "/home/cchilders/projects/stocks_backend/users/urls.py", line 3, in <module>
from . import views
File "/home/cchilders/projects/stocks_backend/users/views.py", line 10, in <module>
from djongo import transaction
File "/home/cchilders/.local/lib/python3.10/site-packages/djongo/transaction.py", line 2, in <module>
from djongo import djongo_access_url
ImportError: cannot import name 'djongo_access_url' from 'djongo' (/home/cchilders/.local/lib/python3.10/site-packages/djongo/__init__.py)
My users/views.py:
from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
import json
from helpers.view_functions import parse_request_body
from .models import UserProfile
from djongo import transaction
#csrf_exempt
def get_user_profile(request, user_id):
# pass
if request.method == 'GET':
with transaction.atomic():
profile, created = UserProfile.objects.get_or_create(user_id=user_id)
if created:
profile.user_id = user_id
profile.searches = [
{'search_term': 'hd'},
{'search_term': 'wba'},
]
profile.display_settings = [
{'setting_name': 'showYieldChange', 'visible': True},
{'setting_name': 'showAllDividends', 'visible': True},
]
profile.save()
print("user saved in db")
user = UserProfile.objects.get(user_id=user_id)
data = {
'user_id': user.user_id,
'searches': user.searches,
'display_settings': user.display_settings
}
json_data = json.dumps(data)
return HttpResponse({json_data}, content_type='application/json')
if request.method == 'POST':
body = parse_request_body(request)
searches = body['searches']
searches_objects = [{'search_term': x} for x in searches]
print("New searches for user {user_id}".format(user_id=user_id))
print(searches_objects)
user = UserProfile.objects.get(user_id=user_id)
user.searches = searches_objects
user.display_settings = body['display_settings']
user.save()
return HttpResponse("it worked")
users/urls.py:
from django.urls import path
from . import views
app_name = 'dividends'
urlpatterns = [
path('<str:user_id>', views.get_user_profile, name='get_user_profile'),
]
requirements.txt:
bs4
django==3.1.12
django-cors-headers
djongo
gunicorn
html5lib
pymongo==3.12.3
python-decouple
yfinance
users/models.py:
from djongo import models
class RecentSearch(models.Model):
search_term = models.CharField(max_length=100)
class Meta:
abstract = True
class DisplaySetting(models.Model):
setting_name = models.CharField(max_length=150)
visible = models.BooleanField()
class Meta:
abstract = True
class UserProfile(models.Model):
user_id = models.CharField(max_length=255, unique=True)
searches = models.ArrayField(model_container=RecentSearch, null=True)
display_settings = models.ArrayField(model_container=DisplaySetting, null=True)
objects = models.DjongoManager()
in ipython:
IPython 8.4.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import djongo
In [2]: from djongo import transaction
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Input In [2], in <cell line: 1>()
----> 1 from djongo import transaction
File ~/.local/lib/python3.10/site-packages/djongo/transaction.py:2, in <module>
1 from djongo.exceptions import NotSupportedError
----> 2 from djongo import djongo_access_url
4 print(f'This version of djongo does not support transactions. Visit {djongo_access_url}')
5 raise NotSupportedError('transactions')
ImportError: cannot import name 'djongo_access_url' from 'djongo' (/home/cchilders/.local/lib/python3.10/site-packages/djongo/__init__.py)
In [3]:
correctly using get_or_create and only saving new data if created=True solves the bug of duplicate models when there should only be one unique one
#csrf_exempt
def get_user_profile(request, user_id):
pass
if request.method == 'GET':
# with transaction.atomic():
profile, created = UserProfile.objects.get_or_create(user_id=user_id)
if created:
profile.user_id = user_id
profile.searches = [
{'search_term': 'hd'},
{'search_term': 'wba'},
]
profile.display_settings = [
{'setting_name': 'showYieldChange', 'visible': True},
{'setting_name': 'showAllDividends', 'visible': True},
]
profile.save()
print("user saved in db")
# user = UserProfile.objects.get(user_id=user_id)
data = {
'user_id': profile.user_id,
'searches': profile.searches,
'display_settings': profile.display_settings
}
json_data = json.dumps(data)
return HttpResponse({json_data}, content_type='application/json')
you must set your unique attribute like user_id or stock ticker to unique=True
#csrf_exempt
def get_user_profile(request, user_id):
pass
if request.method == 'GET':
# with transaction.atomic():
profile, created = UserProfile.objects.get_or_create(user_id=user_id)
if created:
profile.user_id = user_id
profile.searches = [
{'search_term': 'hd'},
{'search_term': 'wba'},
]
profile.display_settings = [
{'setting_name': 'showYieldChange', 'visible': True},
{'setting_name': 'showAllDividends', 'visible': True},
]
profile.save()
print("user saved in db")
# user = UserProfile.objects.get(user_id=user_id)
data = {
'user_id': profile.user_id,
'searches': profile.searches,
'display_settings': profile.display_settings
}
json_data = json.dumps(data)
return HttpResponse({json_data}, content_type='application/json')
models
class UserProfile(models.Model):
user_id = models.CharField(max_length=255, unique=True)
searches = models.ArrayField(model_container=RecentSearch, null=True)
display_settings = models.ArrayField(model_container=DisplaySetting, null=True)
objects = models.DjongoManager()

if model._meta.abstract: AttributeError: type object 'ProductObject' has no attribute '_meta'

I want to add one of my models to the admin panel, but this error falls:
> Traceback (most recent call last): File
> "C:\Users\smirn\AppData\Local\Programs\Python\Python38-32\lib\threading.py",
> line 932, in _bootstrap_inner
> self.run() File "C:\Users\smirn\AppData\Local\Programs\Python\Python38-32\lib\threading.py",
> line 870, in run
> self._target(*self._args, **self._kwargs) File "C:\Users\smirn\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\autoreload.py",
> line 53, in wrapper
> fn(*args, **kwargs) File "C:\Users\smirn\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\commands\runserver.py",
> line 109, in inner_run
> autoreload.raise_last_exception() File "C:\Users\smirn\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\autoreload.py",
> line 76, in raise_last_exception
> raise _exception[1] File "C:\Users\smirn\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py",
> line 357, in execute
> autoreload.check_errors(django.setup)() File "C:\Users\smirn\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\autoreload.py",
> line 53, in wrapper
> fn(*args, **kwargs) File "C:\Users\smirn\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\__init__.py",
> line 24, in setup
> apps.populate(settings.INSTALLED_APPS) File "C:\Users\smirn\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\apps\registry.py",
> line 122, in populate
> app_config.ready() File "C:\Users\smirn\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\contrib\admin\apps.py",
> line 24, in ready
> self.module.autodiscover() File "C:\Users\smirn\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\contrib\admin\__init__.py",
> line 26, in autodiscover
> autodiscover_modules('admin', register_to=site) File "C:\Users\smirn\AppData\Local\Programs\Python\Python38-32\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:\Users\smirn\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\smirn\OneDrive\Desktop\SYZYGY\Coding\Python\Django\Megan\Fridge\admin.py",
> line 13, in <module>
> admin.site.register([Product, Fridge, ProductObject]) File "C:\Users\smirn\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\contrib\admin\sites.py",
> line 104, in register
> if model._meta.abstract: AttributeError: type object 'ProductObject' has no attribute '_meta'
models.py:
from django.db import models as m
from django.conf import settings
import datetime
def mounth():
now = datetime.datetime.now()
return now + datetime.timedelta(days=20)
class Product(m.Model):
product_name = m.CharField(max_length=200)
product_calories = m.PositiveIntegerField(blank=True)
def __str__(self):
return self.product_name
class Fridge(m.Model):
OPTIONS = (
("1", "BASIC"),
("2", "PRO"),
("3", "KING"),
)
fridge_owner = m.ForeignKey(settings.AUTH_USER_MODEL, on_delete=m.CASCADE)
fridge_mode = m.CharField(max_length=5, choices=OPTIONS)
class Recipe(m.Model):
recipe_name = m.CharField(max_length=200)
recipe_products = m.ManyToManyField(Product)
recipe_description = m.TextField()
def __str__(self):
return self.recipe_name
class ProductObject(): # Не знаю как сделать правильно. Вдруг это можно реализовать по другому
product_obj_fridge = m.ForeignKey(Fridge, on_delete=m.CASCADE)
product_obj_product = m.ManyToManyField(Product)
product_shelf_life = m.DateField(default=mounth())
product_count = m.PositiveIntegerField(default=1)
class Meta:
ordering = ('product_shelf_life', )
admin.py:
from django.contrib import admin
from .models import Product, Fridge, Recipe, ProductObject
from tinymce.widgets import TinyMCE
from django.db import models
# Register your models here.
class RecipeAdmin(admin.ModelAdmin):
formfield_overrides = {
models.TextField: {'widget': TinyMCE}
}
admin.site.register([Product, Fridge, ProductObject])
admin.site.register(Recipe, RecipeAdmin)
If I remove the ProductObject in the registration in the admin panel, then there will be no error, but I do not understand this error at all. It seems that everything should be correct, but for some reason not
Please, help me!
In model ProductObject you are missing m.Model in the definition.
Without this the Meta field can not be constructed.

django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty - Even when SECRET_KEY is set in settings.py

I'm having this error while trying to run my Teonite_project/web_scrapper.py script:
File "C:/Users/kfhei/Desktop/Teonite_project/Teonite_project/web_scrapper.py", line 9, in <module>
django.setup()
File "C:\Users\kfhei\Desktop\Teonite_project\env\lib\site-packages\django\__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "C:\Users\kfhei\Desktop\Teonite_project\env\lib\site-packages\django\conf\__init__.py", line 56, in __getattr__
self._setup(name)
File "C:\Users\kfhei\Desktop\Teonite_project\env\lib\site-packages\django\conf\__init__.py", line 43, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\kfhei\Desktop\Teonite_project\env\lib\site-packages\django\conf\__init__.py", line 106, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Users\kfhei\Desktop\Teonite_project\env\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 "C:\Users\kfhei\Desktop\Teonite_project\Teonite_project\Teonite_project\settings.py", line 15, in <module>
django.setup()
File "C:\Users\kfhei\Desktop\Teonite_project\env\lib\site-packages\django\__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "C:\Users\kfhei\Desktop\Teonite_project\env\lib\site-packages\django\conf\__init__.py", line 56, in __getattr__
self._setup(name)
File "C:\Users\kfhei\Desktop\Teonite_project\env\lib\site-packages\django\conf\__init__.py", line 43, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\kfhei\Desktop\Teonite_project\env\lib\site-packages\django\conf\__init__.py", line 125, in __init__
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
My script:
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
#import simplejson as json
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Teonite_project.settings")
import django
django.setup()
from words.models import Word
from authors.models import Author
URL_DOMAIN = 'https://teonite.com/blog/'
def get_links(url):
'''
Returning the array of links
to blog articles from the website
'''
html = get_html(url)
links = []
for link in html.findAll('a'):
link = link.get('href')
if link == None or link[6:9] == 'tag' or link[6:10]=='page':
pass
elif link[:6] == '/blog/':
link = url[:19] + link
links.append(link)
return links
def get_html(url):
'''
Returning the HTML of the website
'''
req = Request(url)
html_page = urlopen(req)
html = BeautifulSoup(html_page, "html.parser")
return html
def get_text(url):
'''
Extracting the post content of
the articles from the blog
'''
html = get_html(url)
text =''
for content in html.select('.post-content'):
text = text + content.text
return content.text
def get_author(url):
'''
Extracting the name of the Author
from the articles
'''
html = get_html(url)
for author in html.select('.author-content'):
return author.text
if __name__ == '__main__':
'''
Main function tasks:
* Extract the neccessary data from the website,
* Save it to the database
'''
links = get_links(URL_DOMAIN)
author_dict_database = {}
word_dict_database = {}
for link in links:
text = get_text(link).strip()
wordslist = text.split()
author = get_author(link).strip()
for word in wordslist:
if not word.isalpha():
wordslist.remove(word)
word = word.lower()
if author in author_dict_database:
for word in wordslist:
if word in word_dict_database:
author_dict_database[author][word] += 1
else:
author_dict_database[author][word] = 1
else:
for word in wordslist:
if word in word_dict_database:
word_dict_database[word] += 1
else:
word_dict_database[word] = 1
author_dict_database[author] = word_dict_database
#Saving values to postgres database
for key_author,word_dict in author_dict_database:
database_author = Author(author=key_author)
for key_word, value_count in word_dict:
database_word = Word(author=key_author, words_list=key_word, words_count=value_count)
As you can seen I tried different things to make it work. I've read several topics in stackoverflow and tried to search in different websites.
Also I have configured the wsgi.py file
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Teonite_project.settings")
Unfortunately I have no idea why this happens, because I have set the SECRET_KEY in my Teonite_project/Teonite_project/settings.py
Basically all I want is to run my script, and add the scrapped values to my postgres database in my authors, and words models.

ValueError: dictionary update sequence element #0 has length 1; 2 is required

I can not locate this error.
Error is genereted after I added one field in disease that is contact and taggable manager in models
If that is correct than is this error is generated due to Id field that I gave manually
Help to me get out of this!!
personal(myapp)/urls.py
urlpatterns = [
url(r'^.*doctorsu/$', views.doctorsu.as_view(), name = 'doctorsu'),
url(r'^.*disease/$', views.AddDisease.as_view(), name = 'AddDisease'),
]
mysite/urls.py
urlpatterns = [
url(r'^$',include('personal.urls')),
url(r'^taggit/',include('taggit_selectize.urls')),
]
models.py
class DoctorSignup(models.Model):
contact_regex = RegexValidator(regex=r'^[789]\d{9}$',message="Phone number must be start with 7,8 or 9")
doid = models.AutoField(verbose_name='Doctor Id',primary_key=True,default=0)
email = models.CharField(max_length=50)
contact = models.CharField(validators=[contact_regex])
class TaggedSymptoms(TaggedItemBase):
content_object = models.ForeignKey("Disease")
class TaggedMedicine(TaggedItemBase):
content_object = models.ForeignKey("Disease")
class Disease(models.Model):
did = models.AutoField(verbose_name='Disease Id', primary_key=True,default=0)
dName = models.CharField(max_length=20)
dtype = models.CharField(max_length=10)
symptoms = TaggableManager(through=TaggedSymptoms)
symptoms.rel.related_name = "+"
medi = TaggableManager(through=TaggedMedicine)
medi.rel.related_name = "+"
views.py
class doctorsu(TemplateView):
template_name = 'personal/doctorsu.html'
def get(self, request):
dsform = DoctorSignupForm()
data = DoctorSignup.objects.all()
args = {'dsform': dsform,'data': data}
return render(request,self.template_name,args)
def post(self, request):
dsform = DoctorSignupForm(request.POST)
if dsform.is_valid():
dsform.save()
cd = dsform.cleaned_data
args = {'dsform': dsform , 'cd': cd}
return render(request,self.template_name,args)
return render(request, 'personal/doctorsu.html')
class AddDisease(TemplateView):
template_name = 'personal/disease.html'
def get(self, request):
dform = DiseaseForm()
ddata = Disease.objects.all()
args = {'dform': dform,'ddata': ddata}
return render(request,self.template_name,args)
def post(self, request):
dform = DiseaseForm(request.POST)
if dform.is_valid():
dform.save()
cd = dform.cleaned_data
args = {'dform': dform , 'cd': cd}
return render(request,self.template_name,args)
forms.py
class DoctorSignupForm(forms.ModelForm):
dname = forms.CharField()
email = forms.EmailField()
contact = forms.RegexField(regex=r'^[789]\d{9}$',error_messages="Enter valid phone no.")
class Meta:
model = DoctorSignup
fields = "__all__"
class DiseaseForm(ModelForm):
dName = forms.CharField(help_text="Enter disease")
symptoms = TagField(help_text="Enter symptoms separated by comma")
medicine = TagField()
class Meta:
model = Disease
fields = "__all__"
Traceback
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\__init__.py", line 363, in execute_from_command_line
utility.execute()
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\base.py", line 327, in execute
self.check()
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\checks\urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\checks\urls.py", line 26, in check_resolver
return check_method()
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\urls\resolvers.py", line 254, in check
for pattern in self.url_patterns:
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\urls\resolvers.py", line 405, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\urls\resolvers.py", line 398, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\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 "E:\IT\project\program\20-8-17\mysite\mysite\urls.py", line 7, in <module>
url(r'^$',include('personal.urls')),
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\conf\urls\__init__.py", line 50, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\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 "E:\IT\project\program\20-8-17\mysite\personal\urls.py", line 2, in <module>
from . import views
File "E:\IT\project\program\20-8-17\mysite\personal\views.py", line 3, in <module>
from personal.forms import *
File "E:\IT\project\program\20-8-17\mysite\personal\forms.py", line 50, in <module>
class DoctorSignupForm(forms.ModelForm):
File "E:\IT\project\program\20-8-17\mysite\personal\forms.py", line 90, in DoctorSignupForm
contact = forms.RegexField(regex=r'^[789]\d{9}$',error_messages="Enter valid phone no.")
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\forms\fields.py", line 517, in __init__
super(RegexField, self).__init__(max_length, min_length, *args, **kwargs)
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\forms\fields.py", line 228, in __init__
super(CharField, self).__init__(*args, **kwargs)
File "C:\Users\Charmi Shah\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\forms\fields.py", line 122, in __init__
messages.update(error_messages or {})
ValueError: dictionary update sequence element #0 has length 1; 2 is required
error_messages should be a dict, not a string.
See the docs.

Import error when pipelining data from spider into sqlalchemy table

This is what the project tree looks like:
rym_chart_scraper
├───scrapy.cfg
├───rym_chart_scraper
│ ├───__init__.py
│ ├───items.py
│ ├───models.py
├───pipelines.py
├───settings.py
├───spiders
├───my_spider.py
├───__init__.py
pipelines.py
from models import TopAlbums, db_connect, create_topalbums_table
from sqlalchemy.orm import sessionmaker
class TopAlbumPipeline:
def __init__(self):
engine = db_connect()
create_topalbums_table(engine)
self.Session = sessionmaker(bind=engine)
def process_item(self, item, spider):
session = self.Session()
topalbums = TopAlbums(**item)
try:
session.add(topalbums)
session.commit()
except:
session.rollback()
raise
finally:
session.close()
return item
models.py
from sqlalchemy import create_engine
from sqlalchemy.engine.url import URL
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, DateTime
import settings
Base = declarative_base()
def db_connect():
return create_engine(URL(**settings.DATABASE))
def create_topalbums_table(engine):
Base.metadata.create_all(engine)
class TopAlbums(Base):
__tablename__ = 'top_albums'
id = Column(Integer, primary_key=True)
Artist = Column('Artist', String)
Album = Column('Album', String)
Chart_year = Column('Chart_year', String)
Genre = Column('Genre', String)
Ratings = Column('Ratings', Integer)
Reviews = Column('Reviews', Integer)
Date = Column('Date', DateTime)
And the spider:
from scrapy import Spider, Request
from rym_chart_scraper.utility import find_between, listToString
from rym_chart_scraper.items import TopAlbumChartItem
from datetime import datetime
class TopAlbumChartSpider(Spider):
name = "top_music_charts"
allowed_domains = ['rateyourmusic.com']
start_urls = [
"https://rateyourmusic.com/charts/top/album/all-time"
]
n_pages = 1
def parse(self, response):
for album, stats in zip(response.css('div.chart_main'),
response.css('div.chart_stats')):
...
yield item
next_page = response.css('a.navlinknext')[0].css(
'a::attr(href)').extract_first()
if next_page is not None:
next_page = response.urljoin(next_page)
self.n_pages += 1
if self.n_pages < 31:
yield Request(next_page, callback=self.parse)
When I run the scraper with:
scrapy crawl top_music_charts
I get the following import error.
2016-12-11 17:46:41 [twisted] CRITICAL:
Traceback (most recent call last):
File "/Users/baasman/anaconda/lib/python3.5/site-packages/twisted/internet/defer.py", line 1299, in _inlineCallbacks
result = g.send(result)
File "/Users/baasman/anaconda/lib/python3.5/site-packages/scrapy/crawler.py", line 72, in crawl
self.engine = self._create_engine()
File "/Users/baasman/anaconda/lib/python3.5/site-packages/scrapy/crawler.py", line 97, in _create_engine
return ExecutionEngine(self, lambda _: self.stop())
File "/Users/baasman/anaconda/lib/python3.5/site-packages/scrapy/core/engine.py", line 69, in __init__
self.scraper = Scraper(crawler)
File "/Users/baasman/anaconda/lib/python3.5/site-packages/scrapy/core/scraper.py", line 71, in __init__
self.itemproc = itemproc_cls.from_crawler(crawler)
File "/Users/baasman/anaconda/lib/python3.5/site-packages/scrapy/middleware.py", line 58, in from_crawler
return cls.from_settings(crawler.settings, crawler)
File "/Users/baasman/anaconda/lib/python3.5/site-packages/scrapy/middleware.py", line 34, in from_settings
mwcls = load_object(clspath)
File "/Users/baasman/anaconda/lib/python3.5/site-packages/scrapy/utils/misc.py", line 44, in load_object
mod = import_module(module)
File "/Users/baasman/anaconda/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 "/Users/baasman/Documents/python-workspace/rym_chart_scraper/rym_chart_scraper/pipelines.py", line 1, in <module>
from models import TopAlbums, db_connect, create_topalbums_table
ImportError: No module named 'models'
Trying to import 'models' interactively from main doesn't give an error, just when running the actual spider from the command line. Is there something wrong with the structure of the project? Or is it some other silly mistake? For some reason I can not get past this.

Categories