I have several form which are contained in different views and template. But these fields are combined into one main model - Profile. For example, instead of choosing a name of the work, I can write a name my work myself.
I built it with several form in my three template. It works well. But if I submit form in one template, the information contains in other templates disappear.
models
class Profile(models.Model):
slug = models.SlugField(unique=True)
user = models.OneToOneField(get_user_model(), on_delete=models.CASCADE)
bio = models.TextField(null=False, blank=True)
# Location
country = models.ForeignKey(
"Country", on_delete=models.SET_NULL, null=True, blank=True
)
city = ChainedForeignKey(
"City",
chained_field="country",
chained_model_field="country",
show_all=False,
auto_choose=True,
sort=True,
null=True,
blank=True)
bachelordeg = models.ManyToManyField(
"University",
blank=True,
related_name="bachelordeg",
)
masterdeg = models.ManyToManyField(
"University",
blank=True,
related_name="masterdeg",
)
work = models.ManyToManyField("Work_place", blank=True)
school = models.ForeignKey(
"School", on_delete=models.SET_NULL, null=True, blank=True
)
class University(models.Model):
slug = models.SlugField(unique=True, blank=True, null=True)
name = models.CharField(max_length=250, blank=True, null=True)
description = models.TextField(null=False, blank=True)
class Work_place(models.Model):
name = models.CharField(max_length=250, blank=True, null=True)
description = models.TextField(null=False, blank=True)
views
def update_profile_about(request, slug):
if request.method == "POST":
user_form = UserForm(
request.POST, instance=request.user, prefix="user")
profile_form = ProfileForm(
request.POST, request.FILES, instance=request.user.profile, prefix="profile"
)
if all([user_form.is_valid(), profile_form.is_valid()]):
us = user_form.save()
prof = profile_form.save()
prof.save()
messages.success(request, _(
"Your profile was successfully updated!"))
return HttpResponseRedirect("about")
else:
messages.error(request, _("Please correct the error below."))
else:
user_form = UserForm(instance=request.user, prefix="user")
profile_form = ProfileForm(
instance=request.user.profile, prefix="profile")
return render(
request,
"users/profile_update_about.html",
{
"user_form": user_form,
"profile_form": profile_form,
},
)
def update_profile_bio(request, slug):
if request.method == "POST":
profile_form = ProfileForm(
request.POST, request.FILES, instance=request.user.profile, prefix="profile"
)
if profile_form.is_valid():
prof = profile_form.save()
prof.save()
messages.success(request, _(
"Your profile was successfully updated!"))
return HttpResponseRedirect("bio")
else:
messages.error(request, _("Please correct the error below."))
else:
profile_form = ProfileForm(
instance=request.user.profile, prefix="profile")
return render(
request,
"users/profile_update_bio.html",
{"profile_form": profile_form},
)
def update_profile_work_uni(request, slug):
WorkFormSet = modelformset_factory(
Work_place, form=WorkForm, extra=0, can_delete=True
)
if request.method == "POST":
profile_form = ProfileForm(
request.POST, instance=request.user.profile, prefix="profile"
)
work_formset = WorkFormSet(request.POST, prefix="work")
if all([profile_form.is_valid(), work_formset.is_valid()]):
prof = profile_form.save()
works = work_formset.save()
# sch = school_formset.save()
prof.Work_place = works
prof.save()
messages.success(request, _(
"Your profile was successfully updated!"))
return HttpResponseRedirect("work_uni")
else:
messages.error(request, _("Please correct the error below."))
else:
work_formset = WorkFormSet(prefix="work")
profile_form = ProfileForm(
instance=request.user.profile, prefix="profile")
context = {"profile_form": profile_form, "work_formset": work_formset}
return render(request, "users/profile_work_uni.html", context)