根据其他两个现有模型创建新模型

时间:2019-04-20 23:15:39

标签: django

我正在建立一个django项目,我有2个现有的模型Offer和Candidates,我想做的就是基于这两个模型Offer和Candidates创建一个新模型(计分模型),所以我创建了一个函数我的Offer模型中的add_line()应该会创建Scoring对象,但是即使我使用了post_save信号,它也不会起作用,有什么建议吗?

候选人模型:

class Candidates(models.Model):
    nom = models.CharField(max_length = 50)
    prenom = models.CharField(max_length= 50)
    email = models.EmailField()
    phone = models.CharField(max_length=100)
    niveau_d_experience = models.IntegerField() 
    etablissement_universitaire= models.CharField(max_length=150)
    niveau_options = (('Licence', 'Licence'), ('Master', 'Master'), ("Diplôme d'ingénieur", "Diplôme d'ingénieur"), ('Doctorat', 'Doctorat'))
    niveau_educatif =  models.CharField(choices=niveau_options, max_length=100)
    profil = models.CharField(max_length=100)
    sexe_options = (('Mâle', 'Mâle'), ('Femelle', 'Femelle'))
    sexe = models.CharField(choices=sexe_options, max_length=100)
    offre = models.CharField(max_length= 200)
    CV = models.FileField()
    date_creation = models.DateTimeField(auto_now_add=True, auto_now=False)  
    text_resume = models.TextField(editable=False)
    language = models.CharField(max_length = 20, editable=False)

优惠模式:

class Offers(models.Model):
    titre = models.CharField(max_length=120)
    description = models.TextField()
    technologies = models.TextField()
    softskills = models.TextField()
    expérience = models.IntegerField()
    contrat_options= (('CDI', 'CDI'), ('CDD', 'CDD'), ('Freelance', 'Freelance'), ('Stage', 'Stage'))
    contrat =  models.CharField(choices=contrat_options, max_length=100)
    mission_options = (('Interne', 'Interne'), ('Externe', 'Externe'))
    mission= models.CharField(choices=mission_options, max_length=100)
    validation_options = (('Offre validée', 'Offre validée'), ('Offre pourvue', 'Offre pourvue'))
    validation = models.CharField(choices =validation_options, max_length=100)

    def add_line(self, **kwargs):
        queryset = Candidates.objects.all()
        for candidate in queryset:
            scoring, created = Scoring.objects.get_or_create(
                offer_id = self,
                cand_id = candidate
                )
        return scoring


    def __str__(self):
        return self.titre

和评分模型:

class Scoring(models.Model):
    offer_id = models.ForeignKey(Offers, on_delete = models.DO_NOTHING, editable = False)
    cand_id = models.ForeignKey(Candidates, on_delete = models.DO_NOTHING, editable= False)

0 个答案:

没有答案