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

  • SEARCH
  • Home
  • 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 7954017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:17:55+00:00 2026-06-04T03:17:55+00:00

I am trying to extend userprofile with column from another model as Foreign key.

  • 0

I am trying to extend userprofile with column from another model as Foreign key. But, while saving its throwing error message.

Models.py:

class Team_Profile(models.Model):
    team_name = models.CharField(max_length=200, unique=True)
    
    
class UserProfile(models.Model):
    user = models.OneToOneField(User)
    team_name = models.ForeignKey(Team_Profile)


    def __str__(self):
        return "%s's profile" % self.user

    def save(self, *args, **kwargs):
        try:
            existing = UserProfile.objects.get(user=self.user)
            self.id = existing.id
        except UserProfile.DoesNotExist:
            pass
        models.Model.save(self, *args, **kwargs)

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        created = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)

Below is my views.py:

Views.py:

data = request.POST.copy()
team = data['team_hidden']
try:
            team_name = Team_Profile.objects.get(team_name=team)
except Team_Profile.DoesNotExist:
            team_profile = Team_Profile()
            team_profile.team_name = team
            team_profile.save()
                
try:
            user=User.objects.get(username=name)
            return render_to_response("user_exist.html", {'media_url':media_url})
            
except User.DoesNotExist:

            user= User.objects.create_user(name,User.email,User.password)
            user.first_name=first_name
            user.last_name=last_name
            user.save()

                        
            userprofile = UserProfile()
            userprofile.user = user
            userprofile.team_name = Team_Profile.objects.get(team_name=team)                    


            userprofile.save()

Let me know if you need more info. Any help is appreciated.

Traceback:

Traceback:

File "/home/dtechsun/testapp/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/dtechsun/testapp/myentrancetest/views.py" in create_login
  796.          user= User.objects.create_user(name,User.email,User.password)
File "/home/dtechsun/testapp/django/contrib/auth/models.py" in create_user
  135.         user.save(using=self._db)
File "/home/dtechsun/testapp/django/db/models/base.py" in save
  460.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/dtechsun/testapp/django/db/models/base.py" in save_base
  592.                 created=created, raw=raw, using=using)
File "/home/dtechsun/testapp/django/dispatch/dispatcher.py" in send
  172.             response = receiver(signal=self, sender=sender, **named)
File "/home/dtechsun/testapp/myentrancetest/models.py" in create_user_profile
  53.         created = UserProfile.objects.get_or_create(user=instance)
File "/home/dtechsun/testapp/django/db/models/manager.py" in get_or_create
  135.         return self.get_query_set().get_or_create(**kwargs)
File "/home/dtechsun/testapp/django/db/models/query.py" in get_or_create
  387.                 obj.save(force_insert=True, using=self.db)
File "/home/dtechsun/testapp/myentrancetest/models.py" in save
  49.       models.Model.save(self, *args, **kwargs)
File "/home/dtechsun/testapp/django/db/models/base.py" in save
  460.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/dtechsun/testapp/django/db/models/base.py" in save_base
  571.                     result = manager._insert(values, return_id=update_pk, using=using)
File "/home/dtechsun/testapp/django/db/models/manager.py" in _insert
  195.         return insert_query(self.model, values, **kwargs)
File "/home/dtechsun/testapp/django/db/models/query.py" in insert_query
  1437.     return query.get_compiler(using=using).execute_sql(return_id)
File "/home/dtechsun/testapp/dbindexer/compiler.py" in execute_sql
  256.         return super(SQLInsertCompiler, self).execute_sql(return_id=return_id)
File "/home/dtechsun/testapp/djangotoolbox/db/basecompiler.py" in execute_sql
  348.                                         "field) to None!" % field.name)

Exception Type: DatabaseError at /create_login/
Exception Value: You can't set team_name (a non-nullable field) to None!

Thanks,

Sunil

  • 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-06-04T03:17:56+00:00Added an answer on June 4, 2026 at 3:17 am

    Adding default for team_name worked fine. Thanks everyone for your help.

    class UserProfile(models.Model): 
            user = models.OneToOneField(User)   
            team_name = models.ForeignKey(Team_Profile, default=0) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to extend a model for a custom extension. Simple enough...but rewrite doesnt seem
I'm trying to extend JButton with Clojure, but I ran into a problem when
I'm trying to extend a form and add an upload file field from within
I'm trying to extend the SPL ArrayObject but I've hit a little snag. Using
I'm trying to extend jWysiwyg with an function to add a map from Google
I'm trying to extend the functions from a Random class. public static class RandomExtensions
I trying to extend UIButton but I keep getting a EXC_BAD_ACCESS, inside the initializer
I'm trying to extend the TextBox , ComboBox and Panel controls using IExtenderProvider but
I was trying to extend user profile. I founded a few solutions, but the
Simply trying to extend View and do some custom work, but Eclipse will complain

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.