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 Customizing Django Templates Template Tags and Filters Handy Dandy Filters

I don't really understand te meaning of step_set.all ยฟwhere can I get more references?

I'm trying to look for any other reference of it but I did't find it out in Django help or webpage. My real problem is that I'm trying to adapt the code to my own project but it doesn't really work.

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

The use of the "_set" suffix is to reference foreign key relationships in reverse. Since Step has a foreign key to Course and is referred to as step.course, the reverse relationship is automatically created for you by Django. A Course can refer to all Step objects that point to it using the "_set" notation as course.step_set.

The ".all()" part means all records. It could also have been a filter or a get

More information in the docs

Thanks Chris. It was very difficult to me to find out this reference in Django Documentation. I appreciate your answer very much.

Arindam Roychowdhury
Arindam Roychowdhury
3,244 Points

Chris Freeman Thanks for the reply. Migrations have been done. I tried again just to check if anything is pending. May be I should paste here my models structure too.

class Step(models.Model): order = models.IntegerField(default=0) title = models.CharField(max_length=200) description = models.TextField() course = models.ForeignKey(Course)

class Meta:
    abstract = True  # To make this class an abstract class which is a schema for other models
    ordering = ['order', ]

def __str__(self):
    return self.title

class Text(Step): content = models.TextField(blank=True, default='')

def get_absolute_url(self):
    return reverse(
        'courses:text_details',
        kwargs={'course_pk': self.course_id, 'step_pk': self.id}
    )

class Quiz(Step): total_questions = models.IntegerField(default=4)

class Meta:
    verbose_name_plural = 'Quizzes'

def get_absolute_url(self):
    return reverse(
        'courses:quiz_details',
        kwargs={'course_pk': self.course_id, 'step_pk': self.id}
    )
Arindam Roychowdhury
Arindam Roychowdhury
3,244 Points

Hi ,

For me , the step_set doesn't work..Here is the code as tried in shell.

>>> from courses.models import Course
>>> c_all = Course.objects.all()
>>> c_all
<QuerySet [<Course: Python Basics>, <Course: Testing in Python>]>
>>> c_all[0]
<Course: Python Basics>
>>> c_all[0].step_set
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'Course' object has no attribute 'step_set'


>>> dir(c1)
oesNotExist', 'MultipleObjectsReturned', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', u'__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '_
bclasshook__', '__weakref__', '_check_column_name_clashes', '_check_field_name_clashes', '_check_fields', '_check_id_field', '_check_index_together', '_check_local_fields', '_check_long_column_names', '_check_m2m_through_same_relationship', '_check_managers', '_check_model', '_check_model_name_db
okup_clashes', '_check_ordering', '_check_swappable', '_check_unique_together', '_do_insert', '_do_update', '_get_FIELD_display', '_get_next_or_previous_by_FIELD', '_get_next_or_previous_in_order', '_get_pk_val', '_get_unique_checks', '_meta', '_perform_date_checks', '_perform_unique_checks', '_s
_parents', '_save_table', '_set_pk_val', '_state', 'check', 'clean', 'clean_fields', 'created_at', 'date_error_message', 'delete', 'description', 'from_db', 'full_clean', 'get_deferred_fields', 'get_next_by_created_at', 'get_previous_by_created_at', 'id', 'objects', 'pk', 'prepare_database_save',
uiz_set', 'refresh_from_db', 'save', 'save_base', 'serializable_value', 'text_set', 'title', 'unique_error_message', 'validate_unique']

[MOD: added ```python formatting -cf]

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Has the database been migrated to include the Step class and its ForeignKey to association to Course?