Adding Fields and Choices with Codex

Introduction

In previous unit, you learned how to build complete features that require coordination across multiple files. You discovered how to plan features and coordinate changes across models, views, templates, and migrations.

Now, you'll learn how to prompt Codex effectively for Django-specific features like model choices and filtering. To prompt Codex well, you need to understand the Django concepts you're asking for. This lesson teaches you both the Django concepts and how to prompt Codex effectively for them.

Understanding Django Model Choices

Django model choices let you define a set of predefined options for a field. Instead of allowing any text, users select from a fixed list. This ensures data consistency and makes forms easier to use.

A model choice field has two parts:

  • Value (stored in database): 'work', 'personal', 'shopping'
  • Label (displayed to users): 'Work', 'Personal', 'Shopping'

When prompting Codex, be specific about both the values and labels:

In core/models.py, add a category field to the Task model with choices: work, personal, shopping, health, and other. Use CharField with choices parameter. The choices should be tuples of (value, label) pairs.

Codex will generate the proper structure. You can also ask Codex to explain:

Explain Django model choices and show me the tuple format for categories: work, personal, shopping, health, and other.

Codex will explain the structure, which helps you understand what you're asking for.

Prompting Codex for Dropdowns in Templates

After adding a field with choices, you need a dropdown in your form. When prompting Codex, specify both the field and the choices source:

In core/templates/task_list.html, add a category dropdown select in the task creation form. Use the choices from Task.CATEGORY_CHOICES. Style it to match the existing form inputs.

Codex understands the Django context and will create the dropdown correctly. You can also ask for specific styling:

Make the category dropdown match the styling of the task title input field - same padding, border, and border radius.

Being specific about styling helps Codex match your existing design.

Prompting Codex to Display Choices Correctly

When displaying choices in templates, use Django's get_FOO_display() method to show labels, not values. When prompting Codex, specify this:

In core/templates/task_list.html, display the category for each task using get_category_display() to show the human-readable label, not the database value.

Codex will use the correct Django method. You can also ask for visual enhancements:

Display the category as a small colored badge next to each task. Use different background colors for each category: work (blue), personal (green), shopping (orange), health (red), other (gray).

Specifying both the Django method and visual styling helps Codex implement exactly what you want.

Understanding Django Filtering

Django filtering lets you query objects based on field values. You can filter by exact matches, case-insensitive contains, and more. When prompting Codex for filtering, specify the field and filter type:

In core/views.py, update the task_list view to filter tasks by category when a category is selected. Use filter(category=category) for exact matches.

Codex will implement the filtering correctly. You can also ask for more complex filtering:

In core/views.py, add filtering by priority. Allow filtering by priority level using filter(priority=priority). If no priority is selected, show all tasks.

Being specific about the filter type and behavior helps Codex implement the right logic.

Prompting Codex for Filtering in Views

When asking Codex to add filtering, be clear about:

  • Which field to filter by
  • What happens when no filter is selected
  • How filters combine (if multiple)
In core/views.py, update the task_list view to allow filtering by both category and priority. If a category is selected, filter by category. If a priority is selected, filter by priority. If both are selected, filter by both. If neither is selected, show all tasks.

Codex will implement the filtering logic correctly. You can also ask for query parameters:

In core/views.py, get the category and priority from GET parameters (request.GET.get('category') and request.GET.get('priority')). Use these to filter tasks.

Specifying the parameter source helps Codex understand the full context.

Prompting Codex for Filtering in Templates

When adding filter controls to templates, specify the form method and parameter names:

In core/templates/task_list.html, add filter dropdowns for category and priority above the task list. Use GET method forms with name="category" and name="priority". Include a "Clear Filters" button.

Codex will create the filter controls correctly. You can also ask to preserve filter values:

Make the filter dropdowns show the currently selected values. If category is 'work', the category dropdown should show 'work' as selected.

Specifying behavior details helps Codex implement the feature correctly.

Prompting Strategies for Django-Specific Features

  • Specify Django concepts: When asking for model choices, mention "CharField with choices parameter" and "tuples of (value, label) pairs".
  • Use Django method names: When asking to display choices, specify "get_FOO_display()" to show labels.
  • Specify filter types: When asking for filtering, specify "filter(category=category)" for exact matches or "filter(title__icontains=search)" for case-insensitive contains.
  • Explain parameter sources: When asking for filtering, specify "request.GET.get('category')" to get query parameters.
  • Be specific about behavior: When asking for filtering, specify what happens when no filter is selected or when multiple filters are used.

Summary and What's Next

In this lesson, you learned how to prompt Codex effectively for Django-specific features. You discovered how to understand Django model choices (values vs labels), prompt Codex for model choices by specifying both values and labels, prompt Codex for dropdowns by specifying the choices source, prompt Codex to display choices using Django's display methods, understand Django filtering concepts, prompt Codex for filtering in views by specifying filter types and parameter sources, and prompt Codex for filtering in templates by specifying form methods and behavior. You also learned effective prompting strategies: specifying Django concepts, using Django method names, specifying filter types, explaining parameter sources, and being specific about behavior.

Next, you'll practice adding these features to your task tracker: categories for organization, priorities for focus, and filtering to help users find tasks.

Move on to the practice exercises to start adding fields and choices!

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal