Django 如何检查a.myid中是否有数字\u id?
这是我的模型:Django 如何检查a.myid中是否有数字\u id?,django,Django,这是我的模型: class A(models.Model): number = models.FloatField(..) myid = models.IntegerField() class B(models.MOdel): name = models.CharField(..) number_id = models.IntegerField() 查询集: a = A.objects.all() 我的尝试: b = B.objects.filter(numb
class A(models.Model):
number = models.FloatField(..)
myid = models.IntegerField()
class B(models.MOdel):
name = models.CharField(..)
number_id = models.IntegerField()
查询集:
a = A.objects.all()
我的尝试:
b = B.objects.filter(number_id__in=a.myid)
如何检查
number\u id
是否在a.myid
中?您应该使用外键来显示这种类型的关系:
class A(models.Model):
number = models.FloatField(..)
myid = models.IntegerField()
class B(models.MOdel):
name = models.CharField(..)
number = models.ForeignKey('A')
…然后将在DB级别自动执行检查;除了“a”类之外,不可能添加其他关系
B.objects.filter(number_id__in=A.objects.all().value_list('id', flat=True))