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 trialDaniel Petrov
3,495 PointsFew questions here
-
Could we define search_query=None in def view_entries(search_query=None): as a variable instead of a function argument? We did so in
def menu_loop(): """"Show the menu.""" choice = None
What is the difference?
Why don't we use init method in the Entry class?
Which function creates the DB - SqliteDatabase('diary.db')? As it says:
def initialize():
"""Create the database and the table if they don't exist.""" <---
db.connect()
db.create_tables([Entry], safe=True)
My understanding is that it connects to the already existing db and creates the table with two columns by calling the class Entry.
4.When is the timestamp created? Is it here:
if data:
if input('Save entry? [Yn] ').lower() != 'n':
Entry.create(content=data) <---
print('Saved successfully')
In this case my logic says that it should be:
class Entry(Model):
content = TextField()
timestamp = DateTimeField()
to create two empty columns and then:
if data:
if input('Save entry? [Yn] ').lower() != 'n':
Entry.create(content=data, timestamp=datetime.datetime.now) <---
print('Saved successfully')
I tried and it did not work.
Kenneth Love , sorry if you explained these already but I could not figure it out by watching the video few times.
1 Answer
Kenneth Love
Treehouse Guest TeacherHey, let's see what I can answer with my really fuzzy memory of this course. :)
- Could we define search_query=None in def view_entries(search_query=None): as a variable instead of a function argument? We did so in
def menu_loop():
""""Show the menu."""
choice = None
What is the difference?
In the menu_loop
, you're waiting for input from the user. It's not logical to provide an argument to the function up front. But with viewing entries, we have two ways of using the function: either viewing everything or viewing things that match the search query. So the difference is the purpose of the function and how it's applied.
Could you use a variable inside of the function and ask the user for search terms? Sure, but that means searching requires yet another step from the users: choose view entries, then choose to search (you'd have to add this), and then enter the search term.
- Why don't we use
__init__
method in the Entry class?
It doesn't need one.
- Which function creates the DB - SqliteDatabase('diary.db')? As it says:
def initialize():
"""Create the database and the table if they don't exist.""" <---
db.connect()
db.create_tables([Entry], safe=True)
My understanding is that it connects to the already existing db and creates the table with two columns by calling the class Entry.
If the database exists, Peewee will connect to it. If it doesn't, Peewee will attempt to create the database.
4.When is the timestamp created? Is it here:
if data:
if input('Save entry? [Yn] ').lower() != 'n':
Entry.create(content=data) <---
print('Saved successfully')
In this case my logic says that it should be:
class Entry(Model):
content = TextField()
timestamp = DateTimeField()
to create two empty columns and then:
if data:
if input('Save entry? [Yn] ').lower() != 'n':
Entry.create(content=data, timestamp=datetime.datetime.now) <---
print('Saved successfully')
I tried and it did not work.
In my reference code, the Entry
class' timestamp
field has a default
value. This value is used if one isn't provided.
Yours fails there because you're providing the field a function/method (now
is a method on the datetime
object) instead of a datetime
instance. In the field, thedefault
is provided with the parentheses because the model will call it when it instantiates an instance.
Daniel Petrov
3,495 PointsDaniel Petrov
3,495 PointsThank you, Kenneth!