Thursday, July 26, 2012

Re: form.save() fails silently - how to debug?

Thank you for the suggestion, but unfortunately that too does not work. 

I really need to find a way to get at the source of the problem; I would think an error message should be generated but none shows up...

On Thursday, July 26, 2012 9:47:00 AM UTC+2, Karl Sutt wrote:
I think
results = formset.save(commit=False)
formset.save()
should be
results = formset.save(commit=False)
results.save() 

Tervitades/Regards
Karl Sutt


On Thu, Jul 26, 2012 at 10:35 AM, Derek  wrote:
I'd appreciate some help with the following problem, under Django 1.4

When running with the models, forms and view as described below, the data from the form is *not* saved into the model.

However, when using the test code (shown at the end) in a shell environment, the data *is* saved as expected.

How can I tell why the form data is not saved in the view?  It appears to "fail silently" (i.e. no errors reported) - but adding print statements into the code reveal that the correct data is available just before the .save() function.  Is there a preferred approach for debugging this type of situation?

Thanks
Derek


# models
class ClientFile(Model):
    id = AutoField(primary_key=True)
    description = CharField(unique=True, max_length=255)
    file = FileField(upload_to='client', storage=CLIENT_STORAGE)


class ClientData(Model):
    id = AutoField(primary_key=True)
    clientfile = ForeignKey(ClientFile)
    data_set = DictionaryField(blank=True, null=True)


# forms
class ClientDataForm(ModelForm):

    class Meta:
        abstract = True
        model = ClientData
        widgets = {
            'data_set': HiddenInput(),
            'clientfile': HiddenInput()
        }


class ClientDataFormLayout(ClientDataForm):
    label = CharField(max_length=255)
    top_left = CharField(max_length=255)
    bottom_right = CharField(max_length=255)

    def __init__(self, *args, **kwargs):
        super(ClientDataForm, self).__init__(*args, **kwargs)

    class Meta(ClientDataForm.Meta):
        pass

    def clean(self):
        cleaned = self.cleaned_data
        # copy for alteration
        cleaned_data = copy.deepcopy(self.cleaned_data)
        del cleaned_data['data_set']
        del cleaned_data['clientfile']
        cleaned['data_set'] = cleaned_data
        return cleaned


# view
    client_dataform_layout = ClientDataFormLayout()
    client = get_object_or_404(ClientFile, pk=1)
    initial = {'clientfile': client}
    if request.method == 'POST':
        formset = client_dataform_layout(request.POST, instance=client)
        if formset.is_valid():
            results = formset.save(commit=False)
            form_data = formset.cleaned_data  #  test only
            formset.save()
    else:
        formset = client_dataform_layout(instance=client, initial=initial)
    return render_to_response("clients/client_data.html",
                              locals(),
                              RequestContext(request))


# test code
formset = ClientDataFormLayout(data {'clientfile': 1, 'data_set': '',
                                     'top_left': 'AA', 'bottom_right': 'ZZ',
                                     'label': 'foobar'})
print formset.is_valid()
results = formset.save(commit=False)
formset.save()
cd = ClientData.objects.get(clientfile__pk=1)
print cd.data_set

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/6CvDRwBfrCAJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment