Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Override <code>get_queryset</code> and vary on whether one of the fields is posted:</p> <pre><code>def get_queryset(self): if self.request.POST.has_key('competition'): return ScoredEvent.objects.all() else: return super(MyView, self).get_queryset() </code></pre> <p>So, just to explain a little better: you would use your "Tie to Competition" option you mentioned to add a field to the form via Javascript. Then, if the field is sent in the POST, it would switch to using <code>ScoredEvent</code> instead.</p> <p>If you want to automatically switch based on what the object currently is, you'd need to override <code>get_object</code> for that. Something along the lines of:</p> <pre><code>def get_object(self, queryset=None): obj = super(MyView, self).get_object(queryset=queryset) try: return ScoredEvent.objects.get(pk=obj.pk) except ScoredEvent.DoesNotExist: return obj </code></pre> <p>Which basically tries to lookup the object again as if it was a <code>ScoredEvent</code> and returns that version if it finds one, instead of <code>Event</code>.</p> <p>You also might need to similarly override <code>get_form_class</code> to ensure that it validates as a <code>ScoredEvent</code> should if it's a <code>ScoredEvent</code> or is becoming a <code>ScoredEvent</code>:</p> <pre><code>from django.forms import models as model_forms def get_form_class(self): if isinstance(self.object, ScoredEvent): return model_forms.modelform_factory(ScoredEvent, ScoredEventForm) else: return super(MyView, self).get_form_class() </code></pre>
    singulars
    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.
 

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