Have a csv file with data. do parse that.
file_reader = csv.reader(f, delimiter=';')
for row in file_reader:
data = UserDataCsv()
data.user = files.user
data.file_hash = request.FILES['file']
data.file = UserFiles.objects.get(id=file_id)
print(row[0])
data.material = row[0]
But when i try add new row got error.
Internal Server Error: /upload_file/
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\handlers\exception.py",
line 42, in inner
response = get_response(request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\PythoProjecs\csv_files\users\user_functions.py", line 112, in upload_file
data.save()
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 796, in save
force_update=force_update, update_fields=update_fields)
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 824, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 908, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 947, in _do_insert
using=using, raw=raw)
File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 1045, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line
1054, in execute_sql
cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\utils.py", line 94, in exit
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line
110, in execute
return self.cursor.execute(query, args)
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler raise errorclass,
errorvalueOperationalError: (1366, "Incorrect string value:
'\xC4\xD1\xCF' for column 'material' at row 1") [30/Jun/2017
16:34:32] "POST /upload_file/ HTTP/1.1" 500 17454
How i can fix that?
Related
I am new to Django. I think it's really easy one for Django expert, so I wish I could get help from you.
I have two tables in SQLite DB-namely Process and ScrapedData. While migrating models I have had several error so I just created these two tables manually by Navicat.
Following shows models.
class Process(models.Model):
PLATFORM=(
('Instagram','Instagram'),
('Facebook','Facebook'),
('LinkedIn','LinkedIn'),
('TikTok','TikTok'),
('Youtube','Youtube'),
('Twitter','Twitter')
)
hashtag=models.CharField(max_length=300)
platform=models.CharField(choices=PLATFORM,max_length=9)
date=models.DateField(auto_now_add=True)
class ScrapedData(models.Model):
homepage=models.CharField(max_length=500)
email=models.CharField(max_length=100)
process_id=models.ForeignKey(
Process,
on_delete=models.CASCADE,
verbose_name="the related Process")
Then I tried to insert data into these two tables then I got following error.
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.OperationalError: table info_scrapeddata has no column named process_id_id
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\task\scrapping\scraping for google api\College-ERP\info\views.py", line 116, in attendance_search
ScrapedData.objects.create(homepage=url.strip(),email=email, process_id=new_process)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\query.py", line 453, in create
obj.save(force_insert=True, using=self.db)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\base.py", line 739, in save
self.save_base(using=using, force_insert=force_insert,
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\base.py", line 776, in save_base
updated = self._save_table(
^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\base.py", line 881, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\base.py", line 919, in _do_insert
return manager._insert(
^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\query.py", line 1270, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\sql\compiler.py", line 1416, in execute_sql
cursor.execute(sql, params)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 79, in _execute
with self.db.wrap_database_errors:
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.OperationalError: table info_scrapeddata has no column named process_id_id
Following shows view.py file that I tried...
urls = store_urls(searches_items, count, platform="")
unique_emails = []
new_process=Process.objects.create(hashtag=searches_items,platform="facebook")
print("Process ID is",new_process.id)
# with open(file_name, "r", encoding="utf-8") as url_file:
for url_no, url in enumerate(urls):
url = url.strip()
if not url.endswith(".pdf"):
if req_delay:
time.sleep(req_delay)
# if("facebook.com" not in url):
# continue
emails, msg = get_emails(url.strip(), ua.random)
if msg == "break":
# emails_to_file(searches_items, unique_emails)
print('result here')
print(unique_emails)
sys.exit()
print(f"url number {url_no + 1} Status: {msg}")
if emails:
for email in emails:
if email not in unique_emails:
if email.endswith((".com", ".net", ".org")):
ScrapedData.objects.create(homepage=url.strip(),email=email, process_id=new_process)
unique_emails.append(email)
else:
continue
After I execute app I got a row newly inserted in Process but ScrapedData always shows me empty rows..
This is process table
This is ScrapedData table
What I want is 1 process row with several ScrapedData rows with one to many relationships.
I have no column named process_id_id in ScrapedData table but log always say this error.
Please help me. Thanks in advance.
I am trying to filter my product list with price less than filter but getting error. I guess the error is I am unable to pass parameters correctly.
any help would be appreciated.
if you require anymore details, please ask.
Note: it does not works even if I pass the query on filter body static.
thanks in advance.
my method:
def getfilter(request, format=None):
kwargs = {
'{0}__{1}'.format('price', 'lt'): Decimal('1000.00'),
# '{0}__{1}'.format('name', 'endswith'): 'Z'
}
products = Product.objects.all().filter(**kwargs)
serializer = ProductSerializer1(products, many=True)
return Response(serializer.data)
Error I Receive:
Traceback (most recent call last):
File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 806, in parse
return handler(self, statement)
File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 934, in _select
self._query = SelectQuery(self.db, self.connection_properties, sm, self._params)
File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 113, in __init__
super().__init__(*args)
File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 74, in __init__
self.parse()
File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 153, in parse
raise SQLDecodeError
djongo.sql2mongo.SQLDecodeError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/sunil/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/sunil/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/sunil/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/sunil/.local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/sunil/.local/lib/python3.6/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/decorators.py", line 55, in handler
return func(*args, **kwargs)
File "/home/sunil/Projects/python/loginBazaar/products/prodView.py", line 219, in getfilter
return Response(serializer.data)
File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/serializers.py", line 768, in data
ret = super(ListSerializer, self).data
File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/serializers.py", line 262, in data
self._data = self.to_representation(self.instance)
File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/serializers.py", line 686, in to_representation
self.child.to_representation(item) for item in iterable
File "/home/sunil/.local/lib/python3.6/site-packages/django/db/models/query.py", line 274, in __iter__
self._fetch_all()
File "/home/sunil/.local/lib/python3.6/site-packages/django/db/models/query.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/sunil/.local/lib/python3.6/site-packages/django/db/models/query.py", line 55, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/home/sunil/.local/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1100, in execute_sql
cursor.execute(sql, params)
File "/home/sunil/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/home/sunil/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/sunil/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/sunil/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/sunil/.local/lib/python3.6/site-packages/djongo/cursor.py", line 53, in execute
params)
File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 743, in __init__
self.parse()
File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 826, in parse
raise exe from e
djongo.sql2mongo.SQLDecodeError: FAILED SQL: SELECT "products_product"."id", "products_product"."name", "products_product"."vendorId", "products_product"."quantity", "products_product"."shopCode", "products_product"."discountPercent", "products_product"."discountPrice", "products_product"."price", "products_product"."dummyPrice", "products_product"."margin", "products_product"."description", "products_product"."is_verified", "products_product"."category", "products_product"."subCategory", "products_product"."images", "products_product"."sizeChart", "products_product"."attributes", "products_product"."buyCount", "products_product"."thumbnail", "products_product"."productCode", "products_product"."atts" FROM "products_product" WHERE "products_product"."price" < %(0)s ORDER BY "products_product"."productCode" ASC
Version: 1.2.32
It seems like the conversion from Decimal to string failed:
'{0}__{1}'.format('price', 'lt'): Decimal('1000.00'),
and corresponding SQL:
< %(0)s
I guess one of this possible troubles:
your model field price type is incorrect for this conversion
your decimal value have two zero numbers after decimal point - try to
remove the last one
Problem Description:
The DRF UniqueTogetherValidator is displaying some odd behaviour.
For example:
models.py
class MyModel1(models.Model):
field1 = models.IntegerField()
field2 = models.ForeignKey('MyModel2', on_delete...)
class MyModel2(models.Model):
field3 = models.IntegerField()
serializer.py
from rest_framework.validators import UniqueTogetherValidator
class MyModel1Seriealizer(serializers.ModelSerializer):
class Meta:
model = MyModel1
validators = [
UniqueTogetherValidator(
queryset=MyModel1.objects.all(),
fields=('field1', 'field2_id')
)
]
When the condition is violated by field1 only, it reports back a good 400 response with a message in non_field_errors, but when the violated field is field2 (or its _id), the server gives back a response code 500 (meaning Django caught it at the database/ORM level).
The actual use-case is here on my GitHub.
Full traceback
Internal Server Error: /parts/10/
Traceback (most recent call last):
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: UNIQUE constraint failed: part_management_part.class_code_id, part_management_part.number
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/rest_framework/viewsets.py", line 116, in view
return self.dispatch(request, *args, **kwargs)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/rest_framework/mixins.py", line 84, in partial_update
return self.update(request, *args, **kwargs)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/rest_framework/mixins.py", line 70, in update
self.perform_update(serializer)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/rest_framework/mixins.py", line 80, in perform_update
serializer.save()
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/rest_framework/serializers.py", line 209, in save
self.instance = self.update(self.instance, validated_data)
File "/home/anani/PycharmProjects/part_management_backend/part_management/serializers.py", line 55, in update
instance.save()
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/db/models/base.py", line 741, in save
force_update=force_update, update_fields=update_fields)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/db/models/base.py", line 779, in save_base
force_update, using, update_fields,
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/db/models/base.py", line 851, in _save_table
forced_update)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/db/models/base.py", line 900, in _do_update
return filtered._update(values) > 0
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/db/models/query.py", line 760, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1429, in execute_sql
cursor = super().execute_sql(result_type)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1100, in execute_sql
cursor.execute(sql, params)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/anani/PycharmProjects/part_management_backend/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed: part_management_part.class_code_id, part_management_part.number
[06/Jun/2019 23:37:04] "PATCH /parts/10/ HTTP/1.1" 500 22340
if field2_id is foreign key, you don't need to put field like that.
just put field2
from rest_framework.validators import UniqueTogetherValidator
class MyModel1Seriealizer(serializers.ModelSerializer):
class Meta:
model = MyModel1
validators = [
UniqueTogetherValidator(
queryset=MyModel1.objects.all(),
fields=('field1', 'field2')
)
]
and you can try post json data like this:
{
"field2": 1,
"field1": "test",
}
I'm working on a Django application which fetches JSON data from an API and stores it in PostgreSQL database. But while migrating the app I'm getting these errors:
psycopg2.ProgrammingError: column "data" of relation "WorldBank_projects" does not exist
LINE 1: INSERT INTO "WorldBank_projects" ("data", "project_id", "pro...
What should I change in code to resolve this error?
Here's the traceback:
Traceback (most recent call last):
File "/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: column "data" of relation "WorldBank_projects" does not exist
LINE 1: INSERT INTO "WorldBank_projects" ("data", "project_id", "pro...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/python/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/python/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/app/aggregator/WorldBank/management/commands/fetch_wb.py", line 53, in handle
project_abstract = data['project_abstract']
File "/python/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/python/lib/python3.6/site-packages/django/db/models/query.py", line 394, in create
obj.save(force_insert=True, using=self.db)
File "/python/lib/python3.6/site-packages/django/db/models/base.py", line 807, in save
force_update=force_update, update_fields=update_fields)
File "/python/lib/python3.6/site-packages/django/db/models/base.py", line 837, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/python/lib/python3.6/site-packages/django/db/models/base.py", line 923, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/python/lib/python3.6/site-packages/django/db/models/base.py", line 962, in _do_insert
using=using, raw=raw)
File "/python/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/python/lib/python3.6/site-packages/django/db/models/query.py", line 1076, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1107, in execute_sql
cursor.execute(sql, params)
File "/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/python/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/python/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "data" of relation "WorldBank_projects" does not exist
LINE 1: INSERT INTO "WorldBank_projects" ("data", "project_id", "pro...
How to fix this problem?
Here's my code for models.py:
from django.db import models
from django.contrib.postgres.fields import JSONField
class Projects(models.Model):
data = JSONField(null=False)
project_id=models.CharField(max_length=100)
project_name=models.CharField(max_length=100)
status=models.CharField(max_length=10)
country=models.CharField(max_length=100)
locations=JSONField()
mjtheme=models.CharField(max_length=50)
project_docs=JSONField()
source=models.CharField(max_length=10)
mjtheme_namecode=models.CharField(max_length=10)
docty=models.TextField()
countryname=models.CharField(max_length=100)
countrycode=models.CharField(max_length=10)
themecode=models.CharField(max_length=100)
theme_namecode=models.CharField(max_length=100)
project_url=models.TextField()
totalcommamt=models.CharField(max_length=100)
mjthemecode=models.CharField(max_length=100)
sector1=models.CharField(max_length=10)
theme1=models.CharField(max_length=10)
theme2=models.CharField(max_length=10)
theme3=models.CharField(max_length=10)
projectinfo=models.TextField()
country_namecode=models.CharField(max_length=5)
p2a_updated_date=models.CharField(max_length=100)
p2a_flag=models.CharField(max_length=5)
project_abstract=JSONField()
And here's the code for fetch.py file which is stored under /management/commands/fetch.py:
import requests
from django.core.management.base import BaseCommand
from aggregator.WorldBank.models import Projects
class Command(BaseCommand):
def handle(self, **options):
response = requests.get("https://search.worldbank.org/api/v2/projects?format=json&countryshortname_exact=India&source=IBRD&kw=N&rows=7")
data = response.json()
projects = data['projects']
for project in projects:
print(projects[project])
print("\n\n")
data = projects[project]
Projects.objects.create(
project_id = data['id'],
project_name = data['project_name'],
status = data['status'],
country = data['countryshortname'],
locations = data['locations'],
mjtheme = data['mjtheme'],
project_docs = data['projectdocs'],
source = data['source'],
mjtheme_namecode = data['mjtheme_namecode'],
docty = data['docty'],
countryname = data['countryname'],
countrycode = data['countrycode'],
themecode = data['themecode'],
theme_namecode = data['theme_namecode'],
project_url = data['url'],
totalcommamt = data['totalcommamt'],
mjthemecode = data['mjthemecode'],
sector1 = data['sector1'],
theme1 = data['theme1'],
theme2 = data['theme2'],
theme3 = data['theme3'],
projectinfo = data['projectinfo'],
country_namecode = ['country_namecode'],
p2a_updated_date = data['p2a_updated_date'],
p2a_flag = data['p2a_flag'],
project_abstract = data['project_abstract']
)
I'm using Django-CMS and I created some custom plugins that I use in several pages but in one of the pages I can't publish nor I can't copy plugins that are nested and gives me the same error in both situations:
Internal Server Error: /en/admin/cms/page/copy-plugins/
Traceback (most recent call last):
File "lib\site-packages\django\core\handlers\base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "lib\site-packages\django\core\handlers\base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "lib\site-packages\django\utils\decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "lib\site-packages\django\views\decorators\cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "lib\site-packages\django\contrib\admin\sites.py", line 244, in inner
return view(request, *args, **kwargs)
File "lib\site-packages\django\utils\decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "lib\site-packages\django\views\decorators\http.py", line 42, in inner
return func(request, *args, **kwargs)
File "lib\site-packages\django\utils\decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "lib\site-packages\django\views\decorators\clickjacking.py", line 39, in wrapped_view
resp = view_func(*args, **kwargs)
File "lib\site-packages\django\utils\decorators.py", line 184, in inner
return func(*args, **kwargs)
File "lib\site-packages\cms\admin\placeholderadmin.py", line 363, in copy_plugins
plugins, target_placeholder, target_language, target_plugin_id)
File "lib\site-packages\cms\utils\copy_plugins.py", line 24, in copy_plugins_to
old_parent_cache, no_signals))
File "lib\site-packages\cms\models\pluginmodel.py", line 319, in copy_plugin
new_plugin.save()
File "lib\site-packages\cms\models\pluginmodel.py", line 240, in save
self.parent.add_child(instance=self)
File "lib\site-packages\treebeard\mp_tree.py", line 970, in add_child
return MP_AddChildHandler(self, **kwargs).process()
File "lib\site-packages\treebeard\mp_tree.py", line 361, in process
newobj.save()
File "lib\site-packages\cms\models\pluginmodel.py", line 248, in save
super(CMSPlugin, self).save(*args, **kwargs)
File "lib\site-packages\django\db\models\base.py", line 708, in save
force_update=force_update, update_fields=update_fields)
File "lib\site-packages\django\db\models\base.py", line 736, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "lib\site-packages\django\db\models\base.py", line 820, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "lib\site-packages\django\db\models\base.py", line 859, in _do_insert
using=using, raw=raw)
File "lib\site-packages\django\db\models\manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "lib\site-packages\django\db\models\query.py", line 1039, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "lib\site-packages\django\db\models\sql\compiler.py", line 1060, in execute_sql
cursor.execute(sql, params)
File "lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "lib\site-packages\django\db\backends\mysql\base.py", line 117, in execute
six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
File "lib\site-packages\django\db\backends\mysql\base.py", line 112, in execute
return self.cursor.execute(query, args)
File "build\bdist.win-amd64\egg\MySQLdb\cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "build\bdist.win-amd64\egg\MySQLdb\connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
IntegrityError: (1048, "Column 'position' cannot be null")
I've checked in my database and in the cms_cmsplugintable exists a column named position but there's no entry with position null. Do you have any idea what's going on?