I have different model. Choices of Multiselctfield of one model is dependent on another model.So , database has to be queried inside model.py While doing so, this causes problem in migration. (Table doesn't exist error)
class Invigilator(models.Model):
---
# this method queries Shift objects and Examroom
def get_invigilator_assignment_list ():
assignment = []
shifts = Shift.objects.all()
for shift in shifts:
rooms= ExamRoom.objects.all()
for room in rooms:
assign = str (shift.shiftName)+ " " +str (room.name)
assignment.append (assign)
return assignment
assignment_choice = []
assign = get_invigilator_assignment_list()
i = 0
for assignm in assign:
datatuple = (i,assignm)
assignment_choice.append(datatuple)
i= i+1
ASSIGNMENT_CHOICE = tuple(assignment_choice)
assignment =MultiSelectField (choices = ASSIGNMENT_CHOICE, blank = True, verbose_name="Assignments")
ShiftandExamRoommodels are and what anassignmentis supposed to represent. So I can't elaborate more than saying that a ManyToMany relationship looks like what you need. Alternatively, create the choices in the form dynamically. But it cannot be done in the model. Your choices are dynamic, they change each time someone adds aShiftor aExamRoomto the database, so it cannot be set at creation time.Shift,InvigilatorandExamRoomand how are they related.