Why is self.instance not set to a pure function for a linked form?

I have a Django 1.1 model with unique_together on the owner and title, where the owner is the foreign key for the user. This restriction applies, but only after cleaning. According to the Django docs , I must have access to self.instance to see the properties of the nonform field object of the model instance.

However, I get an error

'JournalForm' object does not have an 'instance' attribute

Why is self.instance not set in this linked form in the form of clean () or in the clean_title () methods?

My model:

class Journal (models.Model): owner = models.ForeignKey(User, null=True, related_name='journals') title = models.CharField(null=False, max_length=256) published = models.BooleanField(default=False) class Meta: unique_together = ("owner", "title") def __unicode__(self): return self.title 

My form:

 class JournalForm (forms.Form): title = forms.CharField(max_length=256, label=u'Title:') html_input = forms.CharField(widget=TinyMCE(attrs={'cols':'85', 'rows':'40'}, ), label=u'Journal Content:') published = forms.BooleanField(required=False) def clean(self): super(JournalForm, self).clean() instance = self.instance return self.cleaned_input def clean_title(self): title = self.cleaned_data['title'] if self.is_bound: if models.Journal.objects.filter(owner.id=self.instance.owner.id, title=title).exclude(id=self.instance.id).count() > 0: raise forms.ValidationError(u'You already have a Journal with that title. Please change your title so it is unique.') else: if models.LabJournal.objects.filter(owner.id=self.instance.owner.id, title=title).count() > 0: raise forms.ValidationError(u'You already have a Journal with that title. Please change your title so it is unique.') return title 

In accordance with the request - a code of the form:

 def journal (request): try: journal = models.Journal.objects.get(id=id) if request.method == 'GET': if request.user.is_active: if request.user.id == journal.owner.id: data = { 'title' : journal.title, 'html_input' : _journal_fields_to_HTML(journal.id), 'published' : journal.published } form = forms.JournalForm(initial=data) return shortcuts.render_to_response('journal/Journal.html', { 'form':form, }) else: return http.HttpResponseForbidden('<h1>Access denied</h1>') else: return _display_login_form(request) elif request.method == 'POST': if LOGIN_FORM_KEY in request.POST: return _handle_login(request) elif request.user.is_active and request.user.id == journal.owner.id: form = forms.JournalForm(data=request.POST) if form.is_valid(): journal.title = form.cleaned_data['title'] journal.published = form.cleaned_data['title']; journal.save() if _HTML_to_journal_fields(journal, form.cleaned_data['html_input']): html_memo = "Save successful." else: html_memo = "Unable to save Journal." return shortcuts.render_to_response('journal/Journal.html', { 'form':form, 'saved':html_memo}) else: return shortcuts.render_to_response('journal/Journal.html', { 'form':form }) return http.HttpResponseNotAllowed(['GET', 'POST']) except models.Journal.DoesNotExist: return http.HttpResponseNotFound('<h1>Requested journal not found</h1>') 
+1
django django-forms
source share
1 answer

Well, there are a few problems.

Firstly, you are not using ModelForm. The documents you refer to are intended for them, not standard forms.

Secondly, for the form to have an instance attribute, you need to pass that instance when you create the form.

If you use ModelForm, you don’t need code that converts log fields to form fields, and vice versa - save - the form does this for you. You can also remove the clean_title method, which checks for uniqueness, as this is already defined by the unique_together constraint for the model, and ModelForm will provide this for you.

+3
source share

All Articles