使用post_save信号更新foreignkey字段

时间:2014-09-22 12:07:17

标签: python django django-models django-signals

models.py

from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver

class website(models.Model):
    uid = models.CharField(primary_key=True,max_length=40)
    name= models.CharField(max_length=100,unique=True)

    def __unicode__(self):
        return unicode(self.uid)

class data(models.Model):
    uuid=models.ForeignKey('website')
    title=models.CharField(max_length=50)

def __unicode__(self):
    return unicode(self.uuid)

@receiver(post_save,sender=website)
def my_handler(sender,instance,**kwargs):
    p=data(uuid=instance.uid,title='it works!')
    p.save()

每次在data表中创建/更新条目时,我想创建/更新website表中的条目。由于uid和uuid之间存在外键关系,我收到此错误当我尝试使用uid ='xyz'在website表中创建一个条目时:

Traceback:
File "C:\bunker\lib\site-packages\django\core\handlers\base.py" in get_response
112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\bunker\lib\site-packages\django\contrib\admin\options.py" in wrapper
432.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\bunker\lib\site-packages\django\utils\decorators.py" in _wrapped_view
99.                     response = view_func(request, *args, **kwargs)
File "C:\bunker\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
52.         response = view_func(request, *args, **kwargs)
File "C:\bunker\lib\site-packages\django\contrib\admin\sites.py" in inner
198.             return view(request, *args, **kwargs)
File "C:\bunker\lib\site-packages\django\utils\decorators.py" in _wrapper
29.             return bound_func(*args, **kwargs)
File "C:\bunker\lib\site-packages\django\utils\decorators.py" in _wrapped_view
99.                     response = view_func(request, *args, **kwargs)
File "C:\bunker\lib\site-packages\django\utils\decorators.py" in bound_func
25.                 return func(self, *args2, **kwargs2)
File "C:\bunker\lib\site-packages\django\db\transaction.py" in inner
371.                 return func(*args, **kwargs)
File "C:\bunker\lib\site-packages\django\contrib\admin\options.py" in add_view
1131.                 self.save_model(request, new_object, form, False)
File "C:\bunker\lib\site-packages\django\contrib\admin\options.py" in save_model
860.         obj.save()
File "C:\bunker\lib\site-packages\django\db\models\base.py" in save
545.                        force_update=force_update, update_fields=update_fields)
File "C:\bunker\lib\site-packages\django\db\models\base.py" in save_base
582.                                    update_fields=update_fields, raw=raw, using=using)
File "C:\bunker\lib\site-packages\django\dispatch\dispatcher.py" in send
185.             response = receiver(signal=self, sender=sender, **named)
File "c:\bunker\codechef\mysite\img\models.py" in my_handler
58.     p=data(uuid=instance.uid,title='aha!it works!')
File "C:\bunker\lib\site-packages\django\db\models\base.py" in __init__
405.                 setattr(self, field.name, rel_obj)
File "C:\bunker\lib\site-packages\django\db\models\fields\related.py" in __set__
339.                                  self.field.name, self.field.rel.to._meta.object_name))

Exception Type: ValueError at /admin/img/website/add/
Exception Value: Cannot assign "u'xyz'": "data.uuid" must be a "website" instance.

我无法理解,因为我正在使用post_save信号,所以此代码应该工作,因为条目首先保存,然后调用信号。如何解决此错误?

1 个答案:

答案 0 :(得分:2)

我在本地创建了项目。 保存实例时,您应该使用p=data(uuid_id=instance.uid,title='it works!')uuid_id)。

>>> from test2 import models
>>> site = models.website(uid='123',name='test')
>>> site.save()
>>> models.website.objects.get()
<website: 123>
>>> models.data.objects.get()
<data: data object>

注意:类名应为大写(网站,数据)