On Nov 26, 4:25 pm, fiedzia <[EMAIL PROTECTED]> wrote:
> 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?
what you could do, is pass as a parameter to your _init_ method in
your form as:
>
> 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 )
>
here you can pass an extra arg, your user, so this can be added from
the view
def __init__(self, user, *args, **kwargs):
> super(SongEditForm, self).__init__( *args, **kwargs)
> self.fields['author'].required = False
> self.fields['category'].required = False
but you could use the ModelAdmin save method, wich has as a argument
the request object
class ArticleAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
>
> 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
For all of this behavior, i think you could probably check at the
Admin Forms implementation
you have the choice list and also you could add a new instance.
> --
> Maciej Dziardziel
Hope this help :)
Best Regards,
Sergio Hinojosa
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---