Django get_or_create creates object but its not saved in database

So i have a transfer function witch deletes one footballer from database and replaces him with another.I'm also trying to create new instance of transfer object that contains basic data about the transfers. The problem is that the get_or_create fuction returns True for created and even prints out an transfer object with increased pk (i.e. Transfer object(48), but its not appearing in the database.

models.py

class Transfer(models.Model):
    player_out = models.ForeignKey(MyFootballer, on_delete=models.CASCADE, null=True, blank=True)
    player_in = models.ForeignKey(Footballer, on_delete=models.CASCADE, null=True, blank=True)
    matchday = models.ForeignKey(Matchday, on_delete=models.CASCADE, null=True, blank=True)
    owner = models.IntegerField (null=True)

views.py

@login_required
def makeTransfer(request):
    player_in_ID = request.POST.get('playerinID')
    player_out_ID = request.POST.get('playeroutID')
    player_out = MyFootballer.objects.get(id = player_out_ID)
    matchday = player_out.matchday.id
    player_in = Footballer.objects.get(id = player_in_ID)
    squad = MyFootballer.objects.filter(owner = request.user, matchday = matchday)
    budget = 150000000 - calculate_squad_value(squad)
    new_budget = budget + player_out.footballer.price - player_in.price
    transfers = Transfer.objects.filter(owner = request.user.id, matchday_id = matchday)
    transfers_left = 3 - transfers.count()
    if transfers_left >= 1: 
        if new_budget <= 150000000:
            if player_out.footballer.position == player_in.position:
                new_transfer, created = Transfer.objects.get_or_create(player_out_id =player_out_ID, player_in_id = player_in_ID, matchday_id = matchday, owner = request.user.id)
                print ("here",new_transfer)
                MyFootballer.objects.filter(id = player_out.id).delete()
                MyFootballer.objects.create(footballer_id = player_in.id, matchday_id = matchday, owner = request.user, squad_role = player_out.squad_role)
                return JsonResponse({'status':created})
            else:
                return JsonResponse({'status':'err_pos'})
        return JsonResponse({'status':'err_bud'})
    return JsonResponse({"status":'err_trans'})

What's weird is that when i hardcode either player_in_id or player_out_id values, record is saved in the database. What am I doing wrong?

I tried objects.create, objects.save, objects.get_or_create and deleting database and reapplying migrations.

Back to Top