Apparently, every time someone adds a new user to my app they seem to be overwriting their information in the User table with the new person’s. Why?
I don’t use a registration page per se; I have a logged in person ‘registering’ others with all this other information that goes in to User, Profile, Location and Human_notes tables.
I have an input page made of several forms that comprise these several models and here is my view method:
if request.method == 'POST':
user_form = UserForm(request.POST, instance=request.user)
#human_form = HumanForm(request.POST, request.FILES, instance=request.user.profile)
profile_form = ProfileForm(request.POST, request.FILES, instance=request.user.profile)
location_form = LocationForm(request.POST, request.FILES)
human_notes_form = HumanNotesForm(request.POST)
if user_form.is_valid() and profile_form.is_valid() and location_form.is_valid() and human_notes_form.is_valid():
location_form.save()
user_form.save()
usr_curr = User.objects.last() #latest('id')
prf = profile_form.save(commit=False)
prf.user = usr_curr
prf.intaker = request.user
# this is to ID the signed in person who is registering the new user...
loc_curr = Location.objects.last()
loc_curr_id = loc_curr.id
prf.location = loc_curr
profile_form.save()
messages.success(request, f"User Profile data was saved successfully.")
humnote = human_notes_form.save(commit=False)
humnote.intaker = request.user
humnote.user = usr_curr
human_notes_form.save()
messages.success(request, f"Human data was saved successfully.")
return redirect('/intake/humans')
else:
context = {
'user_form': user_form,
'profile_form': profile_form,
'location_form': location_form,
'human_notes_form': human_notes_form,
}
else:
context = {
'user_form': UserForm,
'profile_form': ProfileForm,
'location_form': LocationForm,
'human_notes_form': HumanNotesForm,
}
return render(request, 'intake/human-add.html', context)```
My User model is the default one (I just learned too late I should have done a custom one) and I have a Profile Model that has a OneToOne key to User, and
```class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
intaker = models.ForeignKey(User, default=None, on_delete=models.CASCADE, related_name='profiles', null=True, blank=True,)
location = models.OneToOneField('Location', on_delete=models.CASCADE, null=True, related_name='+')
...
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
def __str__(self):
return "%s %s %s" % (self.id, self.user, self.phn1)```
Thanks for any guidance!