Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The tricky part is basically the rendering of the <code>Temp.food</code> as something other than a select widget(which is the default for ForeignKey relationships).</p> <p>For this you'd have to subclass the <code>Select</code> widget and override the <code>render</code> method to output just a label with the selected value</p> <pre><code>from django.forms.widgets import Select class MyLabelSelect(Select): def render(self, name, value, attrs=None, choices=()): # read the django.forms.widgets.Select.render() source to # understand what you have to do here. Not that hard. pass </code></pre> <p>A formset is indeed what you need here, with a caveat: you'll need to pass it a slightly custom form that uses your new widget for the FK relationship.</p> <pre><code>from django import forms from .widgets import MyLabelSelect from .models import Temp class TempForm(forms.ModelForm): class Meta: model = Temp exclude = ('date', 'time') widgets = { 'food': MyLabelSelect } </code></pre> <p>OK now we need to construct the actual formset</p> <pre><code>from django.forms.models import modelformset_factory from .forms import TempForm from .models import Temp def my_view(request): TempFormset = modelformset_factory( Temp, form = TempForm, max_num = Temp.objects.count() ) if request.method == 'POST' and request.POST: # I'll just demonstrate the GET request pass else: # Grab the Temp objects we want temps = Temp.objects.filter(food__tracktemp=True) temp_formset = TempFormset( queryset=temps ) return render_to_response(....) </code></pre> <p>Haven't actually tested the above code, but something along those lines is what you're after. If I have any glaring errors tell me and I'll correct them.</p>
    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.
    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