How to sort a model's objects if the model has foreign key relations and I have to sort based on the properties of the foreign model?

huangapple go评论69阅读模式
英文:

How to sort a model's objects if the model has foreign key relations and I have to sort based on the properties of the foreign model?

问题

如何根据外键关系模型的属性对模型的对象进行排序?

以下是我的模型:

class Room(models.Model):
    class Meta:
      ordering = ['number']
    number =  models.PositiveSmallIntegerField(
        validators=[MaxValueValidator(550), MinValueValidator(1)],
        primary_key=True
        )
    CATEGORIES = (
        ('Regular', 'Regular'),
        ('Executive', 'Executive'),
        ('Deluxe', 'Deluxe'),
    )
    category = models.CharField(max_length=9, choices=CATEGORIES, default='Regular')
    CAPACITY = (
        (1, '1'),
        (2, '2'),
        (3, '3'),
        (4, '4'),
    )
    capacity = models.PositiveSmallIntegerField(
        choices=CAPACITY, default=2
        )
    advance = models.PositiveSmallIntegerField(default=10)
    manager = models.ForeignKey(
        settings.AUTH_USER_MODEL, on_delete=models.CASCADE
    )

class TimeSlot(models.Model):
    class Meta:
        ordering = ['available_from']
    room = models.ForeignKey(Room, on_delete=models.CASCADE)
    available_from = models.TimeField()
    available_till = models.TimeField()

class Booking(models.Model):
    customer = models.ForeignKey(User, on_delete=models.CASCADE)
    check_in_date = models.DateField()
    timeslot = models.ForeignKey(TimeSlot, on_delete=models.CASCADE)

假设我要根据available_fromnumberBooking模型的对象进行排序,我该如何做?

英文:

How to sort a model's objects if the model has foreign key relations and I have to sort based on the properties of the foreign model?

Here is my model

class Room(models.Model):
    class Meta:
      ordering = ['number']
    number =  models.PositiveSmallIntegerField(
        validators=[MaxValueValidator(550), MinValueValidator(1)],
        primary_key=True
        )
    CATEGORIES = (
        ('Regular', 'Regular'),
        ('Executive', 'Executive'),
        ('Deluxe', 'Deluxe'),
    )
    category = models.CharField(max_length=9, choices=CATEGORIES, default='Regular')
    CAPACITY = (
        (1, '1'),
        (2, '2'),
        (3, '3'),
        (4, '4'),
    )
    capacity = models.PositiveSmallIntegerField(
        choices=CAPACITY, default=2
        )
    advance = models.PositiveSmallIntegerField(default=10)
    manager = models.ForeignKey(
        settings.AUTH_USER_MODEL, on_delete=models.CASCADE
    )

class TimeSlot(models.Model):
    class Meta:
        ordering = ['available_from']
    room = models.ForeignKey(Room, on_delete=models.CASCADE)
    available_from = models.TimeField()
    available_till = models.TimeField()

class Booking(models.Model):
    customer = models.ForeignKey(User, on_delete=models.CASCADE)
    check_in_date = models.DateField()
    timeslot = models.ForeignKey(TimeSlot, on_delete=models.CASCADE)

Suppose I have to sort the objects of the Booking model on the basis of available_from or number how will I do it?

答案1

得分: 0

Django QuerySet API 的方法名为 order_by(...),您可以像这样添加相关字段名称:

# this will sort queryset based on `available_from`
Booking.objects.order_by('timeslot__available_from')

# this will sort queryset based on `number`
Booking.objects.order_by('timeslot__room__number')
英文:

Django QuerySet API have method name as order_by(...) in which you can add related field name like this

# this will sort queryset based on `available_from`
Booking.objects.order_by('timeslot__available_from')

# this will sort queryset based on `number`
Booking.objects.order_by('timeslot__room__number')

huangapple
  • 本文由 发表于 2023年5月22日 01:25:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定