Django“ models1.foreignkey”必须是“ models2”实例

时间:2019-09-03 16:24:49

标签: python django forms model instance

Cannot assign "<QuerySet [<authentifier: authentifier object (1)>]>": "Resasalle.authentifier" must be a "authentifier" instance.

models.py:

class authentifier(models.Model):
   matricule =models.CharField(max_length=254, blank=True, null=True)
   password = models.CharField(max_length=254, blank=True, null=True)
   nom =models.CharField(max_length=254, blank=True, null=True)
   prenom=models.CharField(max_length=254, blank=True, null=True)
   statut = models.CharField(max_length=254, blank=True, null=True)

class Resasalle(models.Model):
    idrs = models.AutoField(primary_key=True)

    nomsalle = models.CharField(max_length=254, blank=True, null=True)
    motifresa = models.CharField(max_length=254, blank=True, null=True)
    datedebut = models.DateField(blank=True, null=True)
    heuredebut = models.TimeField(blank=True, null=True)
    heurefin = models.TimeField(blank=True, null=True)
    authentifier = models.ForeignKey(authentifier,on_delete=models.CASCADE , default="")

views.py:

def reserversalle(request , id):

    form= ReserverSalle(request.POST or None)
    object = models.authentifier.objects.filter(id=id)
    print(object)
    if form.is_valid():
        print("gooddd")
        reserver = models.Resasalle()
        reserver.nomsalle = form.cleaned_data['nomsalle']
        reserver.motifresa = form.cleaned_data['motifresa']
        reserver.datedebut = form.cleaned_data['datedebut']
        reserver.heuredebut = form.cleaned_data['heuredebut']
        reserver.heurefin = form.cleaned_data['heurefin']
        reserver.authentifier = object

        reserver.save()



    context= { 
            'form' : form ,
        }
    return render(request,'registration/reserversalle.html', context)

reserversalle.html:     拯救萨尔!

<form  method="post">

        {%  csrf_token %}
        {{ form.as_p }}

        <input type="submit" value="reserver">

</form>

您好,我需要将此表格保存在我的数据库中,但是如果有人知道如何解决此错误,请尝试在我的数据库中添加此表格

0 个答案:

没有答案