views:

19

answers:

1

I'm using django-bookmarks for one of my projects and recently updated to Django 1.2.1. I noticed that the form submit does not validate since the update. Please note that I did confirm this is working with Django v1.1.1, so the new version and form field validation is different.

This is the model that forms.py is building off of:

class BookmarkInstance(models.Model):

    bookmark = models.ForeignKey(Bookmark, related_name="saved_instances", verbose_name=_('bookmark'))
    user = models.ForeignKey(User, related_name="saved_bookmarks", verbose_name=_('user'))
    saved = models.DateTimeField(_('saved'), default=datetime.now)

    description = models.CharField(_('description'), max_length=100)
    note = models.TextField(_('note'), blank=True)

The view handling the POST:

if request.method == "POST":
    bookmark_form = BookmarkInstanceForm(request.user, request.POST)
    if bookmark_form.is_valid():
        bookmark_instance = bookmark_form.save(commit=False)
        bookmark_instance.user = request.user
        bookmark_instance.save()
        bookmark = bookmark_instance.bookmark
        ...

And the forms.py:

class BookmarkInstanceForm(forms.ModelForm):

    url = forms.URLField(label = "URL", verify_exists=True, widget=forms.TextInput(attrs={"size": 40}))
    description = forms.CharField(max_length=100, widget=forms.TextInput(attrs={"size": 40}))
    redirect = forms.BooleanField(label="Redirect", required=False)
    tags = TagField(label="Tags", required=False)

    def __init__(self, user=None, *args, **kwargs):
        self.user = user
        super(BookmarkInstanceForm, self).__init__(*args, **kwargs)
        # hack to order fields
        self.fields.keyOrder = ['url', 'description', 'note', 'redirect']

    def clean(self):
        if 'url' not in self.cleaned_data:
            return
        if BookmarkInstance.objects.filter(bookmark__url=self.cleaned_data['url'], user=self.user).count() > 0:
        raise forms.ValidationError(_("You have already bookmarked this link."))
        return self.cleaned_data

    ...    

    def save(self, commit=True):
        self.instance.url = self.cleaned_data['url']
        return super(BookmarkInstanceForm, self).save(commit)

    class Meta:
        model = BookmarkInstance
        #fields = ('url', 'description', 'note', 'redirect')

Using pdb, I saw that the foreign key values for BookmarkInstance were empty, therefor is_valid() would return false since all the fields didn't validate.

Does the init function need to be modified to pass in the foreign key parameters explicitly? Shouldn't the foreign key values be passed in automatically like it was doing in v1.1.1?

A: 

I actually just solved it and it was embarrassingly easy. Just uncomment the:

#fields = ('url', 'description', 'note', 'redirect')

as it needs the fields to be explicitly stated. This worked with Django v1.1.1 and I guess the new form validation needs it explicitly stated in v1.2.1.

twampss