How to revert the last migration?

You can revert by migrating to the previous migration. For example, if your last two migrations are: 0010_previous_migration 0011_migration_to_revert Then you would do: ./manage.py migrate my_app 0010_previous_migration You don’t actually need to use the full migration name, the number is enough, i.e. ./manage.py migrate my_app 0010 You can then delete migration 0011_migration_to_revert. If you’re using … Read more

How to check Django version

Django 1.5 supports Python 2.6.5 and later. If you’re under Linux and want to check the Python version you’re using, run python -V from the command line. If you want to check the Django version, open a Python console and type >>> import django >>> django.VERSION (2, 0, 0, ‘final’, 0)

How to upload a file in Django? [closed]

Phew, Django documentation really does not have good example about this. I spent over 2 hours to dig up all the pieces to understand how this works. With that knowledge I implemented a project that makes possible to upload files and show them as list. To download source for the project, visit https://github.com/axelpale/minimal-django-file-upload-example or clone … Read more

How can I combine two or more querysets in a Django view?

Concatenating the querysets into a list is the simplest approach. If the database will be hit for all querysets anyway (e.g. because the result needs to be sorted), this won’t add further cost. from itertools import chain result_list = list(chain(page_list, article_list, post_list)) Using itertools.chain is faster than looping each list and appending elements one by … Read more

How do I do a not equal in Django queryset filtering?

You can use Q objects for this. They can be negated with the ~ operator and combined much like normal Python expressions: from myapp.models import Entry from django.db.models import Q Entry.objects.filter(~Q(id=3)) will return all entries except the one(s) with 3 as their ID: [<Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, …]

Is there a list of Pytz Timezones?

You can list all the available timezones with pytz.all_timezones: In [40]: import pytz In [41]: pytz.all_timezones Out[42]: [‘Africa/Abidjan’, ‘Africa/Accra’, ‘Africa/Addis_Ababa’, …] There is also pytz.common_timezones: In [45]: len(pytz.common_timezones) Out[45]: 403 In [46]: len(pytz.all_timezones) Out[46]: 563

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)