title: Receiving "NOT NULL constraint failed: home_page._order" error in Django model
body: <p>I keep receiving the above error and I am still not sure what component of my code is violating this. Below is my modely.py:</p>
  
  <pre><code>def get_image_path(instance, filename):
      return os.path.join('photos', str(instance.id), filename)
  
  
  # Create your models here.
  class Textbook(models.Model):
      founder = models.CharField(max_length=256)
      title = models.CharField(max_length=256)
      cover = models.ImageField(upload_to=get_image_path, blank=True, null=True)
      def __str__(self):
          return self.title
  
  class Page(models.Model):
  
      textbook = models.ForeignKey(Textbook,related_name="pages",blank=True, null=True)
      page_title = models.CharField(max_length = 256,blank=True, null=True)
      page_num = IntegerRangeField(min_value=0,max_value=256, blank=True, null=True)
  
      def getTextID(self):
          return self.textbook.id
  
      def __str__(self):
          return self.page_title
  
  
      def iterSave(self):
          pages = self.textbook.pages
          MAX_PAGE = pages.aggregate(Max('page_num'))
          try:
              cpy = pages.get(page_num = self.page_num)
              for page in pages:
                  if page.page_num &gt;= self.page_num:
                      obj,created = Page.objects.update_or_create(page_title = page.page_title, page_num = page.page_num+1, textbook = page.textbook)
          except:
              if self.page_num &gt; MAX_PAGE:
                  obj,created = Page.objects.update_or_create(page_title = self.page_title, page_num = self.page_num+1, textbook = self.textbook)
  
      def save(self, *args,**kwargs):
          self.iterSave()
          super(Page,self).save(*args, **kwargs)
  
  class Section(models.Model):
      page = models.ForeignKey(Page,related_name="sections")
      section_title = models.CharField(max_length=256)
      text = RichTextField(config_name='awesome_ckeditor')
      def __str__(self):
          return self.section_title
  </code></pre>
  
  <p>I understand it has something to do with the query functions but I am not sure what I have done incorrectly to generate this error.</p>
  
  <p>Here is my admin.py if it helps:</p>
  
  <pre><code>admin.site.register(Section)
  admin.site.register(Textbook)
  admin.site.register(Page)
  </code></pre>
  
  <p>and here is a helper function I have:</p>
  
  <pre><code>from django.db import models
  
  class IntegerRangeField(models.IntegerField):
      def __init__(self, verbose_name=None, name=None, min_value=None, max_value=None, **kwargs):
          self.min_value, self.max_value = min_value, max_value
          models.IntegerField.__init__(self, verbose_name, name, **kwargs)
      def formfield(self, **kwargs):
          defaults = {'min_value': self.min_value, 'max_value':self.max_value}
          defaults.update(kwargs)
          return super(IntegerRangeField, self).formfield(**defaults)
  </code></pre>
  
  <p>and my views.py:</p>
  
  <pre><code>def index(request):
      books = Textbook.objects.all()
      ret = {}
      for o in books:
          ret[o.id] = o
      ret['books'] = books
      return render(request,'index.html', ret)
  
  
  def genpage(request, bid = -1, pid = 1):
      b = Textbook.objects.get(id = int(bid))
      page = b.pages.get(page_num = int(pid))
      sections = page.sections.all()
      if  b.pages.filter(page_num = page.page_num+1).exists():
          next_page = page.page_num+1
      else:
          next_page = -1
      if b.pages.filter(page_num = page.page_num-1).exists():
          prev_page = page.page_num-1
      else:
          prev_page = -1
  
      ret = {
          'prev_page':prev_page,
          'next_page':next_page,
          'book':b,
          'page_title': page.page_title,
          'sections': sections,
  
      }    
  </code></pre>
  
  <p>I am pretty stumped by this because its an error I am so unfamiliar with. The error comes up in relation to calling super in the save function of page but other than that I do not know what to do. </p>