I have a class named Customer:
class Customer(models.Model):
# One to one relationship: customer is a user
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
name = models.CharField(max_length=200, null=True)
email = models.CharField(max_length=200)
# Returns the string representation of an object
def __str__(self):
return self.name
... And a class named Order (customer can have many orders).
class Order(models.Model):
objects = None
# many to one relationship: customer can have many orders
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, blank=True, null=True)
date_order = models.DateTimeField(auto_now_add=True)
complete = models.BooleanField(default=False, null=True, blank=False)
transaction_id = models.CharField(max_length=200, null=True)
# You can access the parent class from inside a method of a child class by using super()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.id = None
def __str__(self):
return str(self.id)
When I am trying to remove the object of class Customer I have created, named Kasia (which is probably superuser as well) I cannot do it. I have previously deleted this customer's orders and now they are set to Null value. There is a type error and a message: 'Model instances without primary key values are unhashable'. The exception location is as follows: C:\Users\kasia\AppData\Roaming\Python\Python37\site-packages\django\db\models\base.py, line 536, in hash
When I want to delete user, the same thing happens. What is happening here?