我如何按升序重新排列卡片 django

How do I rearrange the cards in ascending order django

我有一个页面,其中卡片中的客户名称未按字母顺序按升序重新排列,例如:B、M、S。如何使其按字母顺序排列,以便首先出现客户名称为 Bangkok airways 的卡片,然后是带有 malaysia airlines 的卡片,依此类推?

views.py

def outgoinggallery(request):
    user = request.user

    category = request.GET.get('category')

    if category == None:
        alloutgoinglru = OutgoingLRU.objects.filter(category__user=user)
    else:
        alloutgoinglru = OutgoingLRU.objects.filter(category__name=category, category__user=user)
        # if query:
        # return OutgoingLRU.objects.filter(title__icontains=query)



    categories = Category.objects.filter(user=user)
    context = {'categories': categories, 'alloutgoinglru': alloutgoinglru}

    return render(request, 'Outgoing/outgoinggallery.html', context)

outgoinggallery.html

{% extends "logisticbase.html" %}
{% block content %}
    <!-- CSS only -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">

    <style>



td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
  border-radius: 15px;
}
        .image-thumbail {
            height: 200px;
            object-fit: cover;
        }


        .list-group-item a {
            text-decoration: none;
            color: black;
        }
    </style>




    <br>


   <div style="padding-left:16px">

        <div class="row">


            <div class="col-md-9">
                <div class="row">
                    <h5>View Outgoing LRU</h5>
                    <div class="col-md-7">
                    
                    </div>
                    <br>
                    <div class="col-md-9">
                <div class="row">

                    {% for OutgoingLRU in alloutgoinglru %}
                    <div class="col-md-4">
                        <div class="card my-4">
                            <img class="image-thumbail" src="{{OutgoingLRU.image.url}}" >

                            <div class="card-body">
                                <small>Customer Name: {{OutgoingLRU.category.name}}</small>
                                <br>
                                <small>Delivery Order: {{OutgoingLRU.Deliveryor}}</small>
                            </div>
                            <a href="{% url 'viewlruphoto' OutgoingLRU.id %}"  style="width:265px" class="btn btn-outline-dark btn-sm m-1">View</a>
                            <form action="{% url 'deleteoutgoing' OutgoingLRU.id %}" method="post">
                                {% csrf_token %}
                                <button type="submit" style="width:270px" class="btn btn-sm btn-danger">Delete</button>
                            </form>

                        </div>
                    </div>
                    {% empty %}
                    <h3>No photos...</h3>
                    {% endfor %}








                </div>
            </div>
        </div>
   </div>


{% endblock %}

models.py

class OutgoingLRU(models.Model):
    class Meta:
        verbose_name = 'OutgoingLRU'
        verbose_name_plural = 'OutgoingLRUs'

    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, blank=True)
    image = models.ImageField(null=False, blank=False)
    equipmentimages = models.ImageField(null=False, blank=False)
    boximages = models.ImageField(null=False, blank=False)
    documentimage = models.ImageField(null=False, blank=False)

    datetime = models.DateTimeField()  # datetime is the date and time the form was created
    serialno = models.TextField()  # serialno stand for serial number
    partno = models.TextField()  # partno stand for part number
    Deliveryor = models.TextField()  # deliveryor stand for delivery order
    MCO = models.TextField()
    descriptionequipment = models.TextField()  # A short description about the equipment (*Optional)
    descriptionequipmentbox = models.TextField()  # A short description of the equipment in the box (*optional)
    descriptionbox = models.TextField()  # A short description of the box (*optional)
    document = models.TextField()  # A short description of the delivery note document (*optional)


    def __str__(self):
        return self.descriptionequipment

升序: alloutgoinglru = alloutgoinglru.order_by('category__name')

降序: alloutgoinglru = alloutgoinglru.order_by('-category__name')

models.py

class Category(models.Model):
    user = models.ForeignKey(
      User, on_delete=models.SET_NULL, null=True, blank=True)
    name = models.CharField(max_length=100, null=False, blank=False)
   
    class Meta:
       ordering = ('name',)
       verbose_name = 'Category'
       verbose_name_plural = 'Categories'

   def __str__(self):
       return self.name


class OutgoingLRU(models.Model):

    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, blank=True)
    image = models.ImageField(null=False, blank=False)
    equipmentimages = models.ImageField(null=False, blank=False)
    boximages = models.ImageField(null=False, blank=False)
    documentimage = models.ImageField(null=False, blank=False)

    datetime = models.DateTimeField()  # datetime is the date and time the form was created
    serialno = models.TextField()  # serialno stand for serial number
    partno = models.TextField()  # partno stand for part number
    Deliveryor = models.TextField()  # deliveryor stand for delivery order
    MCO = models.TextField()
    descriptionequipment = models.TextField()  # A short description about the equipment (*Optional)
    descriptionequipmentbox = models.TextField()  # A short description of the equipment in the box (*optional)
    descriptionbox = models.TextField()  # A short description of the box (*optional)
    document = models.TextField()  # A short description of the delivery note document (*optional)

    class Meta:
        verbose_name = 'OutgoingLRU'
        verbose_name_plural = 'OutgoingLRUs
        ordering = ('category__name',)

    def __str__(self):
        return self.descriptionequipment

之后:
1)运行 python manage.py makemigrations
2)运行 python manage.py 迁移