-2

I have created an expiredate field which is generated automatically based on the registerdate field

I've tried a regular query just like i would use for any field (it is mentioned below)

this is the model:

class UserProfile(models.Model):
    course = models.ForeignKey(Category, on_delete=models.DO_NOTHING)
    phonenumber = models.CharField(max_length=50)
    registerdate = models.DateTimeField()
    expiredate = timezone.now().date() + timedelta(days= 70)
    user = models.OneToOneField(User, on_delete = models.CASCADE)

def __str__(self):
    return "{}" .format(self.user)

and this is the query i'm trying to use:

expire = UserProfile.objects.values('expiredate')

I simply want to select the expiredate and use it on my views.py function but the query generates an error:

'Cannot resolve keyword 'expiredate' into field. Choices are: course, course_id, id, phonenumber, registerdate, user, user_id'
xxbinxx
  • 1,527
  • 11
  • 18
  • I forgot to mention that the expire date doesn't show on the admin site. If it has anything to do with the fact that i can't use a simple query to get the value of a field from the model please explain it in the comments ! – Pàrís Źarka May 30 '19 at 10:23
  • `expiratedate` is not field but just a class attribute. How are you so sure it is a field. In simple words I'll explain, Can you see it in django admin? No right. So, it's not a field. – xxbinxx May 30 '19 at 10:42
  • Tell us the purpose of this field. I guess you've created your model in wrong way. – xxbinxx May 30 '19 at 10:43
  • i want to use it because the users that are going to register will only have their accounts active for a period of time (70 days). and this model extends the django User model because i wanted to add some more details on the registerd users than just the ones that are given by django – Pàrís Źarka May 30 '19 at 10:50

2 Answers2

-1

expiredate can be a DateField.

class UserProfile(models.Model):
    expiredate = models.DateField(default=timezone.now().date() + timedelta(days= 70))

python manage.py makemigrations

python manage.py migrate

katoozi
  • 388
  • 2
  • 6
  • Why not use `DateTimeField`?? If a user registers on 1st june at 11:00pm, the account should expire on 9 Aug at 11:00pm (exactly 70 days), not on 9Aug at 12:00am (user is loosing 11 hours of usage). @PàrísŹarka - you also give it a thought. – xxbinxx May 30 '19 at 12:40
-1

Solution by @katoozi force default value to be same until application restarted.

Use callable to get correct value:

def get_expiredate():
    return timezone.now().date() + timedelta(days= 70)

class UserProfile(models.Model):
    expiredate = models.DateField(default=get_expiredate)

Take a look at How to set a Django model field's default value to a function call / callable (e.g., a date relative to the time of model object creation)

Amaro Vita
  • 437
  • 3
  • 9