// signals.py
@receiver(pre_save, sender=AcademicLesson)
def take_attendance(sender, instance, **kwargs):
new_file = instance.class_img
if new_file:
sub = instance.sub_id
# this for loops gives all student from each branch having comman subject(sub = instance.sub_id)
for branch in sub.universitybranch_set.all():
for std in branch.studentprofile_set.all():
if std.profile_encode: # for understanding, student present or not
print('-------------------------added')
instance.student.add(std)
else:
print('---------------------not-added')
print('no. of student=', len(instance.student.all()))
# // models.py
class AcademicLesson(models.Model):
date = models.DateField()
sub_id = models.ForeignKey('UniversitySubject', on_delete=models.CASCADE, limit_choices_to={'is_elect': True})
class_type = models.CharField(max_length=7, choices=[('lecture','Lecture'),('lab','Lab')])
slot = models.CharField(max_length=1, choices=[('1','1'),('2','2'),('3','3'),('4','4'),('5','5'),('6','6')])
class_img = models.ImageField(upload_to=path_to_upload_media, blank=True)
student = models.ManyToManyField('StudentProfile', blank=True)
def __str__(self):
return f"{self.date} | {self.sub_id}"
class Meta:
verbose_name = 'Academic Lesson'
form which I submited from admin panel
whene I verify with django shell, it return <QuerySet []>
lesson = AcademicLesson.objects.get(date='2021-04-05')
lesson.student.all()
what I tried.
- override models.Model.save method but same problem occurs.
- forms.ModelForm does not suitable for our project
If I run code shown in signals.py in django shell then it work properly but, same logic does not work in signals.py
where did I go wrong?