# database object fails to execute .create() method

**URL:** <https://forum.djangoproject.com/t/database-object-fails-to-execute-create-method/18679>\
**Category:** Mystery Errors\
**Created:** [February 6, 2023, 8:11pm UTC](https://forum.djangoproject.com/t/database-object-fails-to-execute-create-method/18679 "2023-02-06T20:11:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aehulet](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/aehulet/32/12906_2.png) [@aehulet](https://forum.djangoproject.com/u/aehulet)\
**Post date:** [February 6, 2023, 8:11pm UTC](https://forum.djangoproject.com/t/database-object-fails-to-execute-create-method/18679/1 "2023-02-06T20:11:48Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 6, 2023, 8:41pm UTC](https://forum.djangoproject.com/t/database-object-fails-to-execute-create-method/18679/2 "2023-02-06T20:41:09Z")

</div>

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](https://docs.djangoproject.com/en/4.1/ref/models/querysets/#create) 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?

---

<div class="post-metadata">

**Author:** ![aehulet](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/aehulet/32/12906_2.png) [@aehulet](https://forum.djangoproject.com/u/aehulet)\
**Post date:** [February 6, 2023, 9:25pm UTC](https://forum.djangoproject.com/t/database-object-fails-to-execute-create-method/18679/3 "2023-02-06T21:25:29Z")

</div>

If you’re asking literally, you can see all the code at [GitHub - aehulet/sparqlBuilder2: prototype application](https://github.com/aehulet/sparqlBuilder2). 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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 6, 2023, 9:42pm UTC](https://forum.djangoproject.com/t/database-object-fails-to-execute-create-method/18679/4 "2023-02-06T21:42:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![aehulet](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/aehulet/32/12906_2.png) [@aehulet](https://forum.djangoproject.com/u/aehulet)\
**Post date:** [February 6, 2023, 10:03pm UTC](https://forum.djangoproject.com/t/database-object-fails-to-execute-create-method/18679/5 "2023-02-06T22:03:31Z")

</div>

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

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 6, 2023, 10:45pm UTC](https://forum.djangoproject.com/t/database-object-fails-to-execute-create-method/18679/6 "2023-02-06T22:45:49Z")

</div>

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](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:

> [@aehulet](#):
>
> ```auto
> elements = re.split(r'\/', request.path)
> q_code = elements.pop(1)
> 
> ```

The variable `item_id` _is_ the parameter you’re looking for.

---

<div class="post-metadata">

**Author:** ![aehulet](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/aehulet/32/12906_2.png) [@aehulet](https://forum.djangoproject.com/u/aehulet)\
**Post date:** [February 7, 2023, 2:18pm UTC](https://forum.djangoproject.com/t/database-object-fails-to-execute-create-method/18679/7 "2023-02-07T14:18:32Z")

</div>

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

```auto
       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. 😉
