jeudi 9 avril 2020

django sql-server hot to bind checkbox values to a specific id in database

im new to django an programming with pyton and right now i am trying to bind checkboxes to a ID (not the primary Key. Right now i can write the value of the checkboxes to the database, but everytime i do that django makes an insert but i need to do an update to the database. I get the right id true the url. If i use instance= suddenly no checkbox is stored at all but the id is erased from the database.

How can i bind the checkboxes to the posid in my database. So my database would be like

id  posid  vegaplan   mhd   ve    globalgap
74   1515       1      1     1       1

My form:

class MyForm(ModelForm):
    class Meta:
        model = MyForm
        fields = '__all__' (Fields are: id(pk),posid, vegaplan, mhd, ve, globalgap

my view:

def MyView(request, posid):

    fid = MyModel.objects.get(posid=posid)
    form = MyForm(request.POST, instance = fid)

    if request.method == "POST":

        vegaplan = 'vegaplan' in request.POST
        mhd = 'mhd' in request.POST
        ve = 've' in request.POST
        globalgap = 'globalgap' in request.POST

        tosave= models.MyModel(vegaplan=vegaplan, mhd=mhd, ve=ve,                   
                                         globalgap=globalgap)

        tosave.save(form)

    return render(request, "pages.html")

If some could help me or just give me a hind a would be very happy, right know i lost every hope:(




Aucun commentaire:

Enregistrer un commentaire