0

I am having issue's saving my form multiple times. I want the user to fill out my form with as many dimensions as possible(this is the form) so, If they did it once only insert one record into my db if they do two dimensions save each one to my db totaling two new records. Any help figuring this out will be greatly appreciated.

Here is my ajax

function SaveDim() { 

    $.ajax({ 
        type: "POST", 
        url: "/sheet/sheet_form_create.html/_dim", 
        //dataType: "json", 
        data: $('#dim_form').serialize() 
            + 
            "&description=" + $('#id_description').val() + 
            "&style=" + $('#id_style').val() + 
            "&target=" + $('#id_target').val() + 
            "&upper_limit=" + $('#id_upper_limit').val() + 
            "&lower_limit=" + $('#id_lower_limit').val() + 
            "&inspection_tool=" + $('#id_inspection_tool').val() + 
            "&critical=" + $('#id_critical').val() + 
            "&units=" + $('#id_units').val() + 
            "&metric=" + $('#id_metric').val() + 
            "&target_strings=" + $('#id_target_strings').val() + 
            "&ref_dim_id=" + $('#id_ref_dim_id').val() + 
            "&nested_number=" + $('#id_nested_number').val() + 
            "&posistion=" + $('#id_position').val() + 
            "&met_upper=" + $('#id_met_upper').val() + 
            "&met_lower=" + $('#id_met_lower').val() + 
            "&valc=" + $('#id_valc').val() + 
            "&sheet_id=" + $('#id_sheet_id').val() + 
            "", 
        success: function (json) { 
            console.log(json); 
            alert(json); 

        } 
    }); 
} 

here is my views.py (add_dimension) method

def add_dimensions(request): 
  if request.method == 'POST': 
    c_date = datetime.now() 
    u_date = datetime.now() 
    description = request.POST.get('description') 
    style = request.POST.get('style') 
    target = request.POST.get('target') 
    upper_limit = request.POST.get('upper_limit') 
    lower_limit = request.POST.get('lower_limit') 
    inspection_tool = request.POST.get('inspection_tool') 
    critical = request.POST.get('critical') 
    units = request.POST.get('units') 
    metric = request.POST.get('metric') 
    target_strings = request.POST.get('target_strings') 
    ref_dim_id = request.POST.get('ref_dim_id') 
    nested_number = request.POST.get('nested_number') 
    met_upper = request.POST.get('met_upper') 
    met_lower = request.POST.get('met_lower') 
    valc = request.POST.get('valc') 
    sheet_id = request.POST.get('sheet_id') 
    data = {} 
    dim = Dimension( 
          description=description, 
          style=style, 
          target=target, 
          upper_limit=upper_limit, 
          lower_limit=lower_limit, 
          inspection_tool=inspection_tool, 
          critical=critical, 
          units=units, 
          metric=metric, 
          target_strings=target_strings, 
          ref_dim_id=ref_dim_id, 
          nested_number=nested_number, 
          met_upper=met_upper, 
          met_lower=met_lower, 
          valc=valc, 
          sheet_id=sheet_id, 
          created_at=c_date, 
          updated_at=u_date) 
    dim.save() 
    data['description'] = dim.description; 
    data['style'] = dim.style; 
    data['target'] = dim.target; 
    data['upper_limit'] = dim.upper_limit; 
    data['lower_limit'] = dim.lower_limit; 
    data['inspection_tool'] = dim.inspection_tool; 
    data['critical'] = dim.critical; 
    data['units'] = dim.units; 
    data['metric'] = dim.metric; 
    data['target_strings'] = dim.target_strings; 
    data['ref_dim_id'] = dim.ref_dim_id; 
    data['nested_number'] = dim.nested_number; 
    data['met_upper'] = dim.met_upper; 
    data['met_lower'] = dim.met_lower; 
    data['valc'] = dim.valc; 
    data['sheet_id'] = dim.sheet_id; 
    return HttpResponse(json.dumps(data), content_type="application/json",) 

  else: 
      dim_form = DimForm() 
      return render(request, 'app/_dim.html', {'dim_form': dim_form})     

Model

class Dimension(models.Model): 
  description = models.CharField(max_length=255) 
  style = models.CharField(max_length=255) 
  created_at = models.DateField() 
  updated_at = models.DateField() 
  target = models.IntegerField() 
  upper_limit = models.IntegerField() 
  lower_limit = models.IntegerField() 
  inspection_tool = models.CharField(max_length=255) 
  critical = models.IntegerField() 
  units = models.CharField(max_length=255) 
  metric = models.CharField(max_length=255) 
  target_strings = models.CharField(max_length=255) 
  ref_dim_id = models.IntegerField() 
  nested_number = models.IntegerField() 
  #position = models.IntegerField() 
  met_upper = models.IntegerField() 
  met_lower = models.IntegerField() 
  valc = models.CharField(max_length=255) 
  sheet = models.ForeignKey(Sheet, on_delete=models.CASCADE, default=DEFAULT_FOREIGN_KEY) 

Screen Shot

Snowman08
  • 425
  • 1
  • 4
  • 16
  • can you share your model definition? it should work but I have some recommendation (defining updated_at and created_at at model level, not mixing POST and GET, using django form for validating data...). on the other hand if you have an error message please share it. – szaboat Jun 29 '16 at 11:35
  • I updated my question @szaboat -- I have no errors everything works correctly but when trying to save multiples its always the same data even if I change a text value. – Snowman08 Jun 29 '16 at 11:41

1 Answers1

1

change the change dim.save() to Dimension.objects.create()

https://stackoverflow.com/a/23926742/475565

you also should change id's to classes

$('.dim_form').on('submit', function(){
  var data = $(this).serialize();

  // send the ajax call here. $(this) is the form instance
  var description = $(this).find('input[name=description]').val();
  ...
});
Community
  • 1
  • 1
szaboat
  • 593
  • 3
  • 11
  • I added a screen shot for a better example of the issue @szaboat – Snowman08 Jun 29 '16 at 11:56
  • As my screen shot shows the same dim form but multiple times due to me clicking the button twice to add dimension but when I save dim it always saves with the first's values as in the second one should be outter for description as you can see in my console its still set to inner @szaboat – Snowman08 Jun 29 '16 at 11:57
  • hmm, ok, I think I get it. you are always sending the same data. Form id's should be unique. you have multiple forms on the page but you are referencing to one and I guess you are always sending the same data. `$('#dim_form')` will always select the same even if there are multiple one on the page. Try to get the form from javascript which you edited. – szaboat Jun 29 '16 at 12:01
  • change the id's of the inputs to names and you can reference to them by name. id's are always unique. if there are multiple present browsers will get the first one. – szaboat Jun 29 '16 at 12:03
  • Any idea on how I could change them to be unique using javascript? @szaboat – Snowman08 Jun 29 '16 at 12:04
  • I checked the names and they are identical as well. @szaboat – Snowman08 Jun 29 '16 at 12:08
  • 1
    yes, but that is the problem. Here is a tiny example what I've put together to illustrate what you have to do to handle multiple forms on the page. http://jsbin.com/kimafobigo/edit?html,css,js,output – szaboat Jun 29 '16 at 12:52
  • Thank you I'll give it a shot @szaboat – Snowman08 Jun 29 '16 at 13:21