我创建了两个模型Event
和Student
。然后,我创建了另一个表EventParticipants
,其中一个字段是Event
表的外键(包含事件名称)。我现在想要EventParticipants
表中的另一个字段,当他单击一个按钮参加活动时,该字段会用该表Student
中的学生的详细信息进行更新。我怎样才能建立一个这样的领域?
模型:
from django.db import models
from django.urls import reverse
from students.models import Student
class EventType(models.Model):
type_of_event = models.CharField(max_length=100, unique=True)
def __str__(self):
return self.type_of_event
def get_absolute_url(self):
return reverse('dashboard')
class Event(models.Model):
type_of_event = models.ForeignKey(EventType, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
description = models.TextField()
event_date = models.DateField()
venue = models.CharField(max_length=200)
entry_fee = models.FloatField()
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('dashboard')
class EventParticipants(models.Model):
event_name = models.ForeignKey(Event, on_delete=models.CASCADE)
Student
模型:
class Student(AbstractUser):
email = models.EmailField(unique=True)
roll = models.CharField(max_length=200, blank=False)
contact_no = models.DecimalField(
max_digits=10, decimal_places=0, blank=False, unique=True)
objects = UserManager()