In my application I have an extended User model called UserProfile. This user can add up to 4 Friend models. Each added Friend is related 1 to 1 with UserProfile. In my AddFriend form I have a custom cleaning method, for pid field (personal id). This clean_pid should check if the user with given pid is already registered/added as friend and return proper ValidationError. After submitting the form (with no instances in Friend table) I'm getting :
DoesNotExist at /user/add_friend/
UserProfile matching query does not exist.
If there are no users matching sent 'pid', it's value should be returned. Why it's not working ?
Traceback :
File "/home/rails/fandrive/site-packages/django/core/handlers/base.py" in get_response
92. response = callback(request, *callback_args, **callback_kwargs)
File "/home/rails/fandrive/site-packages/django/contrib/auth/decorators.py" in __call__
78. return self.view_func(request, *args, **kwargs)
File "/home/rails/fandrive/accounts/views.py" in add_friend
22. if form.is_valid():
File "/home/rails/fandrive/site-packages/django/forms/forms.py" in is_valid
120. return self.is_bound and not bool(self.errors)
File "/home/rails/fandrive/site-packages/django/forms/forms.py" in _get_errors
111. self.full_clean()
File "/home/rails/fandrive/site-packages/django/forms/forms.py" in full_clean
243. value = getattr(self, 'clean_%s' % name)()
File "/home/rails/fandrive/accounts/forms.py" in clean_pid
48. user = UserProfile.objects.get(pid=self.cleaned_data['pid'])
File "/home/rails/fandrive/site-packages/django/db/models/manager.py" in get
120. return self.get_query_set().get(*args, **kwargs)
File "/home/rails/fandrive/site-packages/django/db/models/query.py" in get
305. % self.model._meta.object_name)
Models :
class InheritedProfile(models.Model):
pid = models.CharField("PID", max_length=11, blank=True, null=True)
(...)
class Meta:
abstract=True
class UserProfile(InheritedProfile):
(...)
user = models.ForeignKey(User, unique=True, related_name='profile')
class Friend(InheritedProfile):
(...)
friend_of = models.ForeignKey(UserProfile, related_name='friend_of')
Forms :
class FriendForm(forms.ModelForm):
pid = forms.RegexField(regex=r'^\d{11}', max_length=11 ,widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=50)))
(...)
class Meta:
model = Friend
exclude = ( 'friend_of', )
def clean_pid(self):
try:
user = UserProfile.objects.get(pid=self.cleaned_data['pid'])
except User.DoesNotExist:
try:
user = Friends.objects.get(pid=self.cleaned_data['pid'])
except Friend.DoesNotExist:
return self.cleaned_data['pid']
raise forms.ValidationError(_(u'User is somebody's friend.'))
raise forms.ValidationError(_(u'User with given PID already registered.'))
def save(self, user, *args, **kwargs):
self.instance.friend_of = user
post = super(FriendForm, self).save(*args, **kwargs)
post.save()
return post
Views (not sure if they're needed):
def add_friend(request):
userprofile = UserProfile.objects.get(user=request.user)
count = 0
if request.method == 'POST':
form = FriendForm(request.POST, request.FILES,)
if form.is_valid():
count = Friend.objects.filter(friend_of=userprofile).count()
if not count > 5:
form.save(user=request.user)
next = reverse('user_profile',)
else:
msg = "You already have 4 friends"
render_to_response('user/data_operations/error.html', {'msg': msg}, context_instance=RequestContext(request))
return HttpResponseRedirect(next)
else:
form = FriendForm()
return render_to_response('user/data_operations/add_friend.html', {
'form':form, 'user':request.user,
}, context_instance=RequestContext(request))