我有一个Django项目,遇到了将数据库外键属性与表单外键属性进行比较的问题。我的项目文件如下:
我的 Model.py 文件:
class Teacher(models.Model):
Name = models.CharField(max_length=100)
Designation = models.CharField(max_length=100,choices=DESIGNATION)
Department = models.CharField(max_length=100,choices=T_Dept)
Address = models.CharField(max_length=100)
def __str__(self):
return self.Name + ", " + self.Designation + ", " + "("+self.Department +"), "+ self.Address
class Moderation(models.Model):
year = models.CharField(max_length=100,choices=T_Year)
semester = models.CharField(max_length=100,choices=T_Semester)
examtype = models.CharField(max_length=30,choices=EXAMTYPE)
examyear = models.CharField(max_length=30,choices=EXAMYEAR)
NamAdd = models.ForeignKey(Teacher, on_delete=models.CASCADE)
position = models.CharField(max_length=100,choices=POSITON)
def __str__(self):
return unicode(self.NamAdd)
我的 forms.py 文件:
class modaForm(forms.ModelForm):
class Meta:
model=Moderation
fields=[
'year',
'semester',
'NamAdd',
'position','examtype','examyear'
]
如何在视图文件中比较obj.NamAdd.Name == NamAdd?请提供任何提示来帮助我。
基本上,我想将一个唯一的 Moderation 对象保存到数据库中怎么办?有什么替代方法吗?
先谢谢了。
相关分类