Hi Daniel Thank you for the tip. Now it works :-)
class CategoryItemForm(ModelForm): class Meta: model = CategoryItem exclude = ('position','defaultItem') #These two attributes shall only be changed in list view def clean(self): """ Checks whether the title is unique for all items associated with the current Category. """ cleaned_data = self.cleaned_data query = CategoryItem.all().ancestor(self.instance.parent()) categoryItems = query.fetch(1000) myTitle = cleaned_data.get('title') for item in categoryItems: if item.title == myTitle: raise ValidationError("The title must be unique, but it " "is already used for another category item.") return cleaned_data On Nov 26, 10:34 am, Daniel Roseman <[EMAIL PROTECTED]> wrote: > On Nov 26, 8:07 am,vanosten<[EMAIL PROTECTED]> wrote: > > > Hi > > > I need to do custom validation to make sure that a new record does not > > pick the same title as a previous record. Therefore in my overriden > > clean method in my custom Form (which extends ModelForm) I need to be > > able to do either of the following: > > # access the session, where the parent object is saved > > # access the POST request object to access session, to do the same as > > above > > # access the model instance, to get to the parent > > > How can I do this? > > You can access the model instance via form.instance. > > To get access to the request, you'll need to pass it in to the form > somehow. The best way is probably to override the form's __init__ > method to add a request parameter and save it as self.request - you > can then pick it up in the clean method. > > However it's not clear why you need to access the session - there's no > need to do if you just want to do model queries. > -- > DR. --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---