简体   繁体   中英

How to get information from one Django models object?

today I'm trying to get and print all my users emails, who had chose selection "Value1".

This is how my model.py looks like:

from django.db import models

class Vartotojas(models.Model):
    email = models.EmailField()
    option = models.CharField(max_length=30)

Forms.py :

from django import forms
from emailai.models import Vartotojas

class VartotojasForm(forms.Form):
    email = forms.EmailField(max_length=100)
    my_field = forms.MultipleChoiceField(choices=(('Value1','Value1'),('Value2','Value2')), widget=forms.CheckboxSelectMultiple())

    def save(self):
        mymodel = Vartotojas(
        email=self.cleaned_data['email'],
        option=self.cleaned_data['my_field'],
        )
        mymodel.save()

And finally my views.py "

from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from emailai.models import Vartotojas
from renginiai.forms import VartotojasForm

def name(request):
    if request.method == 'POST':
        form = VartotojasForm(request.POST)
        if form.is_valid():
            a = Vartotojas.objects.filter(option="u'Value1'") # How to do it right?
            # Now How To Get those object emails?
            new_user = form.save()
            return render(request, "Vartotojas-result.html", {
        'form': form, #BLABLABLA,
    })
    else:
        form = VartotojasForm()
    return render(request, "Vartotojas-form.html", {
        'form': form,
    })

I commented my questions inside my views.py. I hope you will be able to help me. Thank you in advance!


I re-write my code with getlist. Now it looks like this:

views.py :

if form.is_valid():
            email = form.cleaned_data['email']
            option = request.POST.getlist('my_field')
            new_user = form.save(email, option)

forms.py:

email = forms.EmailField(max_length=100)
my_field = forms.MultipleChoiceField(choices=(('Value1','Value1'),('Value2','Value2')), widget=forms.CheckboxSelectMultiple())

    def save(self, email, option):
        mymodel = Vartotojas(
        email=email,
        option = option,
        )
        mymodel.save()

As you see I pasted just most important places. By the way, users can choose 2 values, that's why I use checkbox. But still it not working.

I believe you want to use the values_list property like so:

Vartotojas.objects.filter(option=u"Value1").values_list("email", flat=True)

to get a list of all email addresses. You may also want to apply a distinct() to that if you're not already preventing duplicates. On a side note, look into ModelForms : it looks like that would save you a fair bit of the time/ code you have written for dealing with this. You could create a ModelForm based on your Vartotojas object and not have to write the explicit save() method you have.

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