我想改变这个:
cpses = CanonPerson.objects.filter(persons__vpd = 1)
到此:
cpses = CanonPerson.objects.filter(persons__vpd in [1, 2, 3, 4, 5])
但这引发了一个例外:
NameError: global name 'persons__vpd' is not defined
型号:
class CanonPerson(models.Model):
p_id = models.PositiveIntegerField(primary_key=True)
persons = models.ManyToManyField(Person, related_name='persons')
class Person(models.Model):
vp_id = models.AutoField(primary_key=True)
p_id = models.PositiveIntegerField(db_index=True)
vpd = models.ForeignKey(VPD, db_column='vpd')
class VPD(models.Model):
id = models.SmallIntegerField(primary_key=True)
name = models.CharField(max_length=100)
答案 0 :(得分:0)
使用https://docs.djangoproject.com/en/dev/topics/db/queries/#the-pk-lookup-shortcut,CanonPerson.objects.filter(persons__vpd__in=[1,2,3,4,5])
应该有效。