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 Django Basics Django Templates Add a Detail View

Oprea Mihai
Oprea Mihai
5,592 Points

ForeignKey

When I run Step with ForeignKey in my Pycharm i get an __init()__ error. Can you help me ?

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


    def __str__(self):
        return self.title

# error:
TypeError: __init__() missing 1 required positional argument: 'on_delete'

[MOD: Added ``` python formatting and ` escapes on the dunder -cf]

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

Starting in Django 2.0, the on_delete is required for ForeignKeys. Change your usage to:

    course=models.ForeignKey(Course, on_delete=models.DO_NOTHING, )

Information in the Django docs ForeignKey.

I found the original help on this JetBrains forum post