Django form - using variable from database
<p>I have the following form which is working perfectly:</p>

<pre><code># coding=utf-8

    from django import forms
    from straightred.models import StraightredTeam
    from straightred.models import UserSelection

    class SelectTwoTeams(forms.Form):

        cantSelectTeams = UserSelection.objects.filter(campaignno=102501349)

        currentTeams = StraightredTeam.objects.filter(currentteam = 1).exclude(teamid__in=cantSelectTeams.values_list('teamselectionid', flat=True))
        team_one = forms.ModelChoiceField(queryset = currentTeams)
        team_two = forms.ModelChoiceField(queryset = currentTeams)
</code></pre>

<p>However, as you can see the campaignno is hard written into the query.  Ideally I would like to select the maximum campaignno from a mysql table and use that as the campaigno instead of the 102501349 as above.  The mysql table and model is as follows:</p>

<p>mySQL table:</p>

<pre><code>mysql&gt; desc straightred_userselection;
+-------------------+----------------------+------+-----+---------+----------------+
| Field             | Type                 | Null | Key | Default | Extra          |
+-------------------+----------------------+------+-----+---------+----------------+
| userselectionid   | int(11)              | NO   | PRI | NULL    | auto_increment |
| campaignno        | varchar(36)          | NO   |     | NULL    |                |
| teamselection1or2 | smallint(5) unsigned | NO   |     | NULL    |                |
| fixtureid         | int(11)              | YES  | MUL | NULL    |                |
| teamselectionid   | int(11)              | NO   | MUL | NULL    |                |
| user_id           | int(11)              | NO   | MUL | NULL    |                |
+-------------------+----------------------+------+-----+---------+----------------+
</code></pre>

<p>Django Model:</p>

<pre><code>class StraightredSeason(models.Model):
    seasonid = models.IntegerField(primary_key = True)
    seasonyear = models.CharField(max_length = 4)
    seasonname = models.CharField(max_length = 36)

    def __unicode__(self):
        return self.seasonid

    class Meta:
        managed = True
        db_table = 'straightred_season'
</code></pre>

<p>I hope this makes sense but if you require any more information then just ask :)</p>

<p>I appreciate any assistance in advance, many thanks, Alan.</p>

<p>Got there in the end :)</p>

<p>I used the following:    </p>

<pre><code>campaignnoquery = UserSelection.objects.filter(user=349).order_by('-campaignno')[:1]

    for x in campaignnoquery:
        test2 = x.campaignno
</code></pre>

<p>I can then use test2 as the variable.</p>

python mysql django