Hi I am trying to achieve the following effect:
A ModelForm containing list of choices and input to possibly add another element to the list is presented on page. If user will not choose any item from list and will type something into new_item field, then: - if new_item is present on the list, ValidationError should be raised and user should choose item from list instead of creating new one, but i also want to make this item selected on the list (problem 1) - if new_item is not on the list, then i want to add it to the list and set as model instance property (problem 2) problem 1: i don't know how to modify modelform during or before/after validation. problem 2: basically the same and there is also problem 3: i want access request.user during validation. Is it possible? Thanks for help my model: class Song(models.Model): """ Song """ title = models.CharField(max_length = 500, blank = False) user = models.ForeignKey(User) author = models.ForeignKey(Author) category = models.ForeignKey(Category) content = models.TextField(blank = True) format = models.CharField(max_length = 50, choices = SONG_FORMATS) creationdate = models.DateTimeField(auto_now_add = True) my form: class SongEditForm(ModelForm): class Meta: model = songs_models.Song exclude = ('user') new_author = forms.CharField(max_length=100, required = False ) new_category = forms.CharField(max_length=100, required = False ) def __init__(self, *args, **kwargs): super(SongEditForm, self).__init__( *args, **kwargs) self.fields['author'].required = False self.fields['category'].required = False def clean(self): #FIXME: check user validation cleaned_data = self.cleaned_data if not cleaned_data['author'] and not cleaned_data ['new_author']: raise forms.ValidationError('Fill either author or new author') if not cleaned_data['author'] and cleaned_data['new_author']: authors = songs_models.Author.objects.filter(name = cleaned_data['new_author']) if len(authors) > 0: #TODO: select existing author #self.data.author = authors[0] #doesn't work #self.data.new_author = u'test' #doesn't work raise forms.ValidationError('author with that name already exists, so choose him/her instead of creating new one') else: #create author #self.author = songs_models.Author() #self.author.name = cleaned_data['new_author'] pass return cleaned_data -- Maciej Dziardziel --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---