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

Christopher Gunawan
Christopher Gunawan
10,754 Points

I don't quite understand the dynamics of this inheritance

If my web app (templates and views) are calling the Step model, how does it know the return the Text.content? If Text inherits from Step, shouldn't Step.content fail?

Christopher Gunawan
Christopher Gunawan
10,754 Points

For example:

def step_detail(request, course_pk, step_pk):
    step = get_object_or_404(Step, course_id=course_pk, pk=step_pk)
    return render(request, 'courses/step_detail.html', {'step': step})

You can't query Step anymore right? You want all Text now, not Steps. If Text inherits from Step and now the other way around, how does this still work?

1 Answer

Richard Li
Richard Li
9,751 Points

Step is abstract right? so it is technically not a stand-alone model to use any more. It can only work as a structure for other models.

For the model that created on Step like Text, it will be an stand-alone model who has a table in the database and all the attribute and method of Step plus its own.

As said in the Django Docs:

Abstract base classes are useful when you want to put some common information into a number of other models.

You write your base class and put abstract=True in the Meta class.

This model will then not be used to create any database table. Instead, when it is used as a base class for other models, its fields will be added to those of the child class.