This is my django model:
Instructors model that is being used for registered instructors:
class Instructors(models.Model):
username = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, to_field='username')
contact_home = models.CharField(max_length=200)
contact_mobile = models.CharField(max_length=200)
start_date = models.DateTimeField('Joining Date', default=None)
end_date = models.DateTimeField('Relieving Date', default=None, null=True, blank=True)
comments = models.CharField(max_length=500, blank=True, default='None')
class Meta:
verbose_name = "Instructors"
verbose_name_plural = "Instructors"
def __str__(self):
return '{}'.format(self.username)
Name of the course
class Course(models.Model):
course_name = models.CharField(max_length=200, unique=True)
course_indentifier = models.CharField(max_length=200, unique=True)
course_type = models.ForeignKey(CourseType, on_delete=models.PROTECT, to_field='course_type')
def __str__(self):
return '{} - {} - {}'.format(self.course_name, self.course_indentifier, self.course_type)
Schedule created for instructor with course name and room number with a date range which cannot clash
class Schedule(models.Model):
instructor = models.ForeignKey(settings.AUTH_USER_MODEL, unique=False, on_delete=models.PROTECT, to_field='username')
start_date = models.DateTimeField('Start Date', default=None)
end_date = models.DateTimeField('End Date', blank=True, null=True)
room_number = models.ForeignKey(Room, unique=False, on_delete=models.PROTECT, to_field='room_number')
student_numbers = models.IntegerField('Student Numbers', default=1)
course_name = models.ForeignKey(Course, unique=False, on_delete=models.PROTECT, to_field='course_name')
def __str__(self):
return '{}, Start {}, End {} Room {}'.format(self.course_name.course_name, self.start_date, self.end_date, self.room_number.room_number)
The following attendance is whether the instructor took the course of not. The course_name field is a foreign key to Schedule. This pulls all the schedule created for the instructor and other instructor. I want to filter the course_name for InstructorAttendance only based on/filtered on the specific loggedin instructor
(username of the admin user model as seen in instructor model). I am not able to do a queryset in InstructorAttendance queryset and filter it
. Second, the schedule should not save if the date range clashes for a room
which I am trying to manipulate the save_model function in ScheduleAdmin which is failing saying objects and filter functions are not available in the variable qs.
class InstructorAttendance(models.Model):
instructor = models.CharField(max_length=200, editable=False)
course_name = models.ForeignKey(Schedule, on_delete=models.PROTECT, default=None, related_name='schedule_course_name')
lecture_date = models.DateField('Lecture Date', default=None, auto_created=False, blank=False, null=False)
loggedin_date = models.DateTimeField('Attendance Entry Date', default=timezone.now, editable = False, auto_created=True)
def __str__(self):
return '{} Conducted by {}'.format(self.lecture_date, self.instructor)
I am registering my model using the following:
class ScheduleAdmin(admin.ModelAdmin):
fieldsets = [
(None, { 'fields': ['instructor', 'start_date','end_date', 'room_number', 'student_numbers', 'course_name'] })
]
def save_model(self, request, obj, form, change):
# qs = super(ScheduleAdmin, self).filter(room_number=1)
# qs.filter(room_number=request.POST.room_number)
# if len(qs) == 0:
return super(ScheduleAdmin, self).save_model(request, obj, form, change)
admin.site.register(Schedule, ScheduleAdmin)
This thing I am trying is apply a filter by searching the model Schedule in the database and then show the options which are of the a specific room_name. However, even if i try get_queryset and then filter or just a filter it throws an error saying the object/function is not available in the variable. How do I save a list of options based on room_name option only?
I have also been trying forms to just show the available options based on room_name but couldnt get it working. Any help is welcome.