django - 通过过滤对象列表获取对象列表
django - get list of objects by filtering a list of objects
我正在创建用户 activity 流。
模型 activity:
class Activity(models.Model):
actor = models.ForeignKey(User)
action = models.CharField(max_length=100)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
pub_date = models.DateTimeField(auto_now_add=True, auto_now=False)
关系模型:
class Person(models.Model):
user = models.OneToOneField(User)
relationships = models.ManyToManyField('self', through='Relationship',
symmetrical=False,
related_name='related_to')
RELATIONSHIP_FOLLOWING = 1
RELATIONSHIP_BLOCKED = 2
RELATIONSHIP_STATUSES = (
(RELATIONSHIP_FOLLOWING, 'Following'),
(RELATIONSHIP_BLOCKED, 'Blocked'),
)
class Relationship(models.Model):
from_person = models.ForeignKey(Person, related_name='from_people')
to_person = models.ForeignKey(Person, related_name='to_people')
status = models.IntegerField(choices=RELATIONSHIP_STATUSES)
def get_relationships(self, status):
return self.relationships.filter(
to_people__status=status,
to_people__from_person=self)
def get_following(self):
return self.get_relationships(RELATIONSHIP_FOLLOWING)
在浏览量中:
def home(request):
if request.user.is_authenticated():
user = request.user
userP = Person.objects.get_or_create(user=user)
userP = userP[0]
following = userP.get_following()
activities = Activity.objects.filter(actor__in=following)
if request.POST:
form = StatusForm(request.POST, request.FILES)
if form.is_valid():
if form.cleaned_data:
status = form.save(commit=False)
status.user = user
status.save()
return HttpResponseRedirect('/')
场景
- 用户正在关注用户 2 和用户 3
- 用户 2 正在关注用户和用户 3
- 用户 3 正在关注用户和用户 2
因为我使用 actor=user
而不是 actor=userP
(user.person
类型的对象)过滤活动。我如何从列表 get_following()
方法中获取 user object
的列表,而不是 user.person object
.
的列表
但是我可以像这样遍历每个 following
:
following = userP.get_following()
users = []
for u in following:
users.append(u.user)
users.append(user)
activities = Activity.objects.filter(actor__in=users)
但我希望有另一种更好的方法来做到这一点?
请指导我。非常感谢您的帮助。谢谢。
使用查询集的values_list()方法:
activities = Activity.objects.filter(actor__in=
following.values_list('user', flat=True))
如果您想将另一个用户添加到演员列表,那么您必须将 valies_list
从查询集转换为常规 python 列表:
actors = list(following.values_list('user', flat=True)) + [user.id]
activities = Activity.objects.filter(actor__in=actors)
我正在创建用户 activity 流。
模型 activity:
class Activity(models.Model):
actor = models.ForeignKey(User)
action = models.CharField(max_length=100)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
pub_date = models.DateTimeField(auto_now_add=True, auto_now=False)
关系模型:
class Person(models.Model):
user = models.OneToOneField(User)
relationships = models.ManyToManyField('self', through='Relationship',
symmetrical=False,
related_name='related_to')
RELATIONSHIP_FOLLOWING = 1
RELATIONSHIP_BLOCKED = 2
RELATIONSHIP_STATUSES = (
(RELATIONSHIP_FOLLOWING, 'Following'),
(RELATIONSHIP_BLOCKED, 'Blocked'),
)
class Relationship(models.Model):
from_person = models.ForeignKey(Person, related_name='from_people')
to_person = models.ForeignKey(Person, related_name='to_people')
status = models.IntegerField(choices=RELATIONSHIP_STATUSES)
def get_relationships(self, status):
return self.relationships.filter(
to_people__status=status,
to_people__from_person=self)
def get_following(self):
return self.get_relationships(RELATIONSHIP_FOLLOWING)
在浏览量中:
def home(request):
if request.user.is_authenticated():
user = request.user
userP = Person.objects.get_or_create(user=user)
userP = userP[0]
following = userP.get_following()
activities = Activity.objects.filter(actor__in=following)
if request.POST:
form = StatusForm(request.POST, request.FILES)
if form.is_valid():
if form.cleaned_data:
status = form.save(commit=False)
status.user = user
status.save()
return HttpResponseRedirect('/')
场景
- 用户正在关注用户 2 和用户 3
- 用户 2 正在关注用户和用户 3
- 用户 3 正在关注用户和用户 2
因为我使用 actor=user
而不是 actor=userP
(user.person
类型的对象)过滤活动。我如何从列表 get_following()
方法中获取 user object
的列表,而不是 user.person object
.
但是我可以像这样遍历每个 following
:
following = userP.get_following()
users = []
for u in following:
users.append(u.user)
users.append(user)
activities = Activity.objects.filter(actor__in=users)
但我希望有另一种更好的方法来做到这一点?
请指导我。非常感谢您的帮助。谢谢。
使用查询集的values_list()方法:
activities = Activity.objects.filter(actor__in=
following.values_list('user', flat=True))
如果您想将另一个用户添加到演员列表,那么您必须将 valies_list
从查询集转换为常规 python 列表:
actors = list(following.values_list('user', flat=True)) + [user.id]
activities = Activity.objects.filter(actor__in=actors)