필자는 모든 필드를 외래 키로 사용하는 모델을 가지고 있습니다. 내 모델> 리소스에서 관계형 쿼리를 작성하여 Riggs 또는 Pluto 같은 다른 모델의 특성에 액세스하여 내 템플릿으로 보낼 수있는 json 개체를 만들 수 있습니다.django의 관계형 모델에서 json 객체를 만드는 방법
class Resources (models.Model):
tag = models.CharField(max_length=60)
sans_id = models.ForeignKey(Sans, null=True,blank=True)
riggs_id = models.ForeignKey(Riggs, null=True,blank=True)
crocs_id = models.ForeignKey(Crocs, null=True,blank=True)
pluto_id = models.ForeignKey(Pluto, null=True, blank=True)
user_id = models.ForeignKey(Users, null=True, blank=True)
def __unicode__(self):
return u'%s' % (self.tag)
다음과 같은 방식으로 관계형 객체의 속성에 액세스하려고합니다. 하지만 난 어떤 속성을 내가 다른 사건에서 필요할 수 있습니다 그래서 내가 원하는 모든 필드에 액세스 할 수 있도록 json으로 완전한 관계형 객체를 원하는지 모르겠다.
for x in Resources.objects.all()
x.crocs_id.specifications
x.pluto_id.flag
x.pluto_id.settings
감사합니다. 하지만 select_related() serialize하고 json 다시 보낼 때 완전한 개체 개체를 제공하지 않습니다. FK 값만 있으면 관련 모델을 반복 할 수 없습니다. – Eastwood