ValueError:无法添加*:实例位于数据库“默认”上,值位于数据库“ None”上 [英] ValueError: Cannot add *: instance is on database "default", value is on database "None"

查看:169
本文介绍了ValueError:无法添加*:实例位于数据库“默认”上,值位于数据库“ None”上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 在[1]中:从editor.models导入* 
在[4]中:从subscriptions.models导入*
在[5]中:template = StockTemplate .objects.create(name ='Template 1')
In [6]:template
Out [6]:< StockTemplate:Template 1>
在[7]中:计划= SubscriptionPlan.objects.create(name ='Bronze')
在[8]中:计划
Out [8]:< SubscriptionPlan:Bronze>
在[12]中:plan.templates.add(template)
----------------------------- ----------------------------------------------
ValueError追溯(最近一次通话)

/ home / me / GitProjects / test_git / proj /< ipython控制台>在< module>()中

/home/me/GitProjects/test_git/django-trunk/django/db/models/fields/related.pyc在add(self,* objs)$ b $中b 498如果rel.through._meta.auto_created:
499 def add(self,* objs):
-> 500 self._add_items(self.source_field_name,self.target_field_name,* objs)
501
502#如果这是与self对称的m2m关系,请在m2m表
$中添加镜像条目b_b
/home/me/GitProjects/test_git/django-trunk/django/db/models/fields/related.pyc in _add_items(self,source_field_name,target_field_name,* objs)
558如果不是router.allow_relation(obj,self.instance):
559提高ValueError('无法添加%r:实例位于数据库%s上,值位于数据库%s上'%
-> 560(obj,self.instance._state.db,obj._state.db))
561 new_ids.add(obj.pk)
562 elif isinstance(obj,Model):

ValueError:无法添加< StockTemplate:模板1>:实例位于数据库 default上,值位于数据库 None上

模型

  6类SubscriptionPlan(models.Model):
7名称=模型。 CharField(max_length = 255)
8个模板= models.ManyToManyField(StockTemplate)
9 month_fee = models.IntegerField( Monthly Fee,max_length = 16,default = 0)
10经过修改= models.DateTimeField(auto_now = True,editable = False)
11创建= models.DateTimeField(auto_now_add = True,editable = False)
12
13 def __unicode __(self):
14返回%s%self.name



18类StockTemplate(IKImage):
19名称= models.TextField()
20描述= models.TextField(空白=真实)
21
22 is_public = models.BooleanField(默认=真实)
23
24 html = models.FileField(upload_to ='stock_templates / html /',\
25 help_text ='将用于渲染的文件。')
26 #css = models.FileField(upload_to ='stock_templates / c ss /',blank = True)
27
28 img = models.ImageField(upload_to ='stock_templates / img /')
29
30经过修改= models.DateTimeField(auto_now = True)
31创建=模型.DateTimeField(auto_now_add = True)
32
33对象= StockTemplateManager()
34
35类IKOptions:
36 spec_module ='editor.specs'
37 cache_dir ='stock_templates / img / specs /'
38 image_field ='img'
39
40 def __unicode __(self):
41返回u%s%self.name
42
43 def get_absolute_url(self):
44 return reverse('preview_stock',args = [self.id])

是否与StockTemplate是IKImage对象有关?

解决方案

这里的问题是您需要在添加之前调用两个对象的保存方法:

  template.save()
plan.save()
plan.templates.add(template)

如果所有对象都没有ID,Django将无法添加计划模板/ p>

In [1]: from editor.models import *
In [4]: from subscriptions.models import *
In [5]: template = StockTemplate.objects.create(name='Template 1')
In [6]: template
Out[6]: <StockTemplate: Template 1>
In [7]: plan = SubscriptionPlan.objects.create(name='Bronze')
In [8]: plan
Out[8]: <SubscriptionPlan: Bronze>
In [12]: plan.templates.add(template)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/home/me/GitProjects/test_git/proj/<ipython console> in <module>()

/home/me/GitProjects/test_git/django-trunk/django/db/models/fields/related.pyc in add(self, *objs)
    498         if rel.through._meta.auto_created:
    499             def add(self, *objs):
--> 500                 self._add_items(self.source_field_name, self.target_field_name, *objs)
    501 
    502                 # If this is a symmetrical m2m relation to self, add the mirror entry in the m2m table


/home/me/GitProjects/test_git/django-trunk/django/db/models/fields/related.pyc in _add_items(self, source_field_name, target_field_name, *objs)
    558                         if not router.allow_relation(obj, self.instance):
    559                            raise ValueError('Cannot add "%r": instance is on database "%s", value is on database "%s"' %
--> 560                                                (obj, self.instance._state.db, obj._state.db))
    561                         new_ids.add(obj.pk)
    562                     elif isinstance(obj, Model):

ValueError: Cannot add "<StockTemplate: Template 1>": instance is on database "default", value is on database "None"

Models

  6 class SubscriptionPlan(models.Model):
  7     name = models.CharField(max_length=255)
  8     templates = models.ManyToManyField(StockTemplate)
  9     monthly_fee = models.IntegerField("Monthly Fee", max_length=16, default="0")
 10     modified = models.DateTimeField(auto_now=True, editable=False)
 11     created = models.DateTimeField(auto_now_add=True, editable=False)
 12 
 13     def __unicode__(self):
 14         return "%s" % self.name



 18 class StockTemplate(IKImage):
 19     name = models.TextField()
 20     description = models.TextField(blank=True)
 21 
 22     is_public = models.BooleanField(default=True)
 23 
 24     html = models.FileField(upload_to='stock_templates/html/', \
 25                             help_text='The file that will be used to render.')
 26     #css = models.FileField(upload_to='stock_templates/css/', blank=True)
 27 
 28     img = models.ImageField(upload_to='stock_templates/img/')
 29 
 30     modified = models.DateTimeField(auto_now=True)
 31     created = models.DateTimeField(auto_now_add=True)
 32 
 33     objects = StockTemplateManager()
 34 
 35     class IKOptions:
 36         spec_module = 'editor.specs'
 37         cache_dir = 'stock_templates/img/specs/'
 38         image_field = 'img'
 39 
 40     def __unicode__(self):
 41         return u"%s" % self.name
 42 
 43     def get_absolute_url(self):
 44         return reverse('preview_stock', args=[self.id])

Is it something to do with the fact that StockTemplate is an IKImage object?

解决方案

The problem here is you need call the method save for both objects before add:

template.save()
plan.save()
plan.templates.add(template)

Django can not add a template of a plan if none of that objects don't have an id

这篇关于ValueError:无法添加*:实例位于数据库“默认”上,值位于数据库“ None”上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆