Django default callable referencing to another field of the model
<p>I have a model that represents pages of a book.</p>

<pre><code>class BookPage(models.Model):

def getMaximumPageNumber():
    page_numbers = (i.page_number for i in BookPage.objects.all())
    return max(page_numbers)+1

page_number = models.IntegerField(default=getMaximumPageNumber, validators=[MinValueValidator(1)])
book = models.ForeignKey('Book', on_delete=models.CASCADE)


  //other stuff
</code></pre>

<p>I want Django to assign a page number automatically upon creation, so I made <code>getMaximumPageNumber()</code>. It would work fine if I had only one book. But now I want to reference the <code>book</code> from inside this function, so it would be something like <code>page_numbers = (i.page_number for i in BookPage.objects.filter(book=self.book)</code>. But the problem is that I can't seem to use <code>self</code> in this function, because if I specify it as <code>def getMaximumPageNumber(self):</code>, then it would show the  <code>TypeError: getMaximumPageNumber() missing 1 required positional argument: 'self'</code> when I try to access add page screen in admin. Therefore I cannot specify the book I want to find the maximum page of, and this is what I really need to accomplish here.</p>

<p>Is there a way to reference another field of a newly created instance of a model from a default-assigning function? My initial solution was to override the <code>save()</code> of this model, but it breaks down some other things I've already written, so using a dynamic "default" would be better, if it is possible in my case.</p>