【发布时间】:2012-04-30 05:50:18
【问题描述】:
我已将我的模型简化为 a,以便更清楚地了解我想要做什么。
(应用团队中的models.py)
from django.db import models
from django.contrib.auth.models import User
import datetime
class Team(models.Model):
users = models.ManyToManyField(User)
team_title = models.CharField(max_length=200)
team_description = models.CharField(max_length=200)
def __unicode__(self):
return self.team_title
(应用文档中的models.py)
from django.db import models
import datetime
class Document(models.Model):
teams = models.ManyToManyField("Teams.Team", blank=True)
document_title = models.CharField(max_length=200)
document_description = models.TextField()
def __unicode__(self):
return self.document_title
我想要实现的是通过首先获取与文档关联的所有团队,然后获取与这些团队关联的所有用户,来获取与文档关联的用户列表。
到目前为止,我的尝试都是这样的
(在应用文档中查看.py)
from django.contrib.auth.models import User
from Documents.models import *
from Teams.models import *
def docUsers(request, doc_id):
current_document = Documents.objects.get(pk = doc_id)
associated_users = current_document.teams.all().users
....
错误:“QuerySet”对象没有“用户”属性
associated_users = current_document.items.all().users.all()
错误:“QuerySet”对象没有“用户”属性
associated_users = current_document.items.users.all()
错误:“ManyRelatedManager”对象没有“用户”属性
我是不是走错了路?
【问题讨论】:
标签: django django-models many-to-many django-views