我创建了一个包含CommaSeparatedIntegerField
的模型models.py
class ForumPosts(models.Model):
....
path = models.CommaSeparatedIntegerField(blank=True,max_length=50)
...
我想使用这个模型并定义我的视图如下 的 views.py
def create_forum_post(request, ..):
...
forumpost.path.append(forumpost_id)
...
我遇到了一种情况,我不得不将forumpost_id附加到路径中,该路径被定义为CommaSeperatedIntegerField。调试时出现错误
'unicode'对象没有属性'append'。
我认为可能是由于缺少逗号,我尝试了很多相同代码的变体但无法将forumpost_id添加到路径中。提前致谢
答案 0 :(得分:0)
CommaSeparatedIntegerField
的值不会自动反序列化,此字段类型的唯一功能是验证(它必须是逗号分隔的整数)。
您需要检索字段值,反序列化它,追加新整数,序列化并保存回来。
编辑1:
示例:
class ForumPosts(models.Model):
# ...
def append_to_path(self, value):
path_list = self.path.split(',')
path_list.append(value)
self.path = ','.join(path_list)
用法:
forumpost.append_to_path(forumpost_id)
forumpost.save() # save will validate if the path is correct