Call a model's method when querying for a field
See original GitHub issueLet’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:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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? :
@Mhs-220 did you ever figure out how to do this? I’m in the same boat.