'Django datetime fromisoformat: argument must be str
Hi I had this error while try to running server. It may cause by the datetime field. Can someone check it for me.

models.py
class Post(models.Model):
author = models.ForeignKey("auth.User", on_delete=models.CASCADE)
title = models.CharField(max_length=200)
text = models.TextField()
create_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
def get_absolute_url(self):
return reverse("post_detail", kwargs={"pk": self.pk})
def publish(self):
self.published_date = timezone.now
self.save()
def approve_comments(self):
return self.comments.filter(approve_comment=True)
def __str__(self):
return self.title
class Comment(models.Model):
post = models.ForeignKey('mblog.Post', related_name='comments', on_delete=models.CASCADE)
author = models.CharField(max_length=100)
text = models.TextField()
create_date = models.DateTimeField(default=timezone.now)
approve_comment = models.BooleanField(default=False)
def approve(self):
self.approve_comment = True
self.save()
def get_absolute_url(self):
return reverse("post_detail", kwargs={"pk": self.pk})
def __str__(self):
return self.text
views.py
class PostListView(ListView):
model = models.Post
def get_queryset(self):
return models.Post.objects.filter(published_date__lte=timezone).order_by('-published_date')
class DraftListView(LoginRequiredMixin,ListView):
login_url = '/login/'
redirect_field_name = 'mblog/post_list.html'
model = models.Post
def get_queryset(self):
return models.Post.objects.filter(published_date__isnull=True).order_by('created_date')
And had anyway better to set default for the DateTimeField? Thanks for your time.
Solution 1:[1]
I guess you also go through the udemy course, got the same error myself.
Probably not relevant to you anymore, but if someone else had the same problem, it's just a case of adding parenthesis () after timezone.now in Post class in publish method
so it looks like this:
def publish(self):
self.published_date = timezone.now()
self.save()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Baba Tova |
