如何更新模型查询

时间:2019-06-07 17:26:48

标签: django django-models django-views

我正在开发一个网络应用程序,它将指导学生明年选择什么,如果普通平均水平> 12并且特殊== Si,他可以选择Si,否则他必须选择ISIL

我想将特殊项目更新为ISIL

PS:即时通讯在django

views.py:

def resultats(request,id=None):

    etudiant = MyUser.objects.all
    etud_si = MyUser.objects.all().filter(specialite='SI')
    etud_isil = MyUser.objects.all().filter(specialite='ISIL')
     moy = MyUser.objects.all().annotate(moyenne =(F('moyenne_s1')+F('moyenne_s2'))/2).filter(moyenne__gt=11.99,specialite='SI')
moy1 = MyUser.objects.all().annotate(moyenne =(F('moyenne_s1')+F('moyenne_s2'))/2).filter(moyenne__lt=11.99,specialite='SI')

if moy1 :
    moy1.specialite = 'ISIL'
    moy1.save()


    if id:
        pr = get_object_or_404(MyUser,id=id)
    else:
        pr = request.user


    context = {
        'etudiant':etudiant,
        'profile':pr,
        'etud_si':moy,
        'etud_isil':moy1

    }

     return render(request,'resultats.html',context)

models.py:

class MyUser(AbstractBaseUser,PermissionsMixin):
    SI = 'SI'
    ISIL = 'ISIL'
    SPEC_CHOICES = [
        (SI,'SI - System Informatique'),
        (ISIL,'ISIL - Ingenieur system informatique et logiciels')

    ]

     username = models.CharField(
                    max_length=300,
                    validators = [
                        RegexValidator(regex = USERNAME_REGEX,
                                        message = 'Nom d\'utilisateur   doit etre Alphanumeric',
                                        code = 'nom d\'utilisateur   invalid'
                    )],
                unique = True
            )
email = models.EmailField(unique = True)
nom = models.CharField(default=' ',max_length=300)
prenom = models.CharField(default=' ',max_length=300)
moyenne_s1 =models.DecimalField(default=00.00,max_digits=4,decimal_places=2)
moyenne_s2 = models.DecimalField(default=00.00,max_digits=4,decimal_places=2)
specialite = models.CharField(max_length=300,choices=SPEC_CHOICES,default=SI)

is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)

objects = UserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email','moyenne_s1','moyenne_s2']

def get_update(self):
        return reverse("edit", kwargs={"id": self.id})

@property
def moyenne_gen(self):
    return (self.moyenne_s1+self.moyenne_s2)/2

1 个答案:

答案 0 :(得分:0)

我解决了:

def resultats(request,id=None):

etudiant = MyUser.objects.all
moy = MyUser.objects.all().annotate(moyenne =(F('moyenne_s1')+F('moyenne_s2'))/2).filter(moyenne__gt=11.99,specialite='SI')
moy1 = MyUser.objects.all().annotate(moyenne =(F('moyenne_s1')+F('moyenne_s2'))/2).filter(moyenne__lt=11.99)

if moy1 :
    moy1.specialite = 'ISIL'
    moy1.update(specialite='ISIL')


if id:
    pr = get_object_or_404(MyUser,id=id)
else:
    pr = request.user


context = {
    'etudiant':etudiant,
    'profile':pr,
    'etud_si':moy,
    'etud_isil':moy1

}

return render(request,'resultats.html',context)