将一个json字段添加到python模型并在其上加载其他模型的数据

时间:2014-04-25 14:54:39

标签: python database django postgresql

我正在做一个Django应用程序,我的库存应用程序有两个模型。

class Watchlist(models.Model):
  def number():
    no = Watchlist.objects.count()
    if no == None: 
      return 1
    else: 
      return no + 1    

  name = models.CharField(max_length=200)
  created = models.DateTimeField(auto_now_add=True)
  user = models.ForeignKey('users.User')
  stocks = models.ManyToManyField('Stock')
  equityboss = models.BooleanField(blank=True, default=False)
  last_watched =  models.DateTimeField(auto_now_add=True)
  order = models.IntegerField(max_length=10, default=number)

  def __unicode__(self):
    return "%s - %s"%(self.name, self.id)

  class Meta:
    unique_together = ("name", "user")

  def as_json(self):
      return dict(
          name=self.name, 
          created=self.created.isoformat(),
          equityboss=self.equityboss, 
          id = self.id,
          last_watched=self.last_watched.isoformat(),
          order=self.order)

class IndicatorPreference(models.Model):
  User = models.ForeignKey('users.User')
  indicator = models.ForeignKey('Indicator')
  visible = models.BooleanField(default=True)
  order = models.IntegerField(max_length=2, db_index=True)
  Watchlist = models.ForeignKey('Watchlist')


  def __unicode__(self):
      return 'IndicatorPreference :'+self.User.email+"-"+self.Watchlist.name+"-"+self.indicator.group.name+"-"+self.indicator.indicator.name

我想将一个列indicator_preferences添加到Watchlist模型并编写迁移

脚本将IndicatorPreference模型中的现有数据移植到此indicator_preferences

列。此indicator_preferences列应为json列。

我该怎么做?

0 个答案:

没有答案