question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Call a model's method when querying for a field

See original GitHub issue

Let’s say I have the following query:

query {
  spreadsheet(id: 50) {
    name, 
    user {
      id, username
    },
    rows {id} # this is the foreign key that I need to call a function on 
  }
}

and my spreadsheet Model looks like:

class Spreadsheet:
  objects = SpreadsheetManager()
  user = models.ForeignKey(User)
  name = models.CharField()
  rows = model.ForeignKey(RowGenerator)
  ...

currently my query class looks like:

class Query(graphene.ObjectType):

  spreadsheet = graphene.Field(SheetType,
        id=graphene.Int(),
        name=graphene.String()
    )

  resolve_sheet(self, info, **kwargs):
        sheet_id = kwargs.get('id')
        # name = kwargs.get('name')

        if sheet_id is not None:
            return Sheet.objects.select_related('user','rows').prefetch_related('projects').get(id=sheet_id)

        return None

rows is a foreign key to an object, that upon calling a function generate_rows(), generates row data. Currently, my query returns the id of the foreign key, but what I want to do is to call a function on that object, (rows.generate_rows(), for example). How would it be possible to tell graphene to call that function on the field, once we retrieve it from the database?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
Mhs-220commented, Feb 15, 2018

@BossGrand Hey! is there any way to filter rows? for example, if we have person instead of rows, and it has first name and last name… is there anyway to do this? :

class PersonNode ( DjangoObjectType ):
	class Meta:
		model = Person
		interfaces = ( graphene.relay.Node, )
		filter_fields = {
			'full_name': ['exact', 'icontains', 'istartswith'],
		}

	full_name = graphene.String()
	def resolve_full_name ( instance, info, **kwargs ):
		return " ".join([instance.first_name, instance.last_name])

2reactions
kneufeldcommented, Jan 19, 2019

@Mhs-220 did you ever figure out how to do this? I’m in the same boat.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add a django model method result as a field in an ORM query
I have some ideas to solve this problem. One is using a @property decorator. The scale_output_value in this case will be function only...
Read more >
Allowing queries to be ordered by model methods - ORM
As I've been working with Django queries, I can't help but recognize how nice it would be to order queries by a model...
Read more >
Model Querying - Basics - Sequelize
The Model.create() method is a shorthand for building an unsaved instance with Model.build() and saving the instance with instance.save() . It is also...
Read more >
Querying — peewee 3.15.4 documentation
MyModel.insert_many(data, fields=[MyModel.field1, MyModel.field2]).execute() ... This method is a shortcut that calls Model.select() with the given query, ...
Read more >
Django Tutorial Part 3: Using models - Learn web development
default: The default value for the field. This can be a value or a callable object, in which case the object will be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found