0

I want to save changed values of ModelForm to database. I'm having problems even I follow this documentation if I'm right that it can be possible with initial values: Documentation- providing initial values

models.py:

class Settings(models.Model):
   url = models.URLField(max_length=100, default='https://website.com')
   maxCount = models.SmallIntegerField(default=30)

views.py:

def Settings(request):

    settingsObj = Settings.objects.get(id=1)
    form = SettingsForm(initial={'url': settingsObj.url, 'maxCount':settingsObj.maxCount}, instance=settingsObj)

    if form.is_valid():
        form.save()

forms.py:

class SettingsForm(forms.ModelForm):
    class Meta:
        model = Settings
        fields = ['url', 'maxCount']

templates

    <form class="form-horizontal" role="form" method="POST">{% csrf_token %}
  <div class="form-group">
    <div class="col-sm-offset-1 col-sm-6">
    {{ form.as_p }}
    </div>
  </div>
    <div class="form-group">        
      <div class="col-sm-offset-1 col-sm-6">
        <button type="submit" class="btn btn-primary btn-lg btn-block">Accept</button>
      </div>
    </div>
  </form>

Currently the form is showing the current values from database, but isn't saving changed data. form.is_valid() returns True, but form.save() seems to do nothing.

MMakela
  • 167
  • 2
  • 16
  • I didn't see any problem there – Raja Simon Jul 06 '16 at 07:52
  • I get shown the values when I remove "request.POST" when I call the form at "form = SettingsForm(), but if I now change field values, they aren't saved to the database. – MMakela Jul 06 '16 at 08:00
  • I don't see an `action` attribute in your `form` tag. – Yash Tewari Jul 06 '16 at 08:05
  • @YashTewari I don't want that the user is redirected away from the current site. It isn't the problem. Now I can see the values when the form is rendered, but the ModelForm isn't saving changed data. – MMakela Jul 06 '16 at 08:09
  • Just [pointing this out](http://stackoverflow.com/a/9401608/6492916) to ensure it isn't the reason behind your problems. It probably isn't. – Yash Tewari Jul 06 '16 at 08:16
  • Add a `print` statement under `if form.is_valid():` to test if the flow ever reaches that point? – Yash Tewari Jul 06 '16 at 08:22
  • @YashTewari Now the form.is_valid() returns True when I changed `form = SettingsForm(initial={'url': settingsObj.url, 'maxCount':settingsObj.maxCount}, instance=settingsObj)`, but the form isn't still saved. – MMakela Jul 06 '16 at 08:25

1 Answers1

1

The initial argument and the instance argument in your call to SettingsForm() serve the exact same purpose, because you are using the fields of the instance individually in each field of initial.

The save() method is not working because you need to populate the form with data from request.POST.

This view should work:

def settings(request):    
    settingsObj = Settings.objects.get(id=1)
    if request.POST:
        form = SettingsForm(request.POST, instance=settingsObj)    
        if form.is_valid():
            form.save()
    else:
        form = SettingsForm(instance=settingsObj)

    context = { ..., 'form': form, ... }
    return render(request, 'template-address', context)
Yash Tewari
  • 760
  • 1
  • 6
  • 19
  • This works, but I can get only the url value to field, when the form is rendered. The maxCount-field is empty, when it has a value in database. Why is that? – MMakela Jul 06 '16 at 10:01