Q:

Easiest way to update model values using Django with an AJAX form

I have a django model which contains some fields with associated choices:
class Product(models.Model):
    CONDITION_CHOICES = (
        ("GOOD", "Good"),
        ("BAD", "Bad"),
        ("UNKNOWN", "Unknown"),
    )

    name = models.CharField(max_length=200, blank=True, null=True)
    colour = models.CharField(max_length=200, blank=True, null=True)

    condition = models.CharField(max_length=20, choices=CONDITION_CHOICES, blank=True, null=True)
    condition_source = models.CharField(max_length=20, blank=True, null=True)
    condition_last_updated = models.DateTimeField(blank=True, null=True)

I also have a bootstrap driven form that looks like:
<div class="form-group">
    <label class="control-label"><strong>Condition</strong></label>
    <br/>
    <div class="btn-group btn-group-toggle" data-toggle="buttons">
        <label class="btn btn-outline-primary">
            <input type="radio" name="condition" value="GOOD" autocomplete="off">
            Good
        </label>
        <label class="btn btn-outline-primary">
            <input type="radio" name="condition" value="BAD" autocomplete="off">
            Bad
        </label>
        <label class="btn btn-outline-primary">
            <input type="radio" name="condition" value="UNKNOWN" autocomplete="off">
            Unknown
        </label>
    </div>
</div>

I am trying to make it so that when a user clicks on one of the buttons in the UI, the Product model is updated (specifically the condition, condition_source and condition_last_updated fields). The actual model has multiple fields with associated with choice options so I'd like the model to get updated in real time without a page reload as a user works through the form.
Any guidance would be appreciated here - I have looked at intercooler.js but unsure if this is the right tool for the job.

A:

As you have not specified what condition_source should include I have set it to string some_source
Ajax:
$('.btn').on('click', function(){
    $.post(
           '/your_vew/',
           {
             'source': "some_source",
             'condition': $(this).find('input').val(),
             'csrfmiddlewaretoken': '{{csrf_token}}'
           },
           function(data){
              console.log(data.response);
           }
     );    
});  

urls.py:
urlpatterns = [
    ...
    path('your_vew/', views.your_view), 
    ...
]

views.py:
from django.http import JsonResponse
from datetime import datetime

def your_view(request):
    data = {'response': ''}
    if request.method == 'POST':
        p1 = Product.objects.filter(pk=1).update(
              condition_source=request.POST.get('source'),
              condition=request.POST.get('condition'),
              condition_last_updated=datetime.now()
            )
        if p1:
            data['response'] = 'Record updated!'    
    return JsonResponse(data) 

