0
から変数を使用してI完璧に働いている次のような形式があります。Djangoのフォーム - データベース
# 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)
しかし、あなたがcampaignnoを見ることができるように、クエリに書き込ま難しいです。理想的には、私はmysqlテーブルから最大のcampaignnoを選択し、上記の102501349の代わりにそれをcampaignoとして使用したいと思います。次のようにMySQLのテーブルとモデルがある:
MySQLのテーブル:
mysql> 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 | |
+-------------------+----------------------+------+-----+---------+----------------+
Djangoのモデル:
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'
私は、これは理にかなっていますが、任意のより多くの情報が必要ならば、ちょうど尋ねる願っています:)
事前に助力をいただき、ありがとうございました。最後に
mysqlテーブルからmaximum campaignnoを選択すると、最大数はテーブルの現在の行数に依存しますか?現時点では – Windsooon
には2つしかありませんが、負荷がかかります。それはどのようにして行われたのか、より根本的なものでした。 –