2👍
The error is exactly what it says it is.
In order to create the new project you need to give your project object a user profile object, not a user profile id.
user = User.objects.get(id=7)
userprofile = user.get_profile()
project.client = userprofile
Notice that it is userprofile
object that is assigned to the project instance’s client attribute. You cannot assign the userprofile
object’s id to the project instance’s client attribute.
Also, do not confuse your user id with your userprofile id. They may not be exactly the same.
The user id is the primary key auto-generated in the auth_user table whenever a new user is created in your database.
The userprofile id is the primary key auto-generated in the profiles_userprofile table whenever a corresponding user profile is created that is related to a user.
In other words, your add_project
view function needs to read something like
if (request.method == "POST"):
user = User.objects.get(username = request.POST.get('rsb_username'))
# userprofile = UserProfile.objects.get(user = user) <-- we don't need this anymore.
new_project = Projects(client = user, # <--- give it a user instance of course now that your model has changed
project_name = request.POST.get('rsb_project_name'),
description = request.POST.get('rsb_description'),
budget = request.POST.get('rsb_budget'),
time_frame = request.POST.get('rsb_time_frame'),
time_frame_units = request.POST.get('rsb_time_frame_units'),
contact = request.POST.get('rsb_point_of_contact'),
contact_email = request.POST.get('rsb_contact_email'),
contact_phone = request.POST.get('rsb_contact_phone'),
)
new_project.save()
The key takeaway is that you need to be sure what your original model definition is.
If in your Project class, your client attribute is assigned as FK to User, then you need to give it a user object.
If in your Project class, your client attribute is assigned as FK to UserProfile, then you need to give it a userprofile object.