我正在尝试遍历我的所有Task模型对象,并检查他们是否有员工附加到他们身上。如果他们确实附加了员工,那么我想循环通过项目和用户之间的多种关系,并获得每个关联用户的电子邮件地址。我无法访问电子邮件字段。它一直让我“不可迭代”。或者"全球名称' users__type'未定义,"取决于我改变了什么。如果我的最终结果是一串用户电子邮件,我会遗漏一些简单的东西。
模型
from django.contrib.auth.models import User
from django.db import models
class CustomUser(models.Model):
user = models.OneToOneField(User, blank=True, null=True)
type = models.CharField(max_length=200, blank=True, null=True)
email = models.CharField(max_length=100, blank=True, null=True)
//加上其他自定义用户字段//
class Task(models.Model):
users = models.ManyToManyField(CustomUser, blank=True, null=true)
//加上其他任务栏//
我的其他课程
from django.models import Task
from django.models import CustomUser
from django.contrib.auth.models import User
def emailTaskList():
chosenPeople = []
notchosenPeople = []
chosenTask = Task.objects.filter(users__type = 'employee')
for e in chosen:
for d in e.users.all():
chosenPeople.append(d.email)
notchosenTask = Task.objects.filter(users__type != 'employee')
for a in notchosen:
for b in a.users.all():
notchosenPeople.append(b.email)
test = ' '.join(chosenPeople)
connection = mail.get_connection()
email = mail.EmailMessage('You have been assigned', test, [WebsiteEmail], [testRecipient])
email.send()
connection.close()
return
由于
答案 0 :(得分:0)
使用exclude()
方法代替!=
比较:
notchosen = Task.objects.exclude(users__type='employee')