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

谁都会走 提交于 2020-06-17 14:54:30

问题


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:(

来源:https://stackoverflow.com/questions/61125585/django-sql-server-hot-to-bind-checkbox-values-to-a-specific-id-in-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!