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.

Feature Request: save() method for models

See original GitHub issue

Problem: If I create new model and want to save it - I need to use await ModelClass.objects.create(column_name=model.column_name, ...).

Solution: Just call something like model.save() the same way model.update() is used right now. Also model.save() can fallback to model.update() if id is set.

Maybe I am missing something and there is another easy and clean way to add a new row, I am ready to use it or implement one.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tomchristiecommented, Apr 5, 2019

Instead of…

model = ModelClass()
model.string_field = 'Test' # db.StringField()
model.int_field = 5 # db.IntField()
model.bool_field = False # db.BooleanField()
await model.save()

Use this…

instance = await ModelClass.objects.create(
    string_field=...,
    int_field=...,
    bool_field=...
)

If you really need to build it up gradually you can use this pattern…

kwargs = {}
kwargs['string_field'] = 'Test'
kwargs['int_field'] = 5
kwargs['bool_field'] = False
instance = await ModelClass.objects.create(**kwargs)

Having partially populated model instances, or modifiying model instance properties directly is a problematic pattern and means that model instances don’t do a good job of encapsulating valid state changes.

0reactions
HarrySkycommented, Apr 5, 2019

Thanks, did not know that this is problematic pattern 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Overriding the save method - Django Models - GeeksforGeeks
The save method is an inherited method from models.Model which is executed to save an instance into a particular Model.
Read more >
django - How to access to request.user in def save() in models?
This object is linked to the user by a foreign key. I have override the def_save() method to create the user and link...
Read more >
ability to save a tf.GraphModel to a local file system ... - GitHub
Currently you can load a tf.GraphModel with tf.loadGraphModel. Is there an easy way to save the files for the graph model locally?
Read more >
Save and Load Machine Learning Models in Python with scikit ...
Save Your Model with joblib​​ It provides utilities for saving and loading Python objects that make use of NumPy data structures, efficiently. ...
Read more >
How to save a R model? - ProjectPro
This recipe helps you save a R model. ... method = 'anova'). Using rpart.plot() function to plot the decision tree model. rpart.plot(model) ...
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