英文:
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_from
或number
对Booking
模型的对象进行排序,我该如何做?
英文:
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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论