I need to insert data in table SQLite Django
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.