我尝试将Lesson模型中的lesson.price和lesson.invoice_id以及Student模型中的student.student_id传递到单个视图中,以便在模板中显示它们。但是,Lesson模型中有一个字段“student”,该字段的外键指向User。而不是学生模型。你会发现我的视图类代码是错误的,因为我不知道如何获得一个正确的学生对象与一个主要的是用于课程对象。如何在视图类中获得具有lessage_id主键的适当student对象?
class User(AbstractUser):
'''User model for authentication and lessons authoring.'''
class Role(models.TextChoices):
ADMIN="ADMIN",'Admin'
STUDENT="STUDENT",'Student'
TEACHER="TEACHER",'Teacher'
id = models.AutoField(primary_key=True)
username = models.CharField(
max_length=30,
unique=True,
validators=[RegexValidator(
regex=r'^@\w{3,}$',
message='Username must consist of @ followed by at least three alphanumericals.'
)]
)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField(unique=True, blank=False)
gender = models.CharField(max_length=255)
address = models.TextField(default='')
baseRole = Role.ADMIN
role = models.CharField(max_length=50, choices=Role.choices)
created_at = models.DateTimeField(default=timezone.now, blank=True)
updated_at = models.DateTimeField(default=timezone.now, blank=True)
def save(self, *args, **kwargs):
self.role = self.baseRole
return super().save(*args, **kwargs)
def __str__(self):
return self.first_name+" "+self.last_name
class Student(User):
student_id = models.CharField(max_length=10, default=uuid.uuid4)
baseRole = User.Role.STUDENT
student = StudentManager()
class Lesson(models.Model):
lesson_id = models.AutoField(primary_key=True)
lesson_name = models.CharField(max_length=255)
student = models.ForeignKey(User,on_delete=models.DO_NOTHING,related_name='studying', unique=True)
teacher = models.ForeignKey(User,on_delete=models.CASCADE, related_name='teaching')
start_time = models.DateTimeField(default=timezone.now)
interval = models.IntegerField()
duration = models.IntegerField()
created_at = models.DateTimeField(default=timezone.now, blank=True)
updated_at = models.DateTimeField(default=timezone.now, blank=True)
is_request = models.BooleanField()
number = models.IntegerField(default=0)
invoice_id = models.CharField(max_length=10, default=uuid.uuid4)
@property
def price(self):
return self.duration/5 * self.number
@staticmethod
def durations():
return [20, 30, 40, 45, 60]
@staticmethod
def subjects():
return ['Guitar','Violin','Piano', 'Voice', 'Cello','Ukulele','Recorder', 'Drums']
@staticmethod
def intervals():
return [2, 5, 7, 10, 14]
def __str__(self):
return "Lesson id: "+str(self.lesson_id)+", Student id: "+str(self.student.id)+", Student: "+str(self.student)
views.py
def invoice(request, lesson_id):
lesson = Lesson.objects.get(lesson_id=lesson_id)
student = Lesson.student.get(student_id=lesson.student.student_id)
return render(request, 'invoice.html', {'lesson':lesson, "student":student})
invoice.html
{% extends 'student/student_home_base.html' %}
{% block student_content %}
<head>Your Invoice</head>
<p>Your invoice reference number is{{lesson.student_id}}-{{lesson.invoice_id}}</p>
<p>Your Total Payable is {{lesson.price}}</p>
<p>Once you're done paying, please click this button below.</p>
<input type="submit" value="submit">
{% endblock %}
1条答案
按热度按时间3htmauhk1#
请注意,变量
lesson
包含Lesson
类的一个示例。请注意,变量student
实际上将包含User
类的一个示例。如果需要Student
示例,则应执行以下操作:其中,
lesson
在Lesson对象中,第一个.student
调用与User
的外键关系,第二个.student
是相对Student对象,由于多表继承,该对象与一对一隐式关系(与用户)相关。此处提供所有详细信息。