我正在編寫的應用程序包含一個用於創建事件的管道。 Event
由一類用戶提出,然後由管理員批准和編輯。對我來說,問題是有一個Event
,ScoredEvent
的子類,管理員應該能夠即時指定它。這將添加一個ScoredEvent.competition
外鍵,並覆蓋Event.participants
,使其通過包含每個參與者的關聯分數的表格。有沒有辦法在Django中動態更改UpdateView中使用的模型?
理想的情況下,普通用戶創建一個僅包含名稱和一個簡短的說明(很容易的通過限制非管理員CreateEventForm領域做)的事件,則管理員可以回去,並填寫在其他領域他們正在批准這一事件。
我打的問題是我不知道怎麼會是可能的管理員在審批表單視圖時,他們編輯它,或者如何使這種情況發生改變Event
到ScoredEvent
。我對該頁面的看法是一個編輯視圖,帶有一個標有「與競爭對手聯繫」的複選框,選中該選項後,管理員可以選擇一個競賽,然後將該事件保存爲ScoredEvent
。如果該框未被檢查,則該事件將繼續其作爲Event
的生命。
這應該在哪裏處理?我的直覺是,我應該在Templates.py或Templates中的某些內容中做些特別的事情,但我不知道該從哪裏開始。
class Event(models.Model):
"""Representation of any community event"""
name = models.CharField(max_length=50, unique_for_date="start_datetime")
slug = models.SlugField(max_length=57) # length +7 for datestamp
description = models.TextField()
location = models.CharField(max_length=100, blank=True)
start_datetime = models.DateTimeField('Start', blank=True)
end_datetime = models.DateTimeField('End', blank=True)
participants = models.ManyToManyField(Participant)
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.name)+'-'+datetime.now().strftime("%d%m%y")
super(Event, self).save(*args, **kwargs)
def __unicode__(self):
if self.start_datetime:
return "%s (%s)" % (self.name, self.start_datetime.date())
else:
return self.name
class ScoredEvent(Event):
"""Representation of an event that is tied to a competition"""
competition = models.ForeignKey(Competition)
participants = models.ManyToManyField(Participant, through="ScoredParticipant")
def is_scored(self):
"""Returns true if any of the participants has a score, else returns false"""
for participant in self.participants.objects.all():
if participant.score != 0:
return False
return True
class ScoredPartcipant(models.Model):
"""Participant and associated score for an event"""
participant = models.ForeignKey(Participant)
event = models.ForeignKey(ScoredEvent)
score = models.IntegerField(default=0)
你需要一個外鍵從'ScoredEvent'正常'事件「,然後管理員創建一個'ScoredEvent'鏈接到正常的'Event',用戶填充。 – 2012-08-09 18:47:08