简体   繁体   中英

Django - queryset in a charfield form

I need help please, I'm trying to showing a select in my template from a queryset() but it's not working properly.

Here's what I got:

from __future__ import unicode_literals

from django.db import models

# Create your models here.
from django import forms
from activos.models import activos
from activos.forms import activosForm
# Create your models here.
class grupos(models.Model):
    nombre_grupo = models.CharField(max_length=100)
    frecuencia = models.CharField(max_length=100)
    offset = models.CharField(max_length=100)
    activos_class = activos()
    activos_query = activos.objects.all()
    res =str(activos_query)
    PORT_CHOICES = (res,res),
    nombre_puerto = models.CharField(max_length=100,choices=PORT_CHOICES)

    def __unicode__(self):
        return self.nombre_grupo

    def __str__(self):
        return self.nombre_grupo

as you can see I'm calling usuario from activos() which is in another app from my project, I want to show that result in a select in my template but when doing so I got something like this:

-------------
<QuerySet[<activos:alvaro>, <activos:david>,<activos:daniel>]>

I want something like this:

-----
alvaro
david
daniel

I tried to avoid that editing this line res =str(activos_query[0]) but if I delete every user from my database I ll get index error

Is there an efficient way to do that?

Django version: 1.10.4

EDIT

grupos views.py

from django.shortcuts import render
from django.conf import settings
from django.core.mail import send_mail
from .forms import gruposModelForm
from .models import grupos
from activos.models import activos


def grupos_test(request):
    titulo = "hola"
    if request.user.is_authenticated():
        titulo = "bienvenido %s" %(request.user)
    form = gruposModelForm(request.POST or None)
    queryset = grupos.objects.all()
    # query_delete = queryset.delete()
    context = {
    "titulo": titulo,
    "form": form,
    "queryset": queryset,
    "res": res,
    }
    if form.is_valid():
        instance = form.save(commit=False)
        nombre_grupo = form.cleaned_data.get("nombre_grupo")
        frecuencia = form.cleaned_data.get("frecuencia")
        if not instance.nombre_grupo:
            instance.nombre_grupo = "PERSONA"
        instance.save()
        context = {
            "titulo": "Gracias %s" %(nombre_grupo)
        }
        if not nombre_grupo:
            context = {
                "titulo": "Gracias %s!" %("desconocido"),
                "form": form,
            }

        # form_data = form.cleaned_data
        # abc =  form_data.get("email")
        # abc2 = form_data.get("nombre")
        # obj = Registrado.objects.create(email=abc,nombre=abc2)
    # context = {
    #   "titulo": titulo,
    #   "form": form,
    # }
    return render(request, "forms5.html", context)

html

{{ titulo }}
<hr/>
<br/>
<form method="POST" action="">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Registrame" />
</form>

<p>EXAMPLE</p>
{% for g in queryset %}
  <select>{{ g.usuario }}</select>
{% endfor %}

You are just outputting a QuerySet (in other words a "list" of model objects). Run a for loop in your template to take out each one.

example

{% for g in grupos %}
  <select>{{ g }}</select>
{% endif %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM