Python Forum
Django not saving model in management command
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Django not saving model in management command
#1
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/ho...-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
Reply
#2
So it turns out you need brackets after the "save" in Python.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Django: View is unable to find attributes of database model pythonpaul32 0 506 Dec-07-2023, 06:38 PM
Last Post: pythonpaul32
  Querying Django model db - from Jose Portilla’s Udemy course Drone4four 2 1,689 Aug-09-2022, 09:25 AM
Last Post: Addweb
Photo After using models.Model on my class Im getting 'ImproperlyConfigured' error Django khavro 1 2,167 Apr-05-2021, 03:11 PM
Last Post: SheeppOSU
  webbrowser not working in screen management in kivy thousif 1 1,875 Nov-25-2020, 04:02 PM
Last Post: Axel_Erfurt
  [Django]calculate value in model smabubakkar 0 1,651 Apr-15-2020, 03:40 PM
Last Post: smabubakkar
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 3,293 Jun-30-2019, 12:21 AM
Last Post: scidam
  Need your help to fix my database relationship in Django model PrateekG 0 2,649 Jul-02-2018, 11:08 AM
Last Post: PrateekG
  Django model.py model.foreignkey() HenryJ 1 3,219 Feb-09-2018, 04:21 AM
Last Post: DocSeussMan
  How do I make a Django model from a tab-delimited data file? newbietostuff 0 2,513 Jan-09-2018, 02:44 AM
Last Post: newbietostuff
  Project on Shift management system ab_1986 3 5,362 Nov-30-2017, 02:57 PM
Last Post: ab_1986

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020