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:

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

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:

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

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:

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

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:

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

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)

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

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:

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

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