Home > front end >  accessing an element of a django database object
accessing an element of a django database object

Time:10-21

I am trying to access an element of a django database table object. My under standing is the structure is as follows:

Database table -> object -> elements.

I can access the Database object that i want by doing the following:

    wanted_object = WholeValues.objects.get(id=1)

how can I access the the elements of this database object. I have tried wanted_element = wanted_object.get(id=2) when I try to access the first element I get an error. this error says WholeValues' object has no attribute 'get'

My model for the object is as follows:

class WholeValues(models.Model):
    a_count = models.FloatField()
    b_count = models.FloatField()
    c_count = models.FloatField()

in my specific case I want to access the b_count value.

CodePudding user response:

wanted_object is a WholeValues instance.
you can access to its fields or methods like any normal python object.

# Get object where `id = 1` from database
wanted_object = WholeValues.objects.get(id=1)

# object.<field_name> to access object fields
print(wanted_object.a_count)
  • Related