[Answer]-Django – multiple profiles

1πŸ‘

βœ…

In my opinion it isn’t a good approach.

I would recommend doing 1 unified profile which will contain an option:
user_type = models.CharField(choices=[your_choices], max_length=4)

Then in models you would create two forms – 1 for teacher and 1 for student.

class ProfileFOrm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(BaseProfileForm, self).__init__(*args, **kwargs)
        for name in self.fields:
            self.fields[name].required = True

class TeacherProfile(ProfileForm):
    class Meta:
        model = Profile
        fields = ('your_fields')


class StudentProfile(ProfileForm):
    class Meta:
        model = Profile
        fields = ('school')

That’s just my idea for that πŸ™‚


Edited

Profile edition:

view.

def profile(request):
p = get_objects_or_404(ProfileModel, user=request.user)
return TemplateResponse(request, 'template.html', {'profile': p})

In models we need a function to check if user is a student or a teacher, so:

class Profile(models.Model):
    ... your fields here...
    def get_student(self):
        return self.user_type == 1

In templates:

{% if profile.get_student%}

>>>>get all data for students ex: <<<<
{{profile.name}}

{% endif %}


{% if profile.get_teacher %}
....
πŸ‘€Efrin

Leave a comment