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 beresults = formset.save(commit=False)
results.save()Tervitades/RegardsKarl Sutt
On Thu, Jul 26, 2012 at 10:35 AM, Derek wrote:I'd appreciate some help with the following problem, under Django 1.4When 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?ThanksDerek# modelsclass 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)# formsclass ClientDataForm(ModelForm):class Meta:abstract = Truemodel = ClientDatawidgets = {'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):passdef clean(self):cleaned = self.cleaned_data# copy for alterationcleaned_data = copy.deepcopy(self.cleaned_data) del cleaned_data['data_set']del cleaned_data['clientfile']cleaned['data_set'] = cleaned_datareturn cleaned# viewclient_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 onlyformset.save()else:formset = client_dataform_layout(instance=client, initial=initial) return render_to_response("clients/client_data.html", locals(),RequestContext(request))# test codeformset = 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