首页 > 解决方案 > 如何在 Django 贷款历史模型中按 date_created 添加订单

问题描述

这是模型的代码:

class LoanInfoStatusHistory(models.Model):
    """Model that saves a history of a loan."""
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)  # pylint: disable=invalid-name
    loan = models.ForeignKey(Loan, on_delete=models.PROTECT)
    previous_status = models.CharField(choices=Loan.LOAN_STATUS, max_length=20)
    new_status = models.CharField(choices=Loan.LOAN_STATUS, max_length=20)
    update_date = models.DateTimeField(auto_now_add=True)
    
    def __str__(self):
        """
        Unicode representation for a LoanInfoStatusHistory model.
        :return: string
        """
        return '{}: {} - {}'.format(self.loan.id, self.previous_status, self.new_status)

有没有办法配置项目按创建日期和时间排序?

标签: pythondjango

解决方案


试试这个

class LoanInfoStatusHistory(models.Model):
    # other fields
    date_created = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['date_created']

推荐阅读