Pass database value in form (django)
<p>My <code>form.py</code>:</p>

<pre><code>class BannerForm(forms.ModelForm):
    name = forms.CharField(max_length=32)
    #Affiliazione = forms.CharField(disabled = True, initial='red') #in original question
    #affiliation = forms.ModelChoiceField(Affiliation.objects.all(),
    #widget=forms.HiddenInput(), initial=Affiliation.objects.get(id=1))  #in original question
    Affiliazione = forms.CharField(disabled = True, required=False) #added after first answer
    affiliation = forms.ModelChoiceField(Affiliation.objects.all(),
    widget=forms.HiddenInput()) #added after first answer
</code></pre>

<p>The 'Affiliazione' field display 'red' but it isn't saved because <a href="https://www.w3.org/TR/html401/interact/forms.html#disabled" rel="nofollow">Disabled controls cannot be successful</a>. The 'affiliation' field actually pass the data but is hidden. They together give what I want (a disabled field that pass data so the user can see the value but can't change it).</p>

<p>The problem is I don't like to hardcode that values ('red' and 'id=1'). I have the 'Options' class in models where I choose the value to pass but I don't know how... I think it's a silly question, sorry, but someone can help me?</p>

<p>My <code>models.py</code>:</p>

<pre><code>class Options(models.Model):
    new_affiliation = models.ForeignKey('Affiliation')

class Affiliation(models.Model):
    name = models.CharField(max_length=32, unique=True)
    def __str__(self):
        return self.name

class Banner(models.Model):
    name = models.CharField(max_length=32, unique=True)
    affiliation = models.ForeignKey(Affiliation)
</code></pre>

<p>Edit. My <code>View.py</code>:</p>

<pre><code>def add_banner(request):
    # A HTTP POST?
    if request.method == 'POST':
        form = BannerForm(request.POST)
        print('form is post') #control
        # Have we been provided with a valid form?
        if form.is_valid():
            print('form is valid') #control
            # Save the new banner to the database.
            banner = form.save(commit=False)
            #some irrilevant code here
            form.save(commit=True)
            print('form salvato') #control
            # Now call the homepage() view.
            # The user will be shown the homepage.
            return homepage(request)
        else:
            # The supplied form contained errors - just print them to the terminal
            print (form.errors)
    else:
        # If the request was not a POST, display the form to enter details
        #form = BannerForm(request.POST) #in original question
        #initial_value = 'red' #added after first answer
        #init = Affiliation.objects.get(id=1) #added after first answer
        form = BannerForm(request.POST or None, initial={
        'affiliation': Campaign_Options.new_regent_affiliation}) #added after first answer
    # Bad form (or form details), no form supplied...
    # Render the form with error messages (if any).
    print ('fine')
    return render(request, 'core/add_banner.html', {'form': form})
</code></pre>

<p>My <code>add_banner.html</code>:</p>

<pre><code>  {% csrf_token %}
  {% for hidden in form.hidden_fields %}
  {{ hidden }}
  {% endfor %}

  {% for field in form.visible_fields %}
    {{ field.errors }}
    {{ field.label }}
    {{ field }}
    {{ field.help_text }}
    &lt;br /&gt;
  {% endfor %}
</code></pre>