How to exclude 'queryset' characters when using django sqlite orm

Viewed 31

I want to send mail to many users using django sendmail.

I am using orm in sqlite3 to get the list of recipients stored in db.

However, the orm result includes 'queryset', so mail cannot be sent Is there a way to exclude 'queryset' from orm results?

as-is

enter image description here

I want

enter image description here

views.py

def send_form(request):
    if request.method == 'POST':
        selected_target = request.POST['target']
        target = contact.objects.filter(target=selected_target).values_list('email')
        return render(request, 'view/send_form.html', {'target': target})

def send_success(request):
    if request.method == 'POST':
        subject = request.POST['subject']
        recipient = request.POST['email']
        message = request.POST['message']
        send_mail(
            subject,
            message,
            recipient,
            [recipient],
            fail_silently=False,
        )
    return render(request, 'view/send_success.html')

send result enter image description here

1 Answers

Queryset returns to you all (many) of applicable results, if you want only one email, instead of filter use get,

target_email = contact.objects.get(target=selected_target).email

but with queryset (many emails) you can send to each email in a list of queryset

target_emails = contact.objects.filter(target=selected_target)
for each_email in target_emails:
    target_email = each_email.email
    #this will output your needed email
    print(target_email)
    #and later do logic with target_email to send mail on it
Related