The django framework uses orm to implement bulk data updates
- 2021-06-29 11:28:50
- OfStack
This article provides an example of how the django framework uses orm to batch update data.Share it for your reference, as follows:
I haven't used django to change my blog for a long time, and suddenly I feel uncomfortable.No way, that's why you play python, django in your spare time and work with java.Update the parentcode of a subclass at the same time when you write a category update with django, which is a good solution if you write your own native sql.However, since django is used, it is implemented with orm of django:
The easiest way:
MyModel.objects.filter(parentcode=ori_code).update(parentcode=new_code)
I forgot the most basic method.A complete example:
def updatecategory(request):
comtype=request.POST.get('comtype','')
catname=request.POST.get('categoryname','')
myid=int(request.POST.get('id',''))
parentid= request.POST.get('parentid','')
catcode=request.POST.get('categorycode','')
cat=models.Category.objects.get(autoid=myid)
ori_parent_code = cat.catcode;
if cat:
cat.comtype = comtype
cat.catname = catname
cat.parentcode = parentid
cat.catcode = catcode
cat.save()
if cat.parentcode == '-1': models.Category.objects.filter(parentcode=ori_parent_code).update(parentcode=cat.catcode)
This will give you the right results, even if it's basic, it's easy to forget.Old age, you have to take care of it. Write a log.
I hope that the description in this paper will be helpful to everyone's Python program design based on the Django framework.