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信号,因此此代码应在保存条目然后再调用信号时起作用,如何解决此错误?

最佳答案

我在本地创建项目。
保存实例时,应使用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>


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

关于python - 使用post_save信号更新外键字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25973756/

10-13 09:24