In the end I just did it in the view. Checked for the duplicate before
saving, and sent an error message back to the form if the dupe test
fails. Looks like this. And downside to this approach?
def add_fablist(request):
#
--------------------------------------------------------------------------------------
fablists = FabList.objects.filter(user=request.user) # Get
fablists for left column
duplicate = False
if request.method == 'POST':
form = FabListForm(data=request.POST)
if form.is_valid():
fablist = form.save(commit=False)
if not FabList.objects.filter(user=request.user,
name=
fablist.name): # Check for dupe user/list name combo
fablist.user = request.user
fablist.slug = defaultfilters.slugify(
fablist.name) #
Slugify list name
fablist = form.save()
return HttpResponseRedirect('/')
else:
duplicate = True
else:
form = FabListForm()
return render_to_response('fablist_form.html',
{ 'form' : form,
'fablists' : fablists,
'ADD_FABLIST' : True,
'DUPLICATE' : duplicate
}, context_instance=RequestContext
(request))
On Mar 8, 3:15 pm, Adam Jenkins <
emperorce...@gmail.com> wrote:
> You can override the clean method on the for and have it raise the
> appropriate error for you. I've done the same thing before and came across
> the issue that the form doesn't know the current user. I ended up adding a
> user parameter to the init and passing it when I instantiated the form. Then
> I made my clean_fieldname() method and checked for the record and raised
> ValidationError if it existed.
>
> You can find the docs athttp://
docs.djangoproject.com/en/dev/ref/forms/validation/