Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Using Databases in Python Meet Peewee Queries Are Your Friend

nicole lumpkin
PLUS
nicole lumpkin
Courses Plus Student 5,328 Points

What kind of object is student_record?

I am struggling to understand what kind of object student_record is. So far my understanding is that a Student object translates to three parts that create a whole table.

  1. The table itself (Student)
  2. A column (username)
  3. A column (points)

In the except block, student_record is defined as the following

except IntegrityError:
    student_record = Student.get(username=student['username'])
    student_record.points = student['points']
    student_record.save()

This is where I lose the thread of reasoning re: what kind of obj student_record is. Working from within the parenthesis outwards, I know student['username'] accesses a string value(let's say 'Kenneth'). So now we have the following

student_record = Student.get(username='Kenneth')
  1. I'm assuming the .get() is a query? Y/n
  2. So when we query, do we always use a unique attribute/column as the query?
  3. Is this considered a query instance/object?

-An instance/obj that acts as a handle to the whole row in which it resides?

-An object that allows us to gain access to other attributes/columns within the same row via dot notation? The points attribute for example?

student_record.points = student['points']

Whoever answers this, I'd like to thank you in advance!

Drew Pierce
Drew Pierce
2,079 Points

it's been since I may have done this one but but it sounds like student_record would be a row in the table.

Jorge Gimeno
Jorge Gimeno
5,956 Points

According to peewee's documentation, it returns an instance of Model. http://docs.peewee-orm.com/en/latest/peewee/api.html#Model.get

-Jorge

1 Answer

Hi to add to Jorge answer student_record is an instance of the Student class which inherits from the Peewee model class i.e not sure what you are looking at, hwoever, I would presume the Student class looks something like this

class Student(Model):
    username = CharField(unique=True)
    points = IntegerField()

student_record is an obj oreturned by the Student class based on the query username == 'Kenneth' get() is a query and gets a single record. If you want to get all students with points greate that 10 you would use a SQL query such as:

max_points = Student.select().where(Student.point => 10).order_by(Student.username)

if you want to get all entries in Student you can just use

Student.select()

I hope this helps a little and excuse me if I stated something incorrect :)