Add validation of related fields and lookups in model Meta.ordering
    
    
    
      
      
      
        
When the ordering class member in Meta of a model contains a field from a related model, and that field does not exist, django's makemigrations does not throw an error. However, if it is a direct field member of the same class, makemigrations does throw an error.
Example below tested on Django 2.0.5
from django.db import models
# Create your models here.
class Agreement(models.Model):
    agreement_id = models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)
    #class Meta:
      # generates error in makemigrations
      # app.Agreement: (models.E015) 'ordering' refers to the nonexistent field 'id'.
      # ordering = ['id']
class Order(models.Model):
    agreement = models.ForeignKey(Agreement, models.DO_NOTHING)
    class Meta:
      # does not generate error in makemigrations
      # but does so during runtime
      # e.g. [x for x in Order.objects.all()]
      ordering = ['agreement__id']
       
     
   
 
      
        
        
          Change History
          (17)
        
          
  
  
  
    
      | Component: | 
      
        Uncategorized → Database layer (models, ORM)
       | 
    
    
      | Summary: | 
      
        ordering by field from related model does not validate if field exists → Add validation of ordering by a field from related model.
       | 
    
    
      | Triage Stage: | 
      
        Unreviewed → Accepted
       | 
    
    
      | Type: | 
      
        Uncategorized → Cleanup/optimization
       | 
    
    
      | Version: | 
      
        2.0 → master
       | 
    
  
 
           
          
          
          
  
  
  
    
      | Has patch: | 
      
        set
       | 
    
    
      | Owner: | 
      
        changed from nobody to Hasan Ramezani
       | 
    
    
      | Status: | 
      
        new → assigned
       | 
    
  
 
           
          
  
  
  
    
      | Patch needs improvement: | 
      
        set
       | 
    
    
      | Summary: | 
      
        Add validation of ordering by a field from related model. → Add validation of related fields in model Meta.ordering
       | 
    
  
 
           
          
          
  
  
  
    
      | Patch needs improvement: | 
      
        unset
       | 
    
  
 
           
          
          
  
  
  
    
      | Patch needs improvement: | 
      
        set
       | 
    
  
 
           
          
          
  
  
  
    
      | Patch needs improvement: | 
      
        unset
       | 
    
  
 
           
          
  
  
  
    
      | Patch needs improvement: | 
      
        set
       | 
    
  
 
           
          
  
  
  
    
      | Patch needs improvement: | 
      
        unset
       | 
    
  
 
           
          
  
  
  
    
      | Triage Stage: | 
      
        Accepted → Ready for checkin
       | 
    
  
 
           
          
  
  
  
    
      | Summary: | 
      
        Add validation of related fields in model Meta.ordering → Add validation of related fields and lookups in model Meta.ordering
       | 
    
  
 
           
          
  
  
  
    
      | Resolution: | 
      
        → fixed
       | 
    
    
      | Status: | 
      
        assigned → closed
       | 
    
  
 
           
          
          
         
       
     
        
    
    
I'm going to accept this provisionally. There's a `FIXME` in `models/base.py` specifically about this:
# Skip ordering in the format field1__field2 (FIXME: checking # this format would be nice, but it's a little fiddly). fields = (f for f in fields if LOOKUP_SEP not in f)Added in d818e0c9b2b88276cc499974f9eee893170bf0a8.
Either we should address this, or remove the comment and close as
wontfixif "fiddly" turns out to be more effort than it's worth.A test case and a patch showing what "fiddly" actually entails would be great.