django admin save_model 不保存模型更改
Posted
技术标签:
【中文标题】django admin save_model 不保存模型更改【英文标题】:django admin save_model not saving model changes 【发布时间】:2013-08-02 09:28:05 【问题描述】:这是我的 admin.py:
class TransactionInfoAdmin(admin.ModelAdmin):
search_fields=['transactionId','subscriptionInfo__customerNumber']
save_on_top = False
def has_add_permission(self,request):
return False
def has_delete_permission(self, request, obj=None):
return False
def get_readonly_fields(self, request, obj=None):
if obj:
return self.readonly_fields + ('subscriptionInfo','transactionId','authorizationId','responseCode','operation','batchNumber','merchantId')
return self.readonly_fields
def save_model(self, request, obj, form, change):
#SUCCESS ACTIONS
obj.subscriptionInfo.paid = 1
obj.save()
pass
当我保存obj.subscriptionInfo.paid = 1
时,它没有更新数据行,有什么想法吗?
注意:subscriptionInfo 是上述模型 TransactionInfo 中的外键。
-------------- 更新 -------- ------
我的模型.py
class SubscriptionInfo(models.Model):
subscriptionId = models.CharField(max_length=50)
customerNumber = models.IntegerField()
subscriptionType = models.CharField(max_length=50, default="basic")
currency = models.CharField(max_length=50, default="NOK")
paid = models.IntegerField(default=0)
class Meta:
verbose_name = "Subscription Information"
verbose_name_plural = "Subscription Information"
def __unicode__(self):
return u'%s' % self.customerNumber
class TransactionInfo(models.Model):
subscriptionInfo = models.ForeignKey(SubscriptionInfo, db_column='f')
authorizationId = models.IntegerField()
transactionId = models.TextField()
batchNumber = models.IntegerField()
merchantId = models.IntegerField()
operation = models.CharField(max_length=50)
responseCode = models.CharField(max_length=50)
paidDate = models.DateField(_("Date"), default=datetime.date.today)
class Meta:
verbose_name = "Transaction Information"
verbose_name_plural = "Transaction Information"
def __unicode__(self):
return u'Transaction : %s' % self.pk
有人吗?
【问题讨论】:
你能显示你的模型代码吗? 用我的模型编辑了我的问题。 @GlynJackson我不确定,因为我需要将付费字段更改为1,否则默认为0。 【参考方案1】:你必须打电话:
obj.subscriptionInfo.save()
因为它在一个单独的模型上,它看起来像这样:
def save_model(self, request, obj, form, change):
#SUCCESS ACTIONS
obj.subscriptionInfo.paid = 1
obj.subscriptionInfo.save()
obj.save()
【讨论】:
太棒了!!非常感谢:) 太棒了!!非常感谢:)以上是关于django admin save_model 不保存模型更改的主要内容,如果未能解决你的问题,请参考以下文章
Django admin save_model 没有在 Postgres 中推进主键
覆盖 Django InlineModelAdmin 上的 save_model
为啥 save_model 方法在 admin.StackedInline 中不起作用?