My app name is search_keywords. As on creating this app, I wiil have one file called models.py in which I have written this piece if code :
from django.db import models
class Keywords(models.Model):
file_name = models.CharField(primary_key=True, max_length=100)
frequency_count = models.IntegerField()
then add this app to INSTALLED_APPS and run python manage.py syncdb. On running this command , I will get a table automatically created in django. Then run python manage.py sql search_keywords. It will show the table as desired.
Then the next step is to run python manage.py shell.Instead of running this I want to insert the values in my table created through the python code that I have created.The code is:
#!/usr/bin/python
#here skey.py is another file created by me and has the imported functions
#in this code
from skey import find_root_tags, count, sorting_list
str1 = raw_input("enter the word to be searched\n")
list = []
fo = open("xml.txt","r")
for i in range(count.__len__()):
file = fo.readline()
file = file.rstrip('\n')
find_root_tags(file,str1,i)
list.append((file,count[i]))
sorting_list(list)
fo.close()
I want to insert this list elements in the the table created by the django and that too after the function sorting_list has been called. The list contains the file name and it's count. e.g. list=[('books.xml','3'),('news.xml,'2')].
How can I do that?
Please Help.
//////////////////////////////////////////////////////////////////////////////
Hey I have written the code:
#!/usr/bin/python
#to tell django which settings module to use
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
from search.models import Keywords
from skey import find_root_tags, count, sorting_list
str1 = raw_input("enter the word to be searched\n")
list = []
fo = open("xml.txt","r")
for i in range(count.__len__()):
file = fo.readline()
file = file.rstrip('\n')
find_root_tags(file,str1,i)
list.append((file,count[i]))
sorting_list(list)
for name, count in list:
s = Keywords(file_name=name,frequency_count=count)
s.save()
fo.close()
Here django_project = mysite #my project's name and app = search #my app's name
on running this code it gives me error :
Traceback (most recent call last):
File "call.py", line 7, in
from search.models import Keywords
ImportError: No module named search.models
and on including :
import sys
sys.path.insert(0, path_to_django_project)
this in above code it gives error:
Traceback (most recent call last):
File "call.py", line 4, in
sys.path.insert(0,path_to_mysite)
NameError: name 'path_to_mysite' is not defined
Why?I have my project on the desktop and the above python code file as well. Please Help!!
////////////////////////////////////////// now it's giving me this error , please help.see it at : error in accessing table created in django in the python code