First question. A one to many relationship between the django User model and some model is allowed right?
Now the main question. I have a couple of models all related to the user model. It just takes in user information and renders the information specifically to the user in the templates. When i tried to submit the form i get a error User has no person. Here is the models and view logic
from django.db import models
from django.contrib.auth.models import User
class Person(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
display_picture = models.ImageField(default='default.png', upload_to='display_pictures')
#change default jpeg#
def __str__(self):
return f'{self.user.username} \'s info'
class Occupation(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE)
occupation = models.CharField(max_length = 100)
def __str__(self):
return f'{self.user.username} \'s occupation'
class Skills(models.Model):
occupation = models.ForeignKey(Occupation, on_delete=models.CASCADE)
skills = models.CharField(max_length = 100, blank=True)
def __str__(self):
return f'{self.occupation} \'s skillset'
class WorkExp(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True)
company = models.CharField(max_length = 200, blank=True)
started = models.IntegerField(null=True, blank=True)
left = models.IntegerField(null=True, blank=True)
position = models.CharField(max_length = 200, blank=True)
def __str__(self):
return f'{self.user.username} \'s work experiences'
class AcadExp(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True)
education = models.CharField(max_length = 30, blank=True)
def __str__(self):
return f'{self.user.username} \'s academic experiences'
class Contact(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, blank=True)
cell = models.IntegerField()
twitter = models.CharField(max_length = 100)
instagram = models.CharField(max_length = 100)
linkedin = models.CharField(max_length=100)
def __str__(self):
return f'{self.user.username} \'s contact information'
views
@login_required
def portfolio_form(request):
SkillsFormSet = modelformset_factory(Skills, form=SkillsForm)
WorkExpFormSet = modelformset_factory(WorkExp, form=WorkExpForm)
AcadExpFormSet = modelformset_factory(AcadExp, form=AcadExpForm)
if request.method == 'POST':
person_form = PersonForm(request.POST, request.FILES, instance=request.user.person)
occupation_form = OccupationForm(request.POST, instance=request.user.occupation)
skillset = SkillsFormSet(request.POST, prefix='skills', queryset=Skills.objects.filter(occupation_id=request.user.occupation.id))
workexpset = WorkExpFormSet(request.POST, prefix='work-exp', queryset=WorkExp.objects.filter(user=request.user).last())
acadexpset = AcadExpFormSet(request.POST, prefix='acad-exp', queryset=AcadExp.objects.filter(user=request.user).last())
contact_forms = ContactForm(request.POST, instance=Contact.objects.filter(user=request.user).last())
if person_form.is_valid() and occupation_form.is_valid() and skillset.is_valid() and workexpset.is_valid() and acadexpset.is_valid() and contact_form.is_valid():
person_form.save()
occupation_form.save()
instances = skillset.save(commit=false)
for instance in instances:
instance.save()
instances = workexpset.save(commit=false)
for instance in instances:
instance.save()
instances = acadexpset.save(commit=false)
for instance in instances:
instance.save()
contact_form.save()
else:
person_form = PersonForm()
occupation_form = OccupationForm()
skillset = SkillsFormSet(prefix='skill',queryset=Skills.objects.none())
workexpset = WorkExpFormSet(prefix='workexp', queryset=WorkExp.objects.none())
acadexpset = AcadExpFormSet(prefix='acadexp', queryset=AcadExp.objects.none())
contact_form = ContactForm()
context = {
'person_form' : person_form,
'occupation_form' : occupation_form,
'skillset' : skillset,
'workexpset' : workexpset,
'acadexpset' : acadexpset,
'contact_form' : contact_form
}
return render(request, 'portfolio/portfolio_form.html', context)