Django model default sort order using related table field

后端 未结 6 1282
半阙折子戏
半阙折子戏 2020-12-17 08:26

Is it possible to set the default sort order for a model to a field from a related model (rather than the integer key) i.e. something that yields a SQL order by clause with

6条回答
  •  我在风中等你
    2020-12-17 09:07

    class Question(models.Model):
    
      question_text=models.CharField(max_length=200)
            class Meta:
        verbose_name_plural="  Question"
    
    class Choice(models.Model):
    
      question=models.ForeignKey(Question,on_delete=models.CASCADE)
        class Meta:
        verbose_name_plural=" Choice"
    

    Either you can alter the number of spaces before the class name as above or order it using numbers as below:

    class Question(models.Model):
    
      question_text=models.CharField(max_length=200)
            class Meta:
        verbose_name_plural="1.Question"
    
    class Choice(models.Model):
    
      question=models.ForeignKey(Question,on_delete=models.CASCADE)
        class Meta:
        verbose_name_plural="2.Choice"
    

提交回复
热议问题