0

This the is model's code:

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)

Is there a way to configure that the items are ordered by the date and time they were created?

6
  • Please Elaborate your question.do you want to make a query to get results from database with order by date_created ?
    – manask322
    Aug 31, 2020 at 14:32
  • Do you mean the day, the item was created, or updated? You've already defined an "update_date" field.
    – mcrot
    Aug 31, 2020 at 14:33
  • You don't have a date_created field on your model. Maybe you should rename update_date by date_created ?
    – Benbb96
    Aug 31, 2020 at 14:36
  • class GetLoanRepaymentHistoryView(ListAPIView): """Get all loan repayment history.""" serializer_class = LoanRepaymentSerializer authentication_classes = (TokenAuthentication,) permission_classes = (IsAuthenticated, AdminDashboardPermission,) def get_queryset(self): try: loan = Loan.objects.get(id=self.kwargs.get('id')) except Loan.DoesNotExist: return Response({'non_field_errors': ['Loan does not exist']}, status=status.HTTP_404_NOT_FOUND) return LoanRepayment.objects.filter(loan=loan)
    – dave
    Aug 31, 2020 at 14:50
  • how can i add a query to get the date created loan history in asc order
    – dave
    Aug 31, 2020 at 14:51

1 Answer 1

3

Try this

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

    class Meta:
        ordering = ['date_created']
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.