Python Forum
Django not saving model in management command - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Django not saving model in management command (/thread-10707.html)



Django not saving model in management command - F2Andy - Jun-02-2018

I am setting up a command to runfrom the command line to put some initial values in the database, but for some reason the model is not getting saved. I have set up the command as per here (but with no args):
https://docs.djangoproject.com/en/dev/howto/custom-management-commands/

Here is my class, with a few print commands for debugging. The reset method goes on to add objects, but that fails because they belong to the universe, and that no id:

class Universe(models.Model):
    name = models.CharField(max_length=200)
    turn_count = models.IntegerField(default=0)
    
    def __str__(self):
        return self.name
        
    def reset():
        universe = Universe(name='Testiverse')
        print(universe)
        print(universe.id)
        print("universe turn count is %s." % universe.turn_count)
        universe.save
        print(universe.id)
        print("universe count is %s." % Universe.objects.all().count())
The Universe object is created as it should be, but it does not get saved, as show by the count not going up. I can create them via the admin interface, and the count is reported correctly.

I am only just learning Django/Python, but have a fair bit of experience with Ruby on Rails; nevertheless this has me stumped.

Python 3.6.5/Django 2.0.5 final/Windows 10


RE: Django not saving model in management command - F2Andy - Jun-02-2018

So it turns out you need brackets after the "save" in Python.