Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

django - ValueError: Cannot add *: instance is on database "default", value is on database "None"

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?

question from:https://stackoverflow.com/questions/7837033/valueerror-cannot-add-instance-is-on-database-default-value-is-on-databas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...