我有以下型号:
class Guest(models.Model):
first_name = models.CharField(max_length=128)
last_name = models.CharField(max_length=128)
category = models.ForeignKey(Category)
player_achievement = models.ForeignKey(PlayerAchievement, related_name="guest_achievements")
player = models.ForeignKey(Player)
我希望在表单中选择要在我的save_model方法中使用的播放器:
def save_model(self, request, obj, form, change):
player = ??? how do i get this from the admin form?
obj.player = player
obj.save()
答案 0 :(得分:6)
这取决于您如何申报表格。如果您继承了ModelForm,那么在大多数情况下,您应该使用ModelChoiceField选择与Player
对象关联的Guest
,这是通过调用form.cleaned_data.get('player')
< / p>