gpt4 book ai didi

python - 使用 post_save 信号更新外键字段

转载 作者:太空宇宙 更新时间:2023-11-04 03:41:56 24 4
gpt4 key购买 nike

模型.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()

每次在 website 表中创建/更新条目时,我想在 data 表中创建/更新条目。因为 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>

注意:类名要大写(Website, Data)

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com