Question: -Mac OSX 10.7.5
-Python 2.7 
-PostgreSQL 9.3.2 (set up via Heroku)
-Django 1.6

I've spent the entire day trying to solve this problem and have not been able to find a solution. I'm trying to get my Django app to recognize a Postgres database. The database setting in my django settings.py is 

```
DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
}

```

}

The first thing I did to get psycopg2 installed was download Macports, which was successful.Then in the terminal I typed 

sudo port install py27-psycopg2 

I didn't get any error messages, so assumed the install was successful. 

Just to check I fired up the python shell and did:

```
>>import psycopg2

```

That produced the following error:

```
ImportError: No module named psycopg2

```

I retyped "sudo port install py27-psycopg2" but got this:

```
usage: install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]
           [-o owner] file1 file2
   install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]
           [-o owner] file1 ... fileN directory
   install -d [-v] [-g group] [-m mode] [-o owner] directory ...

```

Does anybody know what might be going on here? I did read that Macports installs a different version of python and I'm wondering if perhaps my current version of python isn't recognizing psycopg2. If so, how do I activate the Macports version and would it recognize my django app?

Answer: 
> 
> How to make enum of preferencekey column which is string type in best optimized way
> 
> 
> 

Well i am afraid you cannot use your preference key as enum for 2 reasons.

1. your preference key is starting with integer values making it an in-valid identifier.
2. it also contains **-** which is not allowed either.

However, if you are able to convert your preference key to some valid identifiers then you can do this.

i would recomment using [Enum.TryParse](http://msdn.microsoft.com/en-us/library/dd991317%28v=vs.110%29.aspx) for parsing because

> 
> TryParse(String, Boolean, TEnum) is identical to the Parse(Type, String, Boolean) method, except that instead of throwing an exception, it returns false if the conversion fails. It eliminates the need for exception handling when parsing the string representation of an enumeration value.
> 
> 
> 

Something like this

```
string testingString = "Test1";
SomeEnum result;
if (Enum.TryParse(testingString, out result))
{
    Console.WriteLine("Success");
}

```

or if case sensitivity doesn't matter to you. Then you can use the overload like this

```
if (Enum.TryParse(testingString,true, out result))    
   Console.WriteLine("Success");

```

where SomeEnum is

```
public enum SomeEnum
    {
        Test1 = 1,
        Test2 = 2
    }

```
The simples way to do this would be:

1) create an enum representing all possible preference key values, but add a 'invalid' with value -1.

```
enum PreferenceKey
{
    INVALID = -1,
    SIX_E_SF_TYPE = 0,
    SIX_E_SF_VALUE = 1,
    ...
}

```

2) create a list of strings containing the string representations of all preference key values **in the same order as the enum**.

```
private List<string> strings = new List<string> {"6E-SF-TYPE", "6E-SF-VALUE", ...};

```

3) Lookup the column value in the list of strings and cast the position to the enum.
If the column value cannot be found in the list of strings, IndexOf will return -1, which is the 'invalid' value.

```
PreferenceKey key = (PreferenceKey) strings.IndexOf(preferenceKeyColumnValue);

```