Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a way to dynamically change the model used in an UpdateView with Django?
    primarykey
    data
    text
    <p>The application I'm writing includes a pipeline for creating events. An <code>Event</code> is proposed by one class of users, then approved and edited by administrators. The problem for me is that there is a subclass of <code>Event</code>, <code>ScoredEvent</code>, which the administrators should be able to specify on-the-fly. This adds a <code>ScoredEvent.competition</code> foreign key and overrides the <code>Event.participants</code> to put them through a table that includes an associated score for each participant.</p> <p>Ideally, a normal user creates an event containing only the name and a short description (easy enough to do by limiting the fields on the non-admin CreateEventForm) then the admins can go back in and fill out the other fields when they are approving the event.</p> <p>The problem I'm hitting is I don't know how it would be possible for an administrator to change an <code>Event</code> to a <code>ScoredEvent</code> in the approval form view when they are editing it or how to make that happen. My vision of the page is an edit view with a checkbox labeled "Tie To Competition" that, when checked, would allow the admin to select a competition and then save the event as a <code>ScoredEvent</code>. If that box weren't checked, the event would continue it's life as an <code>Event</code>.</p> <p>Where should this be handled? My gut feeling is that I should do something special in the forms.py or something in the Templates, but I don't know where I should begin.</p> <pre><code>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) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload