25-3-11 10:16
Alice (培训机构管理者)
Hello Bob, I'm looking for a way to optimize our course scheduling system. Could you help me with that?
Bob (软件工程师)
Sure Alice, we can start by creating a basic structure using Python and Django framework. Here's a simple model for the Course class:
class Course(models.Model):

name = models.CharField(max_length=100)
duration = models.IntegerField()
capacity = models.IntegerField()
]]>
We can then create a Schedule model to manage the timing and availability:
class Schedule(models.Model):
course = models.ForeignKey(Course, on_delete=models.CASCADE)
start_time = models.DateTimeField()
end_time = models.DateTimeField()
]]>
Next, let's implement a function to check if a course can be scheduled at a specific time:
def is_course_scheduled(course, start_time, end_time):
conflicting_schedules = Schedule.objects.filter(
Q(start_time__lt=end_time) & Q(end_time__gt=start_time)
)
return conflicting_schedules.exists()
]]>
This function will help us avoid overlapping schedules. We also need to consider the capacity of each course:
def schedule_course(course, start_time, end_time):

if not is_course_scheduled(course, start_time, end_time):
Schedule.objects.create(
course=course,
start_time=start_time,
end_time=end_time
)
return True
return False
]]>
Finally, we can integrate this into a user-friendly interface for easy management.
What do you think, Alice? Does this sound like a good approach?
Alice
That sounds great, Bob! Let's proceed with this implementation and see how it improves our operations.