So I've been trying to add a model function in Django that takes arguments and then inserts X number of new records into the database.
Why isn't it possible to do MyModel.MyFunction()
? Do I need to make an instance of it first?
If so, is there a better way of achieving the result I desire?
You can certainly do this, you just need to make it a classmethod
:
class MyModel(models.Model):
@classmethod
def my_function(cls):
new_instance = cls(...)
new_instance.save()
# etc...