views:

32

answers:

3

Hi, How could we make the django form to not validate if we are editing, not adding a new record. The code as following :

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

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'.

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?

Thanks a lot!

A: 

in your clean_name function exclude the current object from queryset

query.filter('name = ', name).exclude(pk=self.pk)

or change the if condition to check that page and current object are not the same.

Ashok
A: 

If you are editing form, then the form has some instance, and you can check if that exists.

If it does, then you are probably editing existing object.. right?

Zayatzz
A: 

Hi, Sorry, I couldn't comment below your guys post, don't know why.

@sunn0 : I didn't use django models, coz deploy the app in appengine, so use appengine model instead.

@Zayatzz : May you show a little code how to do it? Since whether we are adding or editing, we always bound the form to request.POST before validation, so don't know how to differentiate.

@Ashok : I made a workaround based on your suggestion. Since previously I didn't pass the pk to form, but passing the prev object as param instead, so couldn't exclude by using pk. So, I change the code and put additional key as pk (if create, let key empty, but if edit fill key with pk) and just check in if condition, if key field not empty, then it means we are editing. Not sure if it is best practice, but it works anyway.

hudarsono