如何将用户模型的不同属性关联到不同的模型

时间:2019-04-10 14:11:40

标签: django django-models django-forms

我正在为我的大学创建查询门户,它有一个投诉表单,具有不同的字段,例如First_name,Last_name,email等。如何从已经保存的用户模型字段中提取First_name,Last_name,email等字段的值。

投诉模型

from django.db import models
from datetime import date
from django.contrib.auth.models import User

class complain(models.Model):
    Username = models.ForeignKey(User,on_delete=models.CASCADE)
    First_name=models.CharField(max_length=30)
    Last_name=models.CharField(max_length=30)
    Roll_no=models.CharField(max_length=30)
    Email_address=models.EmailField(max_length = 100,verbose_name='email address',blank=False,)
    FIRST = '1st'
    SECOND = '2nd'
    THIRD = '3rd'
    FOURTH = '4th'
    Hostel=((FIRST, 'First'),(SECOND, 'Second'),(THIRD, 'Third'),    (FOURTH, 'Fourth'),)
   Hostel=models.CharField(max_length=3,choices=Hostel,default=FIRST,)
    Room_no=models.CharField(max_length=10)
    ACADEMIC = 'Academic'
    HOSTEL = 'Hostel'
    SPORTS= 'Sports'
    RAGGING = 'Ragging'
    ComplainDepartment=((ACADEMIC, 'Academic'),(HOSTEL, 'Hostel'),(SPORTS, 'Sports'),(RAGGING, 'Ragging'),)
                 ComplainDepartment=models.CharField(max_length=100,choices=ComplainDepartm ent,default=HOSTEL)
   Complain_Subject = models.CharField(max_length = 100,default =  "complain_subject")
   ACADEMIC_HEAD = 'Mr.J'
   HOSTEL_HEAD = 'Mr.Y'
   SPORTS_HEAD = 'Mr.Z'
   RAGGING_HEAD  = 'Mr.W'
   DepartmentHead=((ACADEMIC_HEAD, 'Mr.J'),(HOSTEL_HEAD, 'Mr.Y'),(SPORTS_HEAD, 'Mr.Z'),(RAGGING_HEAD, 'Mr.W'),)
DepartmentHead=models.CharField(max_length=10,choices=DepartmentHead,default=ACADEMIC_HEAD)
   ACADEMIC_HEAD_EMAIL = 'it1530@cemk.ac.in'
   HOSTEL_HEAD_EMAIL = 'it1531@cemk.ac.in'
   SPORTS_HEAD_EMAIL = 'it1525@cemk.ac.in'
   RAGGING_HEAD_EMAIL  = 'w.w5@gmail.com'
   E_O_H=((ACADEMIC_HEAD_EMAIL, 'it1530@cemk.ac.in'),(HOSTEL_HEAD_EMAIL, 'it1531@cemk.ac.in'),(SPORTS_HEAD_EMAIL, 'it1525@cemk.ac.in'),(RAGGING_HEAD_EMAIL, 'w.w5@gmail.com'),)
  E_O_H=models.CharField(max_length=100,choices=E_O_H,default=ACADEMIC_HEAD_EMAIL)
   ComplainDescription=models.TextField(max_length=500)
   ComplainDate=models.DateField(("Date"), default=date.today)
   NOTVISITED = 'NV'
   VISITED = 'V'
   INPROCESS= 'IP'
   COMPLETED = 'C'
   Status=((NOTVISITED, 'Not Visited'),(VISITED, 'Visited'),(INPROCESS, 'Inprocess'),(COMPLETED, 'Completed'),)
 Status=models.CharField(max_length=2,choices=Status,default=NOTVISITED)

  def __str__(self):
     return self.Name

投诉表

 from django.forms import ModelForm
 from shp.models import *

 class complainform(ModelForm):
    class Meta:
        model=complain
        fields=  ['Name','Roll_no','Email_address','Hostel','Room_no','ComplainDepartment','Complain_Subject','DepartmentHead','E_O_H','ComplainDescription','ComplainDate','Status']

0 个答案:

没有答案