How to fix TypeError: __init__() missing 1 required positional argument: 'on_delete'

2,521 views
Skip to first unread message

Abba Haruna

unread,
Nov 22, 2018, 2:50:42 PM11/22/18
to Django users
from django.db import models

class Food(models.Model):
name = models.CharField(max_length=30)
picture = models.ImageField(upload_to='images/', null=True)
def __unicode__(self):
return self.name
class Town(models.Model):
name = models.CharField(max_length=30)

def __unicode__(self):
return self.name
class Restaurant(models.Model):
name = models.CharField(max_length=100)
desc = models.CharField(max_length=100)
menu = models.CharField(max_length=100)
web = models.CharField(max_length=100)
phone = models.CharField(max_length=40)
address = models.CharField(max_length=100)
post_code = models.CharField(max_length=20)
picture = models.ImageField(upload_to='images/', null=True)
map = models.ImageField(upload_to='images/', null=True)
gmap_url = models.CharField(max_length=200, null=True)
food = models.ForeignKey(Food)
town = models.ForeignKey(Town)
STARS = ((1,'one'),
(2,'two'),
(3,'three'),
(4,'four'),)
votes = models.IntegerField(choices=STARS, default=4)
def __unicode__(self):
return self.name
def __str__(self):

vineeth sagar

unread,
Nov 22, 2018, 3:07:12 PM11/22/18
to django...@googlegroups.com
For a foreign keys you have to specify an on_delete attribute,

food=models.ForiegnKey(Food,on_delete=models.CASCADE)

If a row in the parent table(food for example) is deleted this will cause the row/s that refers to that particular food item in your Restraunt model be deleted.

on_delete can be given other options as SET_NULL,DO_NOTHING. Refer the docs they are extensive on this topic.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/de6ad3df-e330-43e1-875a-58895e245851%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jason

unread,
Nov 22, 2018, 10:39:01 PM11/22/18
to Django users
on_delete is a required parameter now in django 2.0


  • The on_delete argument for ForeignKey and OneToOneField is now required in models and migrations. Consider squashing migrations so that you have fewer of them to update.

Abba Haruna

unread,
Nov 26, 2018, 8:30:15 AM11/26/18
to Django users
thank you
Reply all
Reply to author
Forward
0 new messages