In-Class Activity 3: The Django Shell:

Objective: Define and create a new model (the Entry model) and connect it to an existing model (the Topic model) to form a one-to-many relationship, meaning many entries can be associated with one topic. Use the Groq API to populate the database with entries.

Description: To record into our code journal project what topics we’ve been learning about (e.g., Data_Types and Lists) we need to define a model for the kinds of entries we can make in the journal. In other words, each entry needs to be associated with a particular topic.

  • Make a copy of your Activity2 folder and name the folder Activity3-DjangoShell_DB. After the activity is completed, take a screenshot matching the solution image below and submit it to the first quiz question on Canvas.
  • Navigate to the directory that contains manage.py and open the folder in VS code.

  1. In the models.py file, define a second model, Entry with 3 fields (attributes) topic, text and date_added.
    • The topic attribute should have a ForeignKey datatype
    • The text attribute should have a TextField datatype.
    • The date_added attribute should have a DateTimeField datatype.
  2. Nest the Meta class inside the Entry class so that multiple entries will appear correctly
  3. Define another __str(self)__ function to show the text of the entry.
  4. After migrating the database, register the Entry model with the Admin Site (in the admin.py file)
  5. Use Groq to create your entries. In the activity root folder (the same folder as manage.py), create a file named populate_entries.py and build the script by following the Add Entries with Groq page.
    • Paste in your own Groq API key, then run the script with python populate_entries.py.
    • The script should create 5 entries for Data_Types and 5 entries for Lists. Only run it once.
  6. Login to the admin site (http://localhost:8000/admin) and confirm your entries were created. To do so, click the Entries link.
    • You should see 10 entries listed. Click an entry to confirm it is connected to the correct topic.

Django admin Add entry page in a desktop web browser. The page shows 10 entries.