Python 查询设置以获取用户的所有属性';谁的联系人?
我有以下相关型号:Python 查询设置以获取用户的所有属性';谁的联系人?,python,django,django-models,Python,Django,Django Models,我有以下相关型号: class PositionTimestamp(models.Model): timestamp = models.DateTimeField(auto_now_add=True) position = models.ForeignKey('Position') profile = models.ForeignKey('UserProfile') class Position(models.Model): position = models.
class PositionTimestamp(models.Model):
timestamp = models.DateTimeField(auto_now_add=True)
position = models.ForeignKey('Position')
profile = models.ForeignKey('UserProfile')
class Position(models.Model):
position = models.CharField(max_length=100)
ordering = models.IntegerField(max_length=3)
select_display = models.CharField(max_length=100)
class UserProfile(models.Model):
def mutual_contacts(self):
# returns all of the current User's contacts. A QuerySet of UserProfile objects.
我想获得给定用户的相互联系人的所有位置
。以下是我目前(效率低下)正在做的事情:
p=UserProfile.objects.get(id=1)
>>>位置设置=[]
>>>对于p.mutual_contacts()中的配置文件:
position\u set.append(p.positiontimestamp\u set.all())
>>>位置集
[[, ]]
>>>位置集合=集合([item.position for position\u set[0]])
>>>位置集
集合([,])
查询集是否可以用于获取相同的位置?要获取给定用户集的所有位置,请尝试以下代码:
p = UserProfile.objects.get(id=1)
contacts = p.mutual_contacts()
positions = Position.objects.filter(positiontimestamp__profile__in=contacts)
p = UserProfile.objects.get(id=1)
contacts = p.mutual_contacts()
positions = Position.objects.filter(positiontimestamp__profile__in=contacts)