0

I am Building a BlogApp and I am stuck on an Error. I tried many times and tried many answers but nothing solved my error.

def validate_date(date_added):
    if date_added < timezone.now().date():
        raise ValidationError("Date cannot be in the past")

class BlogPost(models.Model):
    post_owner = models.ForeignKey(User,default='',null=True,on_delete = models.CASCADE)
    post_title = models.CharField(max_length=500,default='')
    date_added = models.DateTimeField(null=True,default=timezone.now,validators=[validate_date])

The Work of this Function

Work of this function is to prevent past date as a input.

The Problem

Whenever i try to make a new blogpost and try to add the past date then it is keep showing me :-

can't compare datetime.datetime to datetime.date

What have i tried

I tried many answers like This but nothing worked for me.

I don't know what is wrong in this code.

Any help would be appreciated.

Thank You in Advance

Lars
  • 1,234
  • 1
  • 8
  • 31
  • As i mentioned in my Question that the link you gave me was not helpful for me. – Lars Feb 18 '21 at 16:19
  • @Program Ok but the answers you've got are the same as the one you've linked: using `datetime.date()`. This is the obvious answer. If this doesn't work, show us how you're applying this solution and what error message you're getting. Otherwise people will suggest the same thing. – runDOSrun Feb 18 '21 at 16:23
  • It was two `.date() and .now().date()` so i didn't notice one in the previous. They both are same. – Lars Feb 18 '21 at 16:27

2 Answers2

1

You should convert the left operand to a date as well, so:

def validate_date(date_added):
    #               &downarrow; convert to a date 
    if date_added.date() < timezone.now().date():
        raise ValidationError('Date cannot be in the past')

or if the timestamp should not be in the past (both date and time), then we compare the datetime objects:

def validate_date(date_added):
    if date_added < timezone.now():
        raise ValidationError('Date cannot be in the past')
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • The error is gone but new error is saying `'<' not supported between instances of 'builtin_function_or_method' and 'datetime.date'` – Lars Feb 18 '21 at 16:17
  • But you need to *call* `.date`, so not `.date`, but `.date()`. – Willem Van Onsem Feb 18 '21 at 16:20
  • When i make a new `blog_post` of `now time` then it does not any error BUT when i change the` time into past` then it is showing me `The Post could not be created because the data didn't validate.` BUT it should show me the `ValidationError`. Please help me in this – Lars Feb 18 '21 at 16:31
  • @Progam: but then it is not the date that should be in the past, but the entire datetime, so `date_added < timezone.now()`. – Willem Van Onsem Feb 18 '21 at 16:33
  • I changed as you said BUT it is still showing that error. – Lars Feb 18 '21 at 16:36
  • @Progam: hold on, but what do you mean with show me the validation error? It shows it in the form right? A `ValidationError` is treated by the form. It is "catched" by the form and will not raise to the view level. – Willem Van Onsem Feb 18 '21 at 16:37
  • BUT I did according to this :- https://stackoverflow.com/questions/50002600/django-models-datefield-prevent-past AND it is saying that its in models – Lars Feb 18 '21 at 16:39
  • Django models do not validate by default. Only forms will do that for performance purposes. If you want to validate a model object, you call `.full_clean()`, so `mymodelobject.full_clean()`. – Willem Van Onsem Feb 18 '21 at 16:42
  • When i try to input past date in admin then it is showing `Validation Error` Correctly. – Lars Feb 18 '21 at 16:43
  • @Progam: and if you work with a `ModelForm`, it will also show the validation error. – Willem Van Onsem Feb 18 '21 at 16:44
  • Thanks Willem for your help – Lars Feb 18 '21 at 16:54
1

It can easily be done by converting datetime object to date object using .date() method on any datetime object.

like the following

date_added.date()
Radwan Abu-Odeh
  • 1,897
  • 9
  • 16