__init__() got an unexpected keyword argument 'on delete' when make migrations

62 views
Skip to first unread message

Khánh Hoàng

unread,
Dec 5, 2021, 10:36:11 AM12/5/21
to Django users
Hi everyone, I am new to programming, more than 4 months, I had an issue when making  migrations for my apps. 

I run :" python manage.py makemigrations " and  I got a error.
 It said:  TypeError: __init__() got an unexpected keyword argument 'on delete'
I'm Using Django 3.2.9 
Can someone explain it for me? And how to fix it.

Thanks and Regards,
Khanh Hoang

Uzzal Hossain

unread,
Dec 5, 2021, 10:38:43 AM12/5/21
to django...@googlegroups.com
Change on delete to on_delete in your model

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b5adebe5-05b0-4c33-b164-85fda53e13cbn%40googlegroups.com.

Samuel Nogueira

unread,
Dec 5, 2021, 10:39:21 AM12/5/21
to django...@googlegroups.com
Please, can you take some prints of your models?

--

Khánh Hoàng

unread,
Dec 5, 2021, 8:08:28 PM12/5/21
to Django users
from ctypes import DEFAULT_MODE
from django.db import models
from django.db.models.deletion import CASCADE, PROTECT
from django.db.models.expressions import Col
from django.db.models.fields import PositiveSmallIntegerField
from django.utils.translation import pgettext_lazy


class Promotions(models.Model):
    description = models.CharField(max_length=255)
    discount = models.FloatField()


class Collection(models.Model):
    title = models.CharField(max_length=255)


class Product(models.Model):
    title = models.CharField(max_length=255)
    descriptions = models.TextField()
    price = models.DecimalField(max_digits=5, decimal_places=2)
    inventory = models.IntegerField()
    last_update = models.DateTimeField(auto_now=True)
    collection = models.ForeignKey(Collection, on_delete=models.PROTECT)
    promotion = models.ManyToManyField(Promotions, on_delete=models.CASCADE)


class Customer(models.Model):

    MEMBERSHIP_BRONZE = "B",
    MEMBERSHIP_SILEVR = "S",
    MEMBERSHIP_GOLD = "G"
    MEMBERSHIP_CHOICES = [
        ('B', 'Bronze'),
        ('S', 'Silver'),
        ('G', 'Gold')
    ]

    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    email = models.EmailField(unique=True)
    phone = models.CharField(max_length=255)
    birth_date = models.DateField(null=True)
    membership = models.CharField(
        max_length=1, choices=MEMBERSHIP_CHOICES, default=MEMBERSHIP_BRONZE)


class Order(models.Model):

    PAYMENT_STATUS_PENDING = "P"
    PAYMENT_STATUS_COMPLETE = "C"
    PAYMENT_STATUS_FAILED = "F"

    PEYMENT_STATUS_CHOICES = [
        (PAYMENT_STATUS_PENDING, "Pending"),
        (PAYMENT_STATUS_COMPLETE, "Complete"),
        (PAYMENT_STATUS_FAILED, "Failed")
    ]

    placed_at = models.DateTimeField(auto_now_add=True)
    payment_status = models.CharField(
        max_length=1, choices=PEYMENT_STATUS_CHOICES, default=PAYMENT_STATUS_PENDING)
    customer = models.ForeignKey(Customer, on_delete=models.PROTECT)


class Address(models.Model):
    street = models.CharField(max_length=255)
    city = models.CharField(max_length=255)
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)


class Cart(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)


class OrderItem(models.Model):
    order = models.ForeignKey(Order, on_delete=models.PROTECT)
    product = models.ForeignKey(Product, on_delete=models.PROTECT)
    quantity = models.PositiveSmallIntegerField()
    unit_price = models.DecimalField(max_digits=6, decimal_places=2)


class CartItem(models.Model):
    cart = models.ForeignKey(Cart, on_delete=models.CASCADE)
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    quantity = models.PositiveSmallIntegerField()



here is my  models.py. 
Vào lúc 22:39:21 UTC+7 ngày Chủ Nhật, 5 tháng 12, 2021, hu3mu...@gmail.com đã viết:

Anurag Agarwal

unread,
Dec 5, 2021, 8:23:47 PM12/5/21
to django...@googlegroups.com
Why are you fucking passing the args
On_delete in manytomany . Class

  promotion = models.ManyToManyField(Promotions, on_delete=models.CASCADE)


Khánh Hoàng

unread,
Dec 5, 2021, 9:44:11 PM12/5/21
to Django users

Oh, 
Thank you very much, I realized my mistake. 

Vào lúc 08:23:47 UTC+7 ngày Thứ Hai, 6 tháng 12, 2021, anurag....@gmail.com đã viết:

Thomas Lockhart

unread,
Dec 5, 2021, 10:35:41 PM12/5/21
to anurag....@gmail.com, django...@googlegroups.com
Please use polite language. Thank you.

- Tom

Aashish Kumar

unread,
Dec 5, 2021, 11:43:22 PM12/5/21
to django...@googlegroups.com, anurag....@gmail.com
ManyToManyField doesn’t need on_delete attribute 

Remove on_delete attribute 

Khánh Hoàng

unread,
Dec 6, 2021, 3:23:57 AM12/6/21
to Django users

Yes, I found it.
Thank you for replying.
Vào lúc 11:43:22 UTC+7 ngày Thứ Hai, 6 tháng 12, 2021, aashishk...@gmail.com đã viết:
Reply all
Reply to author
Forward
0 new messages