Connecting to a custom model in Django

A quick (maybe stupid) question. This is the flow of my site: the user logs in and is redirected to the user’s admin page. On this admin page, they have the opportunity to make a "Profile". I want to associate the profile that they create with the User data, so that 1 User is associated with 1 profile.

For some reason, the following doesn't work (just trying to tie

UserAdmin.Models

from django.db import models from django.contrib.auth.models import User class Profile(models.Model): username = models.ForeignKey(User) firstname = models.CharField(max_length=200) lastname = models.CharField(max_length=200) email = models.EmailField(max_length=200) def __unicode__(self): return self.username 

UserAdmin.Views

 def createprofile(request): user = User.objects.get(id=1) profile = Profile(username=user, firstname='Joe', lastname='Soe', email=' Joe@Soe.com ') profile.save() 

I keep getting: the useradmin_profile table does not have a column named username_id

Any ideas? Appreciated.

EDIT:

I deleted my db and started a new syncdb, changed to username = models.OneToOneField (User). Now I get I can not assign "u'superuser": "Profile.username" must be an instance of "User".

+7
source share
2 answers

UserAdmin.Models

 from django.db import models from django.contrib.auth.models import User class Profile(models.Model): user = models.OneToOneField(User) def __unicode__(self): return self.user.get_full_name() 

UserAdmin.Views

 def createprofile(request): user_ = User.objects.get(pk=1) profile = Profile(user=user_) profile.user.first_name = 'Joe' profile.user.last_name = 'Soe' profile.user.email = ' Joe@Soe.com ' profile.user.save() profile.save() 
+9
source

You synchronized the profile model before you got the username ForeignKey field. Django will create the tables, but will not modify them once they are created. Here is the answer that lists your options: https://stackoverflow.com/a/212960/

And you should consider renaming username to user .

+2
source

All Articles