Home > Net >  how to add a profile object by using objects.create method
how to add a profile object by using objects.create method

Time:12-03

simply my error is this

Exception has occurred: TypeError User() got an unexpected keyword argument 'User'

here is the data I receive from the post request in view.py

if request.method == "POST":
    student_surname = request.POST.get('student_surname')
    student_initials = request.POST.get('student_initials')
    student_birthday = request.POST.get('student_birthday')
    student_username = request.POST.get('student_username')
    student_email = request.POST.get('student_email')
    student_entrance = request.POST.get('student_entrance')
    student_contact = request.POST.get('student_contact')
    student_residence = request.POST.get('student_residence')
    student_father = request.POST.get('student_father')
    student_other_skills = request.POST.get('student_skills')
    student_sports = request.POST.get('student_sports')
    student_password = request.POST.get('student_password')

I can create user object it's working in view.py

user = User.objects.create_user(
        username=student_username,
        email=student_email,
        password=student_password
    )

some data is related to profile in view.py

    student_profile = User.objects.create(
        User=user, #Error line
        surname=student_surname,
        initials=student_initials,
        entrance_number=student_entrance,
        email=student_email,
        father=student_father,
        skills=student_other_skills,
        sports=student_sports,
        birthday=student_birthday,
        contact=student_contact,
        address=student_residence,
    )

    student_profile.save()

profile definition in models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    surname = models.CharField(max_length=50)
    initials = models.CharField(max_length=10, blank=True)
    entrance_number = models.CharField(max_length=10, blank=True)
    email = models.EmailField(max_length=254, blank=True)
    father = models.CharField(max_length=50, blank=True)
    skills = models.CharField(max_length=50, blank=True)
    sports = models.CharField(max_length=50, blank=True)
    birthday = models.DateField(null=True, blank=True)
    contact = models.CharField(max_length=100, null=True, blank=True)
    address = models.CharField(max_length=100, null=True, blank=True)


    # other fields here
    def __str__(self):
        return self.user.username

I believe the error is in User = user line can somebody tell me how to initialize this profile object correctly AND save record in the database at the time of creating the user.

CodePudding user response:

student_profile = Profile.objects.create(  # Profile
        user=user, #user
        surname=student_surname,
        initials=student_initials,
        entrance_number=student_entrance,
        email=student_email,
        father=student_father,
        skills=student_other_skills,
        sports=student_sports,
        birthday=student_birthday,
        contact=student_contact,
        address=student_residence,
    )

Not User model, must be Profile model, your model field is user, but you have used User

  • Related