Django forms: passing variables to a modelchoicefield

 

So I came across the problem of needing to filter my Django ModelChoiceField queryset by a variable in my view. Specifically I needed to pass the current selected state to the ModelChoiceField queryset. The best way I found to do this is to initialize the form with my state var in the initial:

  1. state = ‘OH’
  2. form = LocationForm(initial={‘state’:state})

After that, instead of creating the field the traditional way, you can append the field to the self.fields dictionary within the init, enabling you to pass the initial state variable into the queryset of the field. Here is my form:

  1. class LocationForm(forms.Form):
  2.     def __init__(self, *args, **kwargs):
  3.         super(LocationForm, self).__init__(*args, **kwargs)
  4.         self.fields.insert(len(self.fields)-1‘location’,
  5.                            forms.ModelChoiceField(queryset=Location.objects.filter(state=self.initial[‘state’])))
  6.     first_name = forms.CharField(50, label=«First Name»)
  7.     last_name = forms.CharField(50, label=«Last Name»)
  8.     address = forms.CharField(60, label=«Street Address»)
  9.     city = forms.CharField(40, label=«City»)
  10.     state = USStateField()
  11.     zip = USZipCodeField()

Self.fields is a Django SortedDict, and it’s insert method takes the index to insert to, the key, and the value.

Source

La clave del éxito: por Laura Federico @adinteractiva


De niños nos preguntaban ¿Qué es lo que querés ser cuando seas grande? Casi siempre las respuestas eran: astronauta, bombero, policía, médico, bailarina. Y ahora, que llegamos a grandes, ¿en qué nos convertimos?

Cuando miramos hacia atrás en la vida, podemos ver una gran cantidad de logros obtenidos, pero cuando observamos hacia el futuro, tratando de definir lo que queremos alcanzar, la visualización se torna nebulosa.
– See more at: http://administracioninteractiva.com/la-clave-del-exito-2/#sthash.omsfGJbE.dpuf

Convert Object To Array With PHP

By Kevin Waterson

Converting an object to an array using PHP comes with a small gotcha. One would be forgiven for thinking it is merely a task of casting the object and the job can be completed with a single line of code. This works well for simple objects, however, the task increases in complexity in line with the objects complexity.

Consider the following object that is cast to an array.


<?php
/*** create an object ***/
$obj = new stdClass;
$obj->foo = 'foo';
$obj->bar = 'bar';
$obj->baz = 'baz';

/*** cast the object ***/
$array = (array) $obj;

/*** show the results ***/
print_r( $array );
?>

Gracias a PHPro

Redis NoSQL Databases(2.6.14): Como instalarlo en OpenSuse 12.3 (linux)

Bueno ahora veremos como instalar redis en OpenSuse 12.3.

  • 1. El primero necesitamos descargar el .tar.gz ya se en su vserion Stable/Dev Release.
  • 2. Extraemos en contenido dentro de /tmp.
    redis-e
  • 3. Compilamos;
    $ cd redis-2.6.14
    $ make
    $ make test
    $ make install

    Compruebe si la compilación se realizó correctamente.

  • 4. Copie el archivo redis.conf a /etc/redis/
    # sudo cp redis.conf /etc/redis/redis.conf

    Si el directorio no está disponible en / etc /, cree una carpeta llamada redis y copiar el archivo de configuración.

  • 5. Para iniciar el servidor de redis en el puerto 6379,
    $ redis-server /etc/redis/redis.conf
  • 6. Agregamos el siguiente fichero en /etc/init.d/redis-servidor con el cual se iniciara redis server
    #! /bin/sh
    ### BEGIN INIT INFO
    # Provides:		redis-server
    # Required-Start:	$syslog
    # Required-Stop:	$syslog
    # Should-Start:		$local_fs
    # Should-Stop:		$local_fs
    # Default-Start:	2 3 4 5
    # Default-Stop:		0 1 6
    # Short-Description:	redis-server - Persistent key-value db
    # Description:		redis-server - Persistent key-value db
    ### END INIT INFO
    
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    DAEMON=/usr/bin/redis-server
    DAEMON_ARGS=/etc/redis/redis.conf
    NAME=redis-server
    DESC=redis-server
    PIDFILE=/var/run/redis.pid
    
    test -x $DAEMON || exit 0
    test -x $DAEMONBOOTSTRAP || exit 0
    
    set -e
    
    case "$1" in
      start)
    	echo -n "Starting $DESC: "
    	touch $PIDFILE
    	chown redis:redis $PIDFILE
    	if start-stop-daemon --start --quiet --umask 007 --pidfile $PIDFILE --chuid redis:redis --exec $DAEMON -- $DAEMON_ARGS
    	then
    		echo "$NAME."
    	else
    		echo "failed"
    	fi
    	;;
      stop)
    	echo -n "Stopping $DESC: "
    	if start-stop-daemon --stop --retry 10 --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON
    	then
    		echo "$NAME."
    	else
    		echo "failed"
    	fi
    	rm -f $PIDFILE
    	;;
    
      restart|force-reload)
    	${0} stop
    	${0} start
    	;;
      *)
    	echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}" >&2
    	exit 1
    	;;
    esac
    
    exit 0
  • 7. Puede iniciar y detener servidores redistribución utilizando,
    sudo /etc/init.d/redis-server start
    sudo /etc/init.d/redis-server stop
  • 8. Finalmente iniciamos la consola de redis
    $ sudo /etc/init.d/redis-server start
    $ redis-cli
  • 9. El famoso Hello World(Inserting (hello, world) Key, Value Pair:)
    >set hello world
    OK

    Getting “world” Value from “hello” Key:

    >get hello
    “world”

fuentes:
http://tutorialforlinux.com/2013/07/getting-started-with-redis-nosql-database-on-linux-opensuse-step-by-step-easy-guide/

http://www.arunchinnachamy.com/howto-install-latest-redis-version-2-4-17/

http://www.art-coder.com/2011/12/01/how-to-run-redis-server-as-daemon/

MySQL Workbnech: OpenSUSE Couldn’t load library libmysqlclient_r.so

Despues de una actualizacion de OpenSUSE 12.3 a 13.1 me enconter con algun problemillas, como este:

 

Couldn't load library libmysqlclient_r.so: libmysqlclient_r.so: cannot open shared object file: No such file or directory
Screenshot from 2014-01-06 21:16:43Solucion:

primero vemos que version de libmysqlclient en /usr/lib64/ bueno en mi caso, dejo la imagen con la sulucion. dudas deja un comentario

Screenshot from 2014-01-06 22:15:13