Categories
Fixes

Update a model field – Django

I noticed a typo in a field name after running migrations, I followed these steps to update the field without loosing data.

  1. Update the filed name in the model with the desired name (taking note of the current name)
  2. Create an empty migration. If you have previous migrations, this one must be named in such a way that it comes last in the migrations list. eg if you already have 001_users and 002_emails then this new migration needs to be named 003_new_name

The new migration should look like this:


from __future__ import unicode_literals

from django.db import migrations
class Migration(migrations.Migration):

dependencies = [
('django_app_name', 'last_previous_migration'), # last_previous_migration is the migration immediately before this new empty migration without the .py extension
]

operations = [
migrations.RenameField(
model_name='model_name',
old_name='old_filed_name', #that was taken note of from step one above
new_name='description', #the new/desired field name
),
]

3. run migrations

 $ python manage.py migrate 

This will update the field.

Hope this saves someone some googling time



		
Categories
Fixes Hacks

virtualenv for python3

Simple steps to setup virtualenv for Python3

To setup virtualenv for python3 for Django projects, create and cd into the desired folder for the Django project.

Create a folder for virtualenv, eg mkdir venv

$ cd venv // The created folder for virtualenv 

Then run these commands worked for me in this order:

$ virtualenv -p python3 . // setup virtualenv with python3 fin current directory 
$ source bin/activate // ativate virtualenv 

At this point, checking python version will give python3

Install Django:

$ pip install django 

To see all installations run :

$ pip freeze //Django should be included in the list of installations 

Start Django project

$ django-admin startproject --projectname 

cd into created project and run:

$ python manage.py runserver 

More on virtualenv

 

 

Categories
Fixes Hacks

Fix IOError: [Errno 2] No such file or directory error for appdirs

Appdirs is a small Python module for determining appropriate platform-specific dirs, e.g. a “user data dir”.

Github repo – https://github.com/ActiveState/appdirs

This error from my experience occurred when I updated my local branch from origin and ran pip install to get the latest installed packages.

pip tried to upgrade appdir but it failed for some unknown reasons, then the error came up:

IOError: [Errno 2] No such file or directory: ‘/.local/lib/python2.7/site-packages/appdirs-1.4.0.dist-info/METADATA’

What worked for me was running

$ pip install appdirs --upgrade 
then:
$ pip install -r requirements.txt --upgrade 

The issue was addressed here:

https://github.com/ActiveState/appdirs/issues/89