Hi,

Profile(newuser.pk,customerid=form1.cleaned_data['customer'])

should be:
 Profile(user_id=newuser.pk,customerid=form1.cleaned_data['customer'])

user is a ForeignKey, so you assign user=<User: ...> after performing a 
get(), or you use the form above with your pk - i.e. 'the user record whose 
id field (primary key) is ..."

On Wednesday, December 12, 2018 at 4:55:57 PM UTC-5, progm...@gmail.com 
wrote:
>
> Hi, I'm trying to save a new user and their profile information in same 
> form and view. It is almost working, but cant get by this error.
>
> [image: errmsg.png]
>
> I know I have values available because I print them just before saving 
> from my views.py, which is:
>
>
> def addUser(request):
>
>     template_name = 'mysite/admin_adduser.html'
>
>     
>
>     # Post
>
>     if request.method == 'POST':
>
>
>         # Form clear was requested 
>
>         if "clearform" in request.POST: 
>
>             print("clearform request")
>
>             return redirect('addusers')
>
>         elif "cancel" in request.POST:
>
>             print("cancel request")
>
>             return redirect('adminhome') 
>
>
>         # Data being posted   
>
>         else:
>
>             form1 = frmAddUser(request.POST)
>
>             if form1.is_valid():
>
>                 newuser = form1.save()
>
>                 print(newuser.pk)                      
>  //*************************This is where I verify that I actually have 
> data*********************************//
>
>                 print(form1.cleaned_data['customer']) 
>
>                 newprofile = Profile(newuser.pk
> ,customerid=form1.cleaned_data['customer'])
>
>                 newprofile.save()
>
>                 return redirect('addusers')
>
>             else:
>
>                 # Form is not valid, give opportunity to fix
>
>                 return render(request, template_name, {"form1":form1,})
>
>
> Here is my Profile model (Its very simple for testing):
>
>
> # User Profile
>
> class Profile(models.Model):
>
>     user = models.OneToOneField(User, on_delete=models.CASCADE)
>
>     customerid = models.IntegerField(blank=True,null=True,default=0)
>
>     # customerid = models.CharField(max_length=254, blank=True)
>
>
> # Create the Profile record when a new user is created by
>
>     #hooking the create_user method  
>
>   
>
> @receiver(post_save, sender=User)                                          
>            //*****************************************I am curious if this 
> hook is causing a problem, but what are your 
> thoughts**********************************//
>
> def create_user_profile(sender, instance, created, **kwargs):
>
>     if created:
>
>         Profile.objects.create(user=instance)
>
>
> So, I know I have data, but whaen I save the profile the dataset must have 
> a null in it.
>
> Any thoughts?
>
>
> Thanks
>
>
>
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/f580c886-bb6f-470b-9907-6c70c680fb5e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to