0

I'am developing a website in Python Django. I have two "class CustomUser" it is used in order to login and another "class coach", this class contains information about users. I need to match these two class. My website identify the user who is logged in but it fail to assign this user a coach class. When I try to submit my form I receive the error "Cannot assign "'prof'": "coach.user" must be a "CustomUser" instance." I'am a beginner in Python Django. Can you help me to resolve this problem ?

This is Site/models.py

class coach(models.Model):
    user = models.OneToOneField(CustomUser,on_delete=models.CASCADE)
    Telephone = models.IntegerField()
    Level = models.TextField()
    Study = models.TextField()

This is my users/models.py

from django.contrib.auth.models import AbstractUser, UserManager
from django.db import models

class CustomUserManager(UserManager):
    pass

class CustomUser(AbstractUser):
    objects = CustomUserManager()

This is my Site/views.py there I have a function which find the user and assign him a coach class.

def Register(request):
    form = ContactForm(request.POST or None, request.FILES)
    if form.is_valid():
        obj = form.save(commit=False)
        obj.user = request.user.username
        obj.save()
        form.save()
    return render(request, 'Register.html', {'form': form})
Nikita
  • 75
  • 1
  • 10
  • You are assigning the `username` instead of the `user`. You need `obj.user = request.user` instead of `request.user.username`. And remove the `form.save()` in the end, you're already saving the object in the previous line. – Paulo Almeida Mar 18 '18 at 21:17
  • Thank you for your help ! I think it resolved a big part of my problem. I do not have anymore the error "cannot assign...." it work the first time when the user put his info. but then if the user want to update his information trough the form it receive an error "UNIQUE constraint failed: Site_coach.user_id" – Nikita Mar 18 '18 at 21:38
  • That's an entirely different question (please open a new one) that needs more information, like what are the constraints and what view is running. – Paulo Almeida Mar 18 '18 at 21:41

0 Answers0