How do I edit and delete data in Django?

后端 未结 3 1855
离开以前
离开以前 2020-12-29 12:00

I am using django 1.0 and I have created my models using the example in the Django book. I am able to perform the basic function of adding data; now I need a way of retriev

3条回答
  •  臣服心动
    2020-12-29 12:37

    To do either of these you need to use something called queries.

    check link below for really great documentation on that! (https://docs.djangoproject.com/en/2.2/topics/db/queries/)

    To Delete Data:

    b = ModelName.objects.get(id = 1)
    b.delete()
    

    This will delete the Object of the model w/ an ID of 1

    To edit Data:

    b = ModelName.objects.get(id = 1)
    b.name = 'Henry'
    b.save()
    

    This will change the name of the Object of the model w/ an ID of 1 to be Henry

提交回复
热议问题