Categories
Python Answers

How to revert the last migration with Python Django?

Sometimes, we want to revert the last migration with Python Django.

In this article, we’ll look at how to revert the last migration with Python Django.

How to revert the last migration with Python Django?

To revert the last migration with Python Django, we can use the migrate command.

For instance, if we have migrations

  • 0010_previous_migration
  • 0011_migration_to_revert

Then we run

./manage.py migrate my_app 0010_previous_migration 

to revert our database back to the state when 0010_previous_migration was run.

Conclusion

To revert the last migration with Python Django, we can use the migrate command.

Categories
Python Answers

How to do a not equal in Python Django queryset filtering?

Sometimes, we want to do a not equal in Python Django queryset filtering.

In this article, we’ll look at how to do a not equal in Python Django queryset filtering.

How to do a not equal in Python Django queryset filtering?

To do a not equal in Python Django queryset filtering, we can negate a equal with ~.

For instance, we write

from myapp.models import Entry
from django.db.models import Q

Entry.objects.filter(~Q(id=3))

to call filter with the Q object negated with ~ to return all the Entry results that don’t have id 3.

Conclusion

To do a not equal in Python Django queryset filtering, we can negate a equal with ~.

Categories
Python Answers

How to compare dates in Python Django templates?

Sometimes, we want to compare dates in Python Django templates.

In this article, we’ll look at how to compare dates in Python Django templates.

How to compare dates in Python Django templates?

To compare dates in Python Django templates, we can add a property into our model to compare the dates.

For instance, we write

from datetime import date

@property
def is_past_due(self):
    return date.today() > self.date

to add the is_past_due computed property into our model that returns if date.today is bigger than `self.date.

Then in our template, we can use it by writing

{% if listing.is_past_due %}
    In the past
{% else %}
    {{ listing.date|date:"d M Y" }}
{% endif %}

Conclusion

To compare dates in Python Django templates, we can add a property into our model to compare the dates.

Categories
Python Answers

How to output Python Django queryset as JSON?

Sometimes, we want to output Python Django queryset as JSON.

In this article, we’ll look at how to output Python Django queryset as JSON.

How to output Python Django queryset as JSON?

To output Python Django queryset as JSON, we can serialize our queryset as JSON with Django’s built in serializers.

For instance, we write

from django.core import serializers
from django.http import HttpResponse

def some_view(request):
    qs = SomeModel.objects.all()
    qs_json = serializers.serialize('json', qs)
    return HttpResponse(qs_json, content_type='application/json')

to call serializers.serialize with 'json' and queryset qs to return the qs_json JSON string.

And then we create a HttpResponse object with that and return it as the response in our view.

Conclusion

To output Python Django queryset as JSON, we can serialize our queryset as JSON with Django’s built in serializers.

Categories
Python Answers

How to fix Python Django TemplateDoesNotExist error?

To fix Python Django TemplateDoesNotExist error, we can register the template directories in our app.

For instance, in settings.py, we add

TEMPLATE_DIRS = (
    os.path.join(SETTINGS_PATH, 'templates'),
)

to register the templates directory as the directory with the templates.