Reputation: 1513
class Transaction(models.Model):
slug = models.SlugField(max_length=200, db_index=True)
order_type = models.CharField(verbose_name="Order Method", max_length=200, choices=ORDER_METHODS)
paid = models.BooleanField(verbose_name="Paid", default=False)
closed = models.BooleanField(verbose_name="Closed", default=False)
created = models.DateTimeField(verbose_name="Date of creation", auto_now_add=True)
id_num = models.IntegerField(verbose_name="Transaction ID", db_index=True)
def __init__(self):
super().__init__()
self.set_transaction_id()
def set_transaction_id(self):
self.id_num = int(str(self.created.year) + str(self.created.month) + str(self.created.day).zfill(2) +\
str(self.created.hour) + str(self.created.minute).zfill(2) + str(self.created.second).zfill(2))
When I create this model in a view like this:
transaction = Transaction(order_type='Choice_1',
paid=False, closed=False)
transaction.save()
I got this error __init__() takes 1 positional argument but 8 were given
.
The method set_transaction_id()
uses the date and time to set the id_num
.
Is there something wrong with the way I declare __init__
and using super()
or calling set_transaction_id()
?
Upvotes: 1
Views: 152
Reputation: 16032
You need to use args
and kwargs
in your __init__
:
class Transaction(models.Model):
...
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_transaction_id()
The parent __init__
method of your model expects the names of each model field as positional or keyword arguments, and when you call super().__init__()
, you are trying to call it without any arguments.
Upvotes: 1