Ver Mensaje Individual
  #2 (permalink)  
Antiguo 27/03/2013, 08:34
tu_alvaro
 
Fecha de Ingreso: enero-2011
Mensajes: 11
Antigüedad: 13 años, 9 meses
Puntos: 0
Respuesta: Duda con consultas ManytoMany

Bueno tras haber indagado mas creo que voy por el buen camino, he realizado un formulario de busqueda
Código:
class buscarForm(forms.Form):
	generos	= forms.ModelMultipleChoiceField(queryset=generos.objects.all(),widget=forms.CheckboxSelectMultiple(),required=True)
	def clean(self):
		return self.cleaned_data
una vista
Código:
def buscar_view(request):
	info ="Selecciona los generos"
	pis = pista.objects.filter(status=True)
	if request.method == "POST":
		form = buscarForm(request.POST)
		if form.is_valid():
			generos = form.cleaned_data['generos']
			pis = pista.objects.filter(generos=generos)
			info = "Estos son los resultados"
		else:
			info = "Datos incorrectos"
	else:
		form = buscarForm()
	ctx = {'form':form, 'pista':pis, 'informacion':info}
	return render_to_response('musica/buscar.html',ctx,context_instance=RequestContext(request))
el template
Código:
{% extends 'base.html' %}
{% block title %}Agregar Pista{% endblock %}
{% block content %}
<h2>Buscar pista...</h2>
	<form action="." method="POST">
		{% csrf_token %}
		{{ form.as_p }}
		<input type="submit" value="Buscar">
		<input type="reset" value="Limpiar">
	</form>
<br>
 	{{ informacion }}
	{% if pista %}
		{% for p in pista %}
			<p><a href="/pista/{{p.id}}/">{{p.nombre}}</a></p>
		{% endfor %}
	{% else %}
		<h2>No hay pistas activas</h2>
	{% endif %}
{% endblock %}
y la url
Código:
	url(r'^musica/$','buscar_view',name= "vista_buscar"),
de esta forma permite seleccionar un genero y mediante pis = pista.objects.filter(generos=generos), obtiene de la tabla pista_genero la relacion many to many y los extiende en el template con un enlace.

Lo que aun no he conseguido es que se pueda marcar mas de una casillaaa, al hacerlo obtengo el siguiente error, por favor necesitaria un poco de ayuda en esto, he visto millones de tutoriales, foros, y de todo no vengo aqui a que me hagan el trabajoa pero por favor una manita, es mi octavo mensaje en el foro que cae en el olvido sin respuesta. Gracias


Código:
DatabaseError at /musica/

(1242, 'Subquery returns more than 1 row')

Request Method: 	POST
Request URL: 	http://127.0.0.1:8000/musica/
Django Version: 	1.5
Exception Type: 	DatabaseError
Exception Value: 	

(1242, 'Subquery returns more than 1 row')

Exception Location: 	C:\Python27\lib\site-packages\MySQLdb\connections.py in defaulterrorhandler, line 36
Python Executable: 	C:\Python27\python.exe
Python Version: 	2.7.3
Python Path: 	

['C:\\Users\\ujake\\impromusic\\impromusic',
 'C:\\Python27\\lib\\site-packages\\distribute-0.6.35-py2.7.egg',
 'C:\\Python27\\lib\\site-packages\\django_audiotracks-0.2.1-py2.7.egg',
 'C:\\Python27\\lib\\site-packages\\mutagen-1.20-py2.7.egg',
 'C:\\Python27\\lib\\site-packages\\django_registration_email-0.5-py2.7.egg',
 'C:\\Python27\\lib\\site-packages\\timeside-0.4.3-py2.7.egg',
 'C:\\Windows\\system32\\python27.zip',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\plat-win',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Python27\\lib\\site-packages',
 'C:\\Python27\\lib\\site-packages\\PIL',
 'C:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg-info']

Server time: 	Wed, 27 Mar 2013 15:28:15 +0100
Error during template rendering

In template C:\Users\ujake\impromusic\impromusic\impromusic\templates\musica\buscar.html, error at line 13
1242
3 	{% block content %}
4 	<h2>Buscar pista...</h2>
5 	<form action="." method="POST">
6 	{% csrf_token %}
7 	{{ form.as_p }}
8 	<input type="submit" value="Buscar">
9 	<input type="reset" value="Limpiar">
10 	</form>
11 	<br>
12 	{{ informacion }}
13 	{% if pista %}
14 	{% for p in pista %}
15 	<p><a href="/pista/{{p.id}}/">{{p.nombre}}</a></p>
16 	{% endfor %}
17 	{% else %}
18 	<h2>No hay pistas activas</h2>
19 	{% endif %}
20 	{% endblock %}
Traceback Switch to copy-and-paste view

    C:\Python27\lib\site-packages\django\core\handlers\base.py in get_response

                                response = callback(request, *callback_args, **callback_kwargs)

        ...
    ▶ Local vars
    C:\Users\ujake\impromusic\impromusic\impromusic\apps\musica\views.py in buscar_view

        	return render_to_response('musica/buscar.html',ctx,context_instance=RequestContext(request))

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\shortcuts\__init__.py in render_to_response

            return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\template\loader.py in render_to_string

                return t.render(context_instance)

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\template\base.py in render

                    return self._render(context)

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\template\base.py in _render

                return self.nodelist.render(context)

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\template\base.py in render

                        bit = self.render_node(node, context)

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\template\debug.py in render_node

                    return node.render(context)

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\template\loader_tags.py in render

                return compiled_parent._render(context)

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\template\base.py in _render

                return self.nodelist.render(context)

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\template\base.py in render

                        bit = self.render_node(node, context)

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\template\debug.py in render_node

                    return node.render(context)

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\template\loader_tags.py in render

                    result = block.nodelist.render(context)

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\template\base.py in render

                        bit = self.render_node(node, context)

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\template\debug.py in render_node

                    return node.render(context)

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\template\defaulttags.py in render

                    if match:

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\db\models\query.py in __nonzero__

                return type(self).__bool__(self)

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\db\models\query.py in __bool__

                    next(iter(self))

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\db\models\query.py in _result_iter

                        self._fill_cache()

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\db\models\query.py in _fill_cache

                            self._result_cache.append(next(self._iter))

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\db\models\query.py in _safe_iterator

                    for item in iterator:

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\db\models\query.py in iterator

                for row in compiler.results_iter():

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\db\models\sql\compiler.py in results_iter

                for rows in self.execute_sql(MULTI):

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\db\models\sql\compiler.py in execute_sql

                cursor.execute(sql, params)

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\db\backends\util.py in execute

                    return self.cursor.execute(sql, params)

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\db\backends\mysql\base.py in execute

                    six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2])

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\django\db\backends\mysql\base.py in execute

                    return self.cursor.execute(query, args)

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\MySQLdb\cursors.py in execute

                    self.errorhandler(self, exc, value)

        ...
    ▶ Local vars
    C:\Python27\lib\site-packages\MySQLdb\connections.py in defaulterrorhandler

            raise errorclass, errorvalue

        ...
    ▶ Local vars