Django - 插入相关实例

时间:2014-01-04 23:49:09

标签: python django django-models save

我正在尝试更改模型的保存方式并自动添加字段。有问题的领域是外国人。

这是代码:

def save(self, *args, **kwargs):
    season = Season.objects.order_by('start')[0]
    self.season = season

但它没有添加任何东西......

它说成功,但它没有增加......

编辑: 完整代码:

class Team(models.Model):

GENDER_CHOICES = (
    ('F', 'Féminin'),
    ('M', 'Masculin'),
)

name = models.CharField(max_length=25,verbose_name="nom")
slug = AutoSlugField(unique=True,populate_from='name')
season = models.ForeignKey(Season,verbose_name="saison",editable=False)
association = models.ForeignKey(Association,verbose_name="association")
category = models.ForeignKey(Category,verbose_name="catégorie")
division = models.ForeignKey(Division,verbose_name="division")
gender = models.CharField(max_length=1,choices=GENDER_CHOICES,verbose_name="sexe")

class Meta:
    verbose_name = 'Équipe'
    verbose_name_plural = 'Équipes'

def __str__(self):  # Python 3: def __str__(self):
    return self.name

def save(self, *args, **kwargs):
    season = Season.objects.order_by('start')[0]
    self.season = season
    super(self,Team).save(*args,**kwargs)

谢谢, ARA

1 个答案:

答案 0 :(得分:1)

在“保存”方法结束时,您必须调用超类“保存”:

def save(self, *args, **kwargs):
    season = Season.objects.order_by('start')[0]
    self.season = season
    super(MyModelName,self).save(*args,**kwargs)