简体   繁体   English

如何从一个Django模型对象获取信息?

[英]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". 今天我正在尝试获取并打印所有用户选择“Value1”的用户电子邮件。

This is how my model.py looks like: 这就是我的model.py的样子:

from django.db import models

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

Forms.py : 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 " 最后我的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. 我在views.py中评论了我的问题。 I hope you will be able to help me. 我希望你能帮助我。 Thank you in advance! 先感谢您!


I re-write my code with getlist. 我用getlist重写了我的代码。 Now it looks like this: 现在它看起来像这样:

views.py : 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: 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. 顺便说一句,用户可以选择2个值,这就是我使用复选框的原因。 But still it not working. 但它仍然无法正常工作。

I believe you want to use the values_list property like so: 我相信你想使用values_list属性,如下所示:

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. 如果您还没有阻止重复,您可能还想对其应用distinct() 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. 另外,请看一下ModelForms :它看起来会为你节省相当多的时间/代码来处理这个问题。 You could create a ModelForm based on your Vartotojas object and not have to write the explicit save() method you have. 您可以基于Vartotojas对象创建ModelForm,而不必编写您拥有的显式save()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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