[英]Graphene-Django and Model Properties
Presume a Django model similar to this: 假设一个类似于这个的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
I'm using Graphene-Django with this project, and I successfully created a schema to describe the Profile type, and can access it properly through a GraphQL query. 我正在使用Graphene-Django这个项目,我成功地创建了一个描述Profile类型的模式,并且可以通过GraphQL查询正确访问它。 However, I can't see any way that I can make that property available too. 但是,我无法看到任何可以使该属性可用的方式。
Is the presumption that I should remove all property-style logic from my Django models, and instead only use GraphQL with the raw information in the model, and then do that logic instead in the place that I use the GraphQL data (eg. in the React app that uses it)? 假设我应该从我的Django模型中删除所有属性风格的逻辑,而只是将GraphQL与模型中的原始信息一起使用,然后在我使用GraphQL数据的地方(例如,在反应使用它的应用程序)?
In the schema object for Profile, you can add a String field with a source: 在Profile的架构对象中,您可以添加带有源的String字段:
class Profile(DjangoObjectType):
profile_name = graphene.String(source='profile_name')
class Meta:
model = ProfileModel
interfaces = (relay.Node, )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.