title: Validate a condition that relies on foreign keys in Django admin
body: <p>I have two models, <code>Book</code> and <code>Page</code>, where <code>Page</code> has a foreign key relation to <code>Book</code>. In admin.py, the webpage to modify a book has inline elements for <code>Page</code> objects.</p>
  
  <p>models.py:</p>
  
  <pre><code>class Book(models.Model):
      # ...
  
  class Page(models.Model):
      book = models.ForeignKey(Book, editable=False)
      number = models.IntegerField('Page number')
      # ...
  </code></pre>
  
  <p>admin.py:</p>
  
  <pre><code>class PageAdminInline(admin.TabularInline):
      model = Page
      extra = 1
  
  class BookAdmin(admin.ModelAdmin):
      inlines = [PageAdminInline]
  
  admin.site.register(Book, BookAdmin)
  </code></pre>
  
  <p>In the inline forms, a field allows to set the page number for each page. With this configuration, how can I validate the global condition that all pages numbers of a book should be different and numbered from one to the number of <code>Page</code> objects associated to the book?</p>
  
  <p>I suppose I have to override a <code>clean()</code> method related to a book model or form somewhere, but I don't know how to access from there the data related to the pages that the user inputs.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Based on Emett's suggestion, I have tried to override the <code>clean()</code> method of the <code>Page</code> model:</p>
  
  <pre><code>class Page(models.Model):
      book = models.ForeignKey(Book, editable=False)
      number = models.IntegerField('Page number')
  
      def clean():
          book_pages = Page.objects.filter(book=self.book)
          # ... [apply condition on the book_pages QuerySet]
          super(Page, self).clean()
  </code></pre>
  
  <p>This does not work: if I modify the page number of all pages in the admin site for a book, <code>book_pages</code> will contain objects that have the old page numbers.</p>
  
  <p>In addition, having the condition checked in <code>Page</code> also means that it  will be tested n times if I have n pages, while just checking it once should be sufficient.</p>