import json
from django.http import Http404, HttpResponse
from main.models import Personnages
def store_data(request):
if request.is_ajax() and request.POST:
value = request.POST.get("value")
toChange = request.POST.get("name")
id = request.POST.get("id") #A IMPORTER
perso = Personnages.objects.get(id=id)
perso.toChange = value
perso.save()
return HttpResponse(value+toChange)
else :
raise Http404
I have written this code but the problem is that this part does not work:
perso.toChange = value
This doesn't seem to work. I figure it is because I extract the string from JSON instead of the field.
This is the model for reference:
class Personnages(models.Model):
id = models.AutoField(primary_key=True)
Joueur = models.ForeignKey(User, on_delete=models.PROTECT)
Nom = models.CharField(max_length=40)
Age = models.CharField(max_length=20)
Genre = models.CharField(max_length=30)
Date = models.DateTimeField(default=timezone.now, verbose_name="Date de parution")
Image = models.ImageField(upload_to="imagePerso/", null=True)