2017-03-22 45 views
3

假定類似於這樣一個Django模型:石墨烯Django和模型屬性

class Profile(models.Model): 
    user = models.OneToOneField(User, on_delete=models.PROTECT) 
    name = models.CharField(max_length=255) 
    other_alias = models.CharField(blank=True,null=True,max_length=255) 

    @property 
    def profile_name(self): 
     if self.other_alias: 
      return self.other_alias 
     else: 
      return self.name 

我使用石墨烯的Django這個項目,我成功地創建一個模式來描述配置文件類型,並可以訪問它正確地通過GraphQL查詢。但是,我看不出有什麼辦法可以讓這個物業也可以使用。

是否應該從我的Django模型中刪除所有屬性類型的邏輯,而只是將GraphQL與模型中的原始信息一起使用,然後在使用GraphQL數據的地方執行該邏輯例如在使用它的React應用程序中)?

回答

5

在爲配置文件架構對象,你可以添加一個字符串字段與源:

class Profile(DjangoObjectType): 
    profile_name = graphene.String(source='profile_name') 
    class Meta: 
     model = ProfileModel 
     interfaces = (relay.Node,)