How do I create a function to update many entries at once in Django SQLite database?

Viewed 52

I have a functioning application, but would like to streamline aspects. I have create, update and view pages for a table of about 20 instances of Post2. I would like to assign a function to a button that would update all player1, player2 and player3 to "Empty". Even better would be to set it to a weekly timer. Any help would be very much appreciated, nothing I've tried has worked. The following is my models.py-

class Post2(models.Model):
    time=models.CharField(max_length=50)
    player1=models.CharField(max_length=50, default="Player 1")
    player2=models.CharField(max_length=50, default="Player 2")
    player3=models.CharField(max_length=50, default="Player 3")
    
    def __str__(self):
        return self.time

This is my views.py-

def teetimes(request):
    posts=Post2.objects.all()
    return render(request, 'memtees2/teetimes.html', {'posts':posts})

def add(request):
    if request.method=='POST':
        time=request.POST['time']
        player1=request.POST['player1']
        player2=request.POST['player2']
        player3=request.POST['player3']
        Post2.objects.create(time=time,player1=player1,player2=player2,player3=player3)
        messages.success(request,'New Time has been added')
    return render(request,'memtees2/add.html')

def update(request,id):
    if request.method=='POST':
        time=request.POST['time']
        player1=request.POST['player1']
        player2=request.POST['player2']
        player3=request.POST['player3']
        Post2.objects.filter(id=id).update(time=time,player1=player1,player2=player2,player3=player3)
        messages.success(request,'Information has been updated, return to Tee-Times to view ')
    post=Post2.objects.get(id=id)
    return render(request,'memtees2/update.html',{'post':post})
1 Answers

I am not sure if I understand your question correctly. You want a function that clears the player fields for all posts?

def clear_players(request):
    Post2.objects.all().update(player1=None, player2=None, player3=None
Related