Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to validate django form only when adding not editing
    primarykey
    data
    text
    <p>How could we make the django form to not validate if we are editing, not adding a new record. The code as following :</p> <pre><code>class PageForm(forms.Form): name = forms.CharField(max_length=100,widget=forms.TextInput(attrs={'class':'textInput'})) description = forms.CharField(max_length=300, required=False,widget=forms.TextInput(attrs={'class':'textInput'})) body = forms.CharField(widget=forms.Textarea) template = forms.CharField(max_length=30,widget=forms.TextInput(attrs={'class':'textInput'})) navbar = forms.BooleanField(required=False, widget=forms.Select(choices=(('True','True'), ('False', 'False')))) publish = forms.BooleanField(widget=forms.Select(choices=(('Published','Publish Now'), ('Private','Private'), ('Draft','Draft')))) def save(self, page=None, commit=True): data = self.cleaned_data if not page: page = models.Page(key_name=data['name'].replace(' ','-')) page.name = data['name'] page.description = data['description'] page.body = data['body'] page.template = data['template'] page.publish = data['publish'] if commit: page.put() return page # prevent the same page 's name def clean_name(self): name = self.cleaned_data['name'] query = models.Page.all(keys_only=True) query.filter('name = ', name) page = query.get() if page: raise forms.ValidationError('Page name "%s" was already used before' % name) return name </code></pre> <p>The purpose of this name validation is to prevent the records with the same name. BUt i found that, it also validate on edit, so we couldn't edit records, since it will said 'records with same name already exist'. </p> <p>Actually for editing, the page param on save function wont be none, but prev record instead, and wil be none on saving a new one. But how we read this param, on clean_name function so we can now whether it is editing or creating?</p> <p>Thanks a lot!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload