I have been learning django for like 1 month now and i felt the best way is to do a project. i settled on a simple school management project but I am kind of stacked. This project has different categories of users i.e students and administrator. the school administrator should log in and add new students, the students should also log in and may be edit their profile. I created a nother app UserProfile with User as foreignKey and category
class Category(models.Model):
name=models.CharField(max_length=200)
def __unicode__(self):
return self.name
class UserProfile(models.Model):
user=models.OneToOneField(User)
category=models.ForeignKey(Category)
def __unicode__(self):
return "%s "%(self.category.name)
I added sample users from the admin site and am so far able to redirect the school administrator to his frontend dashboard where he should be adding new student. the students also are redirected to the students page upon login. i have a common login form. my first question is, should i have user as foreigkey in the student model like below?
class Student(models.Model):
user=models.OneToOneField(User)
regNum=models.CharField(max_length=200, unique=True)
lastName=models.CharField(max_length=200)
otherNames=models.CharField(max_length=200)
dateOfBirth=models.DateField()
idNo=models.IntegerField(default=123456)
email=models.EmailField()
contact=models.IntegerField(default=071234567)
HomeTown=models.CharField(max_length=200)
You will need to create a User instance and add it to the student together with student details.
As described here :
And here: Creating users django 1.8
Example:
from django.contrib.auth.models import User
#place this after your form is valid
form=StudentForm(request.POST)
if form.is_valid():
#create student
username=form.cleaned_data['username']
email=form.cleaned_data['email']
password=form.cleaned_data['password']
user=User.objects.create_user(username=username,email=email,password=password)
#create your userprofile here
............
............
#using the user instance now create your student
student=Student.objects.create(user=user,regNum=''......)
.............
..............