Reputation: 763
I am new to Django and would like to know how can I save the teacher's email when the teacher logs in with LoginFormView shown below. After the teacher logs in, he can create a class using CreateClassForm.
I have problem trying to store ClassTeacherTeaches model into the database under CreateClassForm when the teacher creates a new class. How to I retrieve the teacher's email from LoginFormView and store the teacher's email and get the auto incremented class id (this is created once teacher creates a new class) into ClassTeacherTeaches model database?
models.py
class User(AbstractBaseUser):
email = models.EmailField(max_length=100, primary_key=True)
password = models.CharField(max_length=100, null=False)
full_name = models.CharField(max_length=100, default="", null=False)
USERNAME_FIELD = 'email'
def get_full_name(self):
return self.full_name
def set_full_name(self, fname):
self.full_name = fname
def get_short_name(self):
pass
def get_full_name(self):
pass
class Class(models.Model):
classid = models.AutoField(primary_key=True)
class_name = models.CharField(max_length=100, null=False)
class ClassTeacherTeaches(models.Model):
class Meta:
unique_together = (('classid', 'teacher_email'),)
classid = models.ForeignKey(Class)
teacher_email = models.ForeignKey(User)
form.py
class CreateClassForm(forms.Form):
class_name = forms.CharField(widget=forms.widgets.TextInput(attrs={'placeholder': 'Module Code'}), label='')
class Meta:
model = Class
fields = ['class_name']
def save(self, commit=True):
data = self.cleaned_data
module = Class(class_name=data['class_name'])
if commit:
module.save()
return module
class LoginFormView(FormView):
form_class = LoginForm
template_name = 'App/index.html'
success_url = '/'
def post(self, request, *args, **kwargs):
login_form = self.form_class(request.POST)
register_form = RegistrationForm()
if login_form.is_valid():
auth = EmailAuthBackend()
user = auth.authenticate(email=request.POST['email'], password=request.POST['password'])
if user is not None:
django_login(request, user)
return HttpResponseRedirect("../instructormodule")
else:
return self.render_to_response(
self.get_context_data(
login_form=login_form,
register_form=register_form
)
)
Upvotes: 0
Views: 60
Reputation: 856
I agree with Daniel's answer, but would like to give better direction here. Which is having your Class model linked to your users, by ForeignKey. This way you can related your classes to Users and hence you will have pointer to the user who created the class.
Please read django documentation on this:
https://docs.djangoproject.com/en/1.10/topics/db/examples/many_to_one/
Upvotes: 1
Reputation: 599956
You don't "retrieve the teacher's email from the login form". The teacher is logged in; therefore you retrieve the email from the logged-in uesr.
class CreateClassForm(forms.Form):
...
def save(self, commit=True):
data = self.cleaned_data
module = Class(class_name=data['class_name'])
module.teacher_email = self.request.user
if commit:
module.save()
return module
Note, it makes no sense to call that field teacher_email
; it points at the entire User model, so it should be called just teacher
.
Upvotes: 2