I’m currently doing the below approach. However, I’m not sure if doing a workcycle.applicants.add
will always return in the set up random order (the order users came in with order_by(‘?’)). Does anyone know about this?
class WorkCycle(models.Model):
job = models.ForeignKey(Job, on_delete=models.CASCADE)
applicants = models.ManyToManyField(Applicant)
@classmethod
def create(cls, job):
workcycle = cls(job=job)
tags = [str(tag) for tag in job.tags.all()]
users = User.objects.filter(tags_string__contains=tags[0], role="employee") # TODO Check
for tag in tags[1:]:
users = User.objects.filter(tags_string__contains=tag)
if not users:
break
workcycle.save() # BUGFIX: Save call must go prior to many-to-many field editing (or weird "object needs id" exception)
for user in users.order_by('?'): # Return users in random order
applicant = Applicant(user=user, job=job, created_at=timezone.now(), comment="(TODO)")
applicant.save()
workcycle.applicants.add(applicant)
return workcycle
def __str__(self):
return f'Work cycle for Job ID: {self.job.id}'
If the above approach doesn’t work, then how would I do it? I need both a random order of users (applicants) starting out and also the ability to insert an applicant at a random location (haven’t coded this yet).