0

I am making a portal for the procedure of Interview rounds. My basic code is as follows:

models.py

class Round(models.Model):
    pending_students = models.ManyToMany(User, related_name='pending_student', blank=True)
    done_students = models.ManyToMany(User, related_name='done_students', blank=True)
    round_number = models.PositiveIntegerField(default=0)
    #other fields

First, for the current round, I am displaying pending_students and done_students separately. Current user will bring some pending to done(and done to pending if he wants) and then he will submit. I want that, On submitting, next round should be created. Now I want that if new round is created, then all done_students of previous round should come to pending_students for this newly created round. I am using generic create view (but if needed, I am fine with writing manually).

Now problem is that in create view, how can I know which student should go to next round (means who are in the list of done_students on client side) ? I want to know, how can I pass the id's of all done_students from HTML page to newly called create view to create next round ?

Any suggestion for different way is welcomed. I want to get it any way.

e4c5
  • 52,766
  • 11
  • 101
  • 134
Chaitanya Patel
  • 390
  • 1
  • 4
  • 15
  • using a signal is probably overkill. Are you using a formset here to change pending/done? can you update post wtih relevent section of view? – e4c5 Sep 18 '16 at 01:05
  • There is no any form.(I don't know if any form is applicable or not.) For client side, I will do it manually. Like I will have two list : pending and done. Current user will do some flipping. After he submits, I will get javascript array of all id's of done_students. Now I want to pass it to the create view of round model to create next round. – Chaitanya Patel Sep 18 '16 at 09:41
  • I think you really ought to sit down and read the section on forms. https://docs.djangoproject.com/en/1.10/topics/forms/ – e4c5 Sep 18 '16 at 13:34

2 Answers2

0

You could add submitted = models.BooleanField(default=False) to your round. When you submit a round, the value of submitted becomes True.

Then, you can create a signal that will check when Round is saved, and whether submitted is True. In which case you can automatically create a new Round and then use the add() method to assign it all the passing students.

Something like :

from django.db.models.signals import pre_save
from django.dispatch import receiver
from myapp.models import Round

@receiver(post_save, sender=Round)
def my_handler(sender, **kwargs):
    instance = past_round
    if past_round.submitted :
        next_round = Round()
        next_round.save()
        round_students = [student.pk for student in past_round.done_students]
        next_round.pending_students.add(round_students)
Brachamul
  • 1,886
  • 2
  • 21
  • 34
  • Your method works only if students are already in done_students. Here for current job, say pending_students has all students and done_students is empty (like at the starting of the round). Current user does some flipping on client side. Then he will submit. At that time, I want to add all those students who are in done_students list on client side, to done_student field of that round object. More briefly : I will get a javascript list of id's of all done_students, I will somehow pass it to view. In view, I will add those id's to done_students of current round, and then initialize next round. – Chaitanya Patel Sep 18 '16 at 09:34
  • I want to know how can I pass those id's from client side to view ? If I am somehow able to pass them then I can finish this current round and initialize next round (Your given method will do the initialization of next round). – Chaitanya Patel Sep 18 '16 at 09:37
0

I did in a different and a cool way.

I made a shortlisting page and after selecting students for next round, user will click update button. By that, the ajax call will handle the change in m2m relationships in backend. (I found it convenient to use ajax for sending a list instead of sending via args and refreshing the page.)

User can surf and update as much time as he wants.

After that he can click lock round, and the new round will be created as required.

Chaitanya Patel
  • 390
  • 1
  • 4
  • 15