I am trying to make a website where I have a very basic Subscription system. I have different plans (30, 60, 90 and 365 days) and I would like to save, upon the user payment, the duration of the subscription plan. But I am receiving this weird response, probably is something wrong in my model but I couldn't fix it. Here is some code.
models.py
from datetime import datetime as dt, date
DURATION_CHOICES = (
(30, 30),
(60, 60),
(180, 180),
(365, 365),
)
class Subscription(models.Model):
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
issue_date = models.DateTimeField(auto_now_add=True)
duration = models.IntegerField(choices=DURATION_CHOICES)
expiration_date = models.DateTimeField()
def __str__(self):
return f'{self.user} - {str(self.expiration_date)}'
def get_expiration(self):
issue_date = date(self.issue_date)
expiration_date = issue_date dt.timedelta(days=self.duration)
return expiration_date
def save(self, *args, **kwargs):
self.expiration_date = self.get_expiration
super(Subscription, self).save(*args, **kwargs)
views.py
def new_subscription(request, days):
user = request.user
subscription = Subscription(user=user, duration=days)
subscription.save()
messages.success(request, "Subscription successfully obtained")
return HttpResponseRedirect(reverse('main:index'))
I know there is probably something wrong in my save method or get_expiration method but I couldn't find it.
CodePudding user response:
In your save()
method in first line you have this:
self.expiration_date = self.get_expiration
However it must changed to this:
self.expiration_date = self.get_expiration()
Explanation:
In your save()
method, you were assigning something like <function a at 0x100d1c1f0>
which is a reference to memory, and it means that you are assigning a function to self.expiration_date
, while you need to assign a value to it.