Profile() got unexpected keyword arguments: 'id_user'

115 Views Asked by At

I cant seam to figure out why this error occurs when 'user_id' is on the Profile model. Help! The Profile is getting a FK from the Django user form and the id_user is on the Profile model. Im following a code along it works fine for the instructor but not for me.

I want to see a profile in the admin portal.

Here is my models

from django.db import models
from django.contrib.auth import get_user_model

User = get_user_model()
# Create your models here.

class Profile(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    id_user = models.IntegerField
    bio = models.TextField(blank=True)
    profileimg = models.ImageField(upload_to='profile_images', default='book-icon.png')
    location = models.CharField(max_length=100, blank=True)

    def __str__(self):
        return self.user.username

here is my views

def signup(request):

    if request.method == 'POST':
        username = request.POST['username']
        email = request.POST['email']
        password = request.POST['password']
        password2 = request.POST['password2']

        if password == password2:
            if User.objects.filter(email=email).exists():
                messages.info(request, 'Email Taken')
                return redirect('signup')
            elif User.objects.filter(username=username).exists():
                messages.info(request, 'Username Taken')
                return redirect('signup')
            else:
                user = User.objects.create_user(username=username, email=email, password=password)
                user.save()

                #log user in and redirect to settings page
               

                #create a Profile object for the new user
                user_model = User.objects.get(username=username)
                new_profile = Profile.objects.create(user=user_model, id_user=user_model.id)
                new_profile.save()
                return redirect('settings')
        else:
            messages.info(request, 'Password Not Matching')
            return redirect('signup')
        
    else:
        return render(request, 'signup.html')

Here are my Urls

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('signup', views.signup, name='signup')
]

1

There are 1 best solutions below

1
Nick Langat On

You forgot to add parenthesis to your field so in your models you have:

id_user = models.IntegerField

Simply change that to:

id_user = models.IntegerField()

Then python manage.py makemigrations && python manage.py migrate. You should be fine onwards.