Django过滤多对多的意外结果

时间:2016-03-11 07:42:18

标签: django

class Meet(models.Model):
    type = models.ForeignKey(MeetType)
    owner = models.ForeignKey(User)
    name = models.CharField(max_length=200)
    description = models.TextField(blank=True, null=True)
    limit_per_day = models.IntegerField(blank=False, default=30)
    start_at = models.DateTimeField(blank=False, auto_now=False, auto_now_add=True)
    finish_at = models.DateTimeField(blank=False, auto_now=False, auto_now_add=False)


class MeetList(models.Model):
    meet = models.ForeignKey(Meet)
    user = models.ForeignKey(User)


owner = User.objects.create_user(username='owner', email='owner@gmail.com', password='pass2')

meet = Meet.objects.create(name='test_meet', owner=owner, type=meettype, start_at=datetime.datetime(2016, 3, 11),finish_at=datetime.datetime(2016, 3, 14))
meet2 = Meet.objects.create(name='test_meet2', owner=owner, type=meettype, start_at=datetime.datetime(2016, 3, 11),finish_at=datetime.datetime(2016, 3, 14))

MeetList.objects.create(meet=meet, user=owner)
MeetList.objects.create(meet=meet2, user=owner)

MeetList.objects.all()并获取

MeetList.objects.all()[0].meet.name
u'test_meet'

MeetList.objects.all()[1].meet.name
u'test_meet2'

然后我想按用户过滤,但我接下来

MeetList.objects.filter(user=owner)[0].meet.name
u'test_meet2'

MeetList.objects.filter(user=owner)[1].meet.name
u'test_meet2'

为什么MeetList.objects.filter(user=owner)[0].meet.nameMeetList.objects.filter(user=owner)[1].meet.name相同?

0 个答案:

没有答案