Graphene-Django Properties and Models

Let's assume your Django model looks like the following:

  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 am using Graphene-Django with this project and I have successfully created a schema to describe the profile type and can access it properly via a GraphQL query. However, I don't see any way that I can make this property available.

Is it an assumption that I should remove all property style logic from my Django models and instead use GraphQL with only the raw information in the model, and then do that logic instead of using GraphQL data (for example, in a React app that uses it)?

+3


source to share


1 answer


In the schema object for the profile, you can add a String field with a source:



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

      

+6


source







All Articles