[英]django - inlineformset_factory with more than one ForeignKey
Im trying to do a formset with the following models (boost is the primary): 我正在尝试使用以下模型制作表单(boost是主要模型):
class boost(models.Model): creator = models.ForeignKey(userInfo) game = models.ForeignKey(gameInfo) name = models.CharField(max_length=200) desc = models.CharField(max_length=500) rules = models.CharField(max_length=500) subscribe = models.IntegerField(default=0) class userInfo(models.Model): pic_url= models.URLField(default=0, blank=True) auth = models.ForeignKey(User, unique=True) birth = models.DateTimeField(default=0, blank=True) country= models.IntegerField(default=0, blank=True) class gameInfo(models.Model): psn_id = models.CharField(max_length=100) name = models.CharField(max_length=200) publisher = models.CharField(max_length=200, default=0) developer = models.CharField(max_length=200, default=0) release_date = models.DateTimeField(blank=True, null=True)
I want to display a form to add a Boost item, trying to do in this way : 我想显示一个表单以添加Boost项目,尝试以这种方式进行操作:
TrophyFormSet = inlineformset_factory(db.gameInfo, db.boost, extra=1) formset = TrophyFormSet()
Here is my questions : 这是我的问题:
1 - When renderized, the combo box for "Creator" show a list of "db.userInfo" (literally) ! 1-渲染后,“ Creator”的组合框显示“ db.userInfo”的列表(字面意思)! I want this to display db.userInfo.auth.username that are already in the database... how to do this ? 我希望它显示数据库中已经存在的db.userInfo.auth.username ...如何执行此操作?
2 - In this way, where is my "db.gameInfo" to choose ? 2-这样,我的“ db.gameInfo”在哪里选择?
thank you ! 谢谢 ! =D = D
====== ======
czarchaic answered my question very well ! czarchaic很好地回答了我的问题! Bu now i need just a little question: Bu现在我只需要一个小问题:
When i use the modelform to create a form for the boost_trophy model : 当我使用modelform为boost_trophy模型创建表单时:
class boost_trophy(models.Model): boost = models.ForeignKey(boost) trophy = models.ForeignKey(gameTrophyInfo) # 0 - Obtiveis # 1 - Requisitos minimos type = models.IntegerField(default=0) class gameTrophyInfo(models.Model): game = models.ForeignKey(gameInfo) name = models.CharField(max_length=500) desc = models.CharField(max_length=500) type = models.CharField(max_length=20)
its work nice , but i want to the form to show in the "game" box only a realy small set of itens, only the : gameTrophyInfo(game__name="Game_A") results. 它的工作很好,但是我想让表单在“游戏”框中仅显示一小部分itens,仅显示:gameTrophyInfo(game__name =“ Game_A”)结果。 How can i do this ? 我怎样才能做到这一点 ?
If I understand you correctly: 如果我正确理解您的意见:
To change what is displayed set the model's __unicode__
function 要更改显示的内容,请设置模型的__unicode__
函数
class userInfo(models.Model):
#model fields
def __unicode__(self):
return self.auth.username
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.