Search code examples
djangodjango-rest-frameworkdjango-viewsdjango-filterdjango-3.0

Automatic publication of a post


In my blog I've a filter that put online only the posts that aren't draft and that aren't published in the future.

models.py

class BlogPost(models.Model):
    title =....
    ....
    publishing_date = models.DateTimeField(
        default=timezone.now,
    )
    draft = models.BooleanField(
        default=False,
    )

views.py

@api_view(["GET"])
def blogPost_apiview(request):

    if request.method == "GET":
        objects = BlogPost.objects.filter(Q(draft=False) & Q(publishing_date__lte=datetime.now()))
        serializer = BlogPostSerializer(objects, many=True)
        return Response(serializer.data)

I've seen that when the post goes from future to past it is not put online. I can see the post online only if I change manually the publishing date and time and save it.

How can I make it happen automatically?


Solution

  • I've solved changing & with and. Maybe is a bug?