django mysql 連線查詢join
阿新 • • 發佈:2019-02-18
假設有AppInfo 模型:
class AppInfo(models.Model):
platform = models.CharField("平臺",max_length=20,choices=APP_PLATFORMS);
category = models.ForeignKey(AppCategory,on_delete=models.SET_NULL,blank=True,null=True);
name = models.CharField("英文名稱",max_length=200);
和AppAttribute模型:
class AppAttribute(models.Model): app = models.OneToOneField(AppInfo); app_manager=models.CharField("產品策劃",max_length=50); app_designer=models.CharField("產品設計",max_length=50); app_engineer=models.CharField("技術實現",max_length=50); def __unicode__(self): return "應用邊緣屬性" class Meta: verbose_name="應用邊緣屬性"
現在想要查詢appinfo platform='ios' name="test"的應用的 AppAttribute值,這時候就需要連線查詢了,查詢的方式如下:
app = AppAttribute.objects.filter(app__name__exact="test",app__platform__exact="ios")[0];
print(app.app_manager)
print(app.app_designer)
print(app.app_engineer)