python - 如何在 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)
有没有办法配置项目按创建日期和时间排序?
解决方案
试试这个
class LoanInfoStatusHistory(models.Model):
# other fields
date_created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['date_created']
推荐阅读
- swift - 使用 getter 和 setter 从 tableView 单元格获取更新的值
- vuejs2 - Vuex 计算属性未更新
- css - flex 容器中的图像宽度
- javascript - 通过选择 Select 中的值自动添加输入的问题
- r - 为什么在这个例子中 summarise_if 不能与 bind_rows 一起工作?
- laravel - 显示除您关注 Laravel 7 的用户之外的所有帖子
- css - CSS Flex 硬破坏项目
- assembly - 如何在汇编中获得数字的平方
- sql - 有没有一种方法可以轻松地将内部连接中的共享列压缩为单个列,同时仍然获取所有信息?
- c++ - Qlist Struct append 在 QT 4.8 发布模式下无法访问