如何在django == 2.1中链接两个表中的字段? (不是外键)

时间:2019-01-26 09:10:48

标签: django django-models

我创建了两个模型EventStudent。然后,我创建了另一个表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()

0 个答案:

没有答案