Home > Net >  Accessing model data in django
Accessing model data in django

Time:12-10

I have a a Django model:

class Myvalues(models.Model):
        items_list = models.JSONField()

I have populated this model in Django admin. I now want to access the data inside this model. However I am getting an error that says: 'Manager' object has no attribute ‘items_list' The way I am trying to access it is as follows:

Print(Myvalues.objects.items_list.values())

I don’t understand why this is coming up as the class has that model in it. Out of interest I printed the all() result of Myvalues class, like so:

print(Myvalues.objects.all())

this resulted in the following:

<QuerySet [<Myvalues: Myvalues object (1)>]>

How can I access the data that’s in my items_list model?

CodePudding user response:

Use the following syntax instead (documentation).

Myvalues.objects.values_list('items_list', flat=True)

If you want to have tuples returned, omit flat=True.

CodePudding user response:

Myvalues.objects

returns a django.db.models.manager.Manager object, not a Myvalues: Myvalues object

In order to request the items_list use

Myvalues.objects.all()[i].item_list

It returns all the Myvalue objects and picks one with the index. From this object you can get your model properties.

  • Related