I’m new to Django but have gone through the tutorial and have started a simple app that loads data from SPARQL queries into web pages. The site partly works, but for my “Item” model class in one particular def, the runtime fails to find the .create() method of the object. I can access Item objects just fine in other routines and can insert data into the underlying table via the command line interpreter, so I’m at a loss. Any help is greatly appreciated!
problem method code:
def get_item_data(qcode):
funct_val = False
# create nested results list from SPARQL query: works
qry = spql.query_item_detail(qcode)
result_dict = spql.get_wd_query(qry)
item_results = spql.load_item_detail(result_dict)
# instantiate a QuerySet of existing Items
i_objs = models.Item.objects.all()
# if i_objs:
# i_objs.delete()
# i_objs.update()
for i in item_results:
new = i_objs.create() # code breaks here: 'Item' object has no attribute 'create'
new.topic_id = i[0]
new.item_text = i[1]
new.item_property = i[2]
new.item_value = i[3]
new.save()
funct_val = True
return funct_val
stack trace:
testy = dbmgmt.get_item_data(“Q1426968”)
Traceback (most recent call last):
File “/usr/lib/python3.10/code.py”, line 90, in runcode
exec(code, self.locals)
File “”, line 1, in
File “/home/ed/PycharmProjects/sparqlBuilder2/unlv/dbmgmt.py”, line 39, in get_item_data
new = i_objs.create()
AttributeError: ‘Item’ object has no attribute ‘create’
testy = dbmgmt.get_item_data(“Q1426968”)
Traceback (most recent call last):
File “/usr/lib/python3.10/code.py”, line 90, in runcode
exec(code, self.locals)
File “”, line 1, in
File “/home/ed/PycharmProjects/sparqlBuilder2/unlv/dbmgmt.py”, line 39, in get_item_data
new = i_objs.create()
AttributeError: ‘Item’ object has no attribute ‘create’
Side note: When posting code (or templates, error messages, etc), enclose it between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code (or template, error, etc), then another line of ```. This forces the forum software to keep your code properly formatted.
The create method is a method on a model manager - not on the model.
First, you’re using it wrong. It’s not appropriate to call create without passing the parameters to initialize the object.
But aside from that, where is this code located? (What file?) What is causing it to be executed?
If you’re asking literally, you can see all the code at GitHub - aehulet/sparqlBuilder2: prototype application. I’ve created a dbmgmt.py file that I’m using to load the results of wikidata queries into a SQLite db via the models. The def with the problem is in dbmgmt. I’m attempting to use views to call table loading methods, then render the html. My “index” view works; my “item” view doesn’t due to the problem in dbmgmt.
Thanks for the advice on .create(). I’m going to rewrite and will post if that solves the problem.