Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 907077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:29:38+00:00 2026-05-15T16:29:38+00:00

In my application I have an extended User model called UserProfile. This user can

  • 0

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))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:29:38+00:00Added an answer on May 15, 2026 at 4:29 pm
    try:
        user = UserProfile.objects.get(pid=self.cleaned_data['pid'])
    except user.DoesNotExist:
        ...
    

    It’s because you try to get UserProfile object but check for user object DoesNotExists, not UserProfile.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C++ Windows application that can be extended by writing C++ plugins,
In my current application I have a form that requires the user to enter
In my iPhone application I have a table view. When user taps any row,
I have an game application I have written for Windows Mobile and I want
We have a Hibernate/Spring application that have the following Spring beans: <bean id=transactionManager class=org.springframework.orm.hibernate3.HibernateTransactionManager
In my application I have TextBox in a FormView bound to a LinqDataSource like
In my application I have a DataGridView control that displays data for the selected
In my application I have a window which I popup with small messages on
In my application I have a Customer class and an Address class. The Customer
In my VC++ application I have an embedded browser (MSHTML). It works fine and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.