Answer the question
In order to leave comments, you need to log in
How to make a userprofile in django?
Hello! There are models:
class UserProfile(models.Model):
user = models.OneToOneField(User)
class Project(models.Model):
title = models.CharField(max_length=200, verbose_name=u'Название')
slug = models.SlugField(unique=True, verbose_name=u'URL')
user = models.ForeignKey(UserProfile, verbose_name=u'Пользователь')
class ProjectCreate(CreateView):
model = Project
template_name = 'add_project.html'
form_class = AddProjectForm
success_url = '/project/success'
def form_valid(self, form):
form.instance.user = self.request.user
return super(ProjectCreate, self).form_valid(form)
Cannot assign "<SimpleLazyObject: <User: galkinav>>": "Project.user" must be a "UserProfile" instance.
Answer the question
In order to leave comments, you need to log in
The process of creating a profile is elementary.
profile = UserProfile(user=request.user)
profile.save()
@receiver(post_save, sender=User, dispatch_uid="create_profile")
def update_profile(sender, instance, **kwargs):
if kwargs["created"]:
UserProfile.objects.create(user=instance)
class SomeAuthBackend(ModelBackend):
def get_user(self, user_id):
try:
user = User.objects.select_related('profile_set').get(pk=user_id)
try:
user.profile_set
except UserProfile.DoesNotExist:
UserProfile.objects.create(user=user)
return user
except User.DoesNotExist:
return None
admin.site.unregister(User)
class UserProfileInline(admin.StackedInline):
model = UserProfile
@admin.register(User)
class UserProfileAdmin(UserAdmin):
inlines = [ UserProfileInline ]
def save_model(self, request, obj, form, change):
super(UserProfileAdmin, self).save_model(request, obj, form, change)
if not change:
UserProfile.objects.create(user=obj)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question