database object fails to execute .create() method

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!

models code:
from django.db import models

class Topic(models.Model):
topic_id = models.CharField(max_length=20)
topic_text = models.CharField(max_length=200)
category = models.CharField(max_length=100)

def __str__(self):
    return self.topic_id

class Item(models.Model):
topic_id = models.CharField(max_length=20)
item_text = models.CharField(max_length=200)
item_property = models.CharField(max_length=10)
item_value = models.CharField(max_length=100)

def __str__(self):
    return self.topic_id

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.

How are you running that dbmgmt.py program?

Or to rephrase it, what is the command that you are issuing that is causing that particular function to execute?

I’m importing it into views.py. Here’s the contents:

from django.shortcuts import render
from .models import Topic, Item
from .dbmgmt import get_topic_data, get_item_data
import re


def index(request):
    get_topic_data(True)
    topic_results = Topic.objects.all()
    context = {'topic_results': topic_results}
    return render(request, 'unlv/index.html', context)


def item(request, item_id):
    # hacky way to get item id
    elements = re.split(r'\/', request.path)
    q_code = elements.pop(1)

    get_item_data(q_code)
    item_results = Item.objects.get(topic_id=q_code)
    context = {'item_results': item_results}
    return render(request, 'unlv/item.html', context)

Ok, so you’ve got a browser that is issuing the request to the url /unlv/Q1426968/item/ while your application is being run by runserver?

Can you please post the complete traceback?

Side note: The entire reason you specify the url using the line at https://github.com/aehulet/sparqlBuilder2/blob/976f32a5c0c4c3098ea80a986735fca533703f0c/sparqlBuilder2/urls.py#L23, and identify the item_id as the parameter within the view function, is so you don’t need to do stuff like:

The variable item_id is the parameter you’re looking for.

Your point about not using .create() correctly solved the problem. I used the following

       for i in latest_result:
            new = models.Item.objects.create(topic_id=i[0], topic_text=i[1], category=i[2])

and everything works great now. Thanks again.

Side note: Yes, the code I inserted to side-step using item_id was a temp hack. I understand what the parameter on the view method is meant to do. :wink: