Python 属性错误位于/posts/create/:';str';对象没有属性';设置';当尝试从标题字段解析哈希标记并将其保存在标记字段中时
我试图用django中的Python 属性错误位于/posts/create/:';str';对象没有属性';设置';当尝试从标题字段解析哈希标记并将其保存在标记字段中时,python,django,django-models,django-forms,Python,Django,Django Models,Django Forms,我试图用django中的post_save信号从title字段解析hashtags并保存到tags字段。我正在为标记使用django-taggit包,但在保存表单时出现此错误。save_m2m。有人能帮忙解决这个错误吗 this is the link to the package: https://django-taggit.readthedocs.io/en/latest/getting_started.html signal.py def parse_hash_tags(sender,
post_save信号
从title字段解析hashtags并保存到tags字段。我正在为标记使用django-taggit
包,但在保存表单时出现此错误。save_m2m
。有人能帮忙解决这个错误吗
this is the link to the package: https://django-taggit.readthedocs.io/en/latest/getting_started.html
signal.py
def parse_hash_tags(sender, instance, created, **kwargs):
post_save.disconnect(parse_hash_tags, sender=Post)
instance.tags = ','.join(re.findall(r'(?:#(\w+))', instance.title))
instance.save()
post_save.connect(parse_hash_tags, sender=Post)
post_save.connect(parse_hash_tags, sender=Post)
回溯
Traceback:
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\generic\base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\contrib\auth\mixins.py" in dispatch
52. return super().dispatch(request, *args, **kwargs)
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\generic\base.py" in dispatch
97. return handler(request, *args, **kwargs)
File "C:\danny\Study\test\posts\views.py" in post
231. form.save_m2m()
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\forms\models.py" in _save_m2m
441. f.save_form_data(self.instance, cleaned_data[f.name])
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\taggit\managers.py" in save_form_data
517. getattr(instance, self.name).set(*value)
Exception Type: AttributeError at /posts/create/
Exception Value: 'str' object has no attribute 'set'
不能将逗号分隔的列表用作多对多关系的值。但是,您可以列出标记,然后使用:
def parse_hash_标记(发送方、实例、已创建、**kwargs):
tags=re.findall(r'(?:#(\w+)),instance.title)
instance.tags.set(*tags)
post\u save.connect(解析\u散列\u标记,发送方=post)
设置多对多关系不会触发保存实例本身,因此无需调用
instance.save()
或断开/重新连接。断开连接也有点“棘手”,因为同时修改的另一个模型对象将不再触发信号。您不能将逗号分隔的字符串设置为标记列表。您好,感谢您的响应。我对这件事很陌生。那么,你能告诉我我可以在这里做些什么更改来实现这一点吗?非常感谢你提供的新信息,但我尝试了代码,它在保存表单后并没有像我预期的那样将hashtags保存到tags字段。@Danny:但你没有触发form.save()
,或者form.save\m2m()
对吗?从那时起,您将“撤消”触发器所做的工作。不过,将逻辑移到我想的形式可能更好,而且无论如何不要使用信号。可以那么,您是想在表单保存到视图中还是在前端之前执行此操作?@Danny:如果您使用instance=form.save(commit=False)
,那么instance.save()
您将避免使用表单.save\u m2m()
。但也许最好是“删除”信号,并以形式实现它。save_m2m()
,因为这样会更方便。好的,我明白了。非常感谢。我会尝试的。
def parse_hash_tags(sender, instance, created, **kwargs):
tags = re.findall(r'(?:#(\w+))', instance.title)
instance.tags.set(*tags)
post_save.connect(parse_hash_tags, sender=Post)