API: Create a category but append/add to description more details

I’m using API request to Discourse to create a category, however in the same process I would like to add text or URL reference etc, to Category description (or is it description_text as I try to understand the properties or differences).

Note also that there might be a “robot” adding to this, so the best way…

  1. create category (working)
  2. Update category or more so append the link, but to category.description or category.description_text)?

However there are no properties on update except name, color, text_color, so are there other properties I can send? Like (and which) description or/maybe description_text?

All I want to do is add a slug link to a site related to the category I’m creating… or simply add at the end of the Discourse Category description “[link][https/example.projectxxx”

1 个赞

Hi, Andrew.

Although the docs only list those 3 properties, most of the properties mentioned in the Response object can be set on the create call. I found this out when I needed to script the creation of a large number of categories with the same settings except for the name and slugs.

The only property I needed to set and consistently had issues with is the permissions.

2 个赞

Yes, I’ve noticed that properties allowed are not those defined in the API documentation.

However to set the Category “description” which is very confusing as the category description is not at all what is displayed, in fact below the category name is the HTML element “cooked” which is empty, so how to set that…

From what I’ve been able to understand, I must set a post in the “About the --category name-- …” and it must be the first post in that category, but I think there is other critaria, like the cooked or also the fact that we have a Bot that first posts there.

I’ve tried adjusting the created_at/updated_at dates to make my post appear above so that the category will grab or use that description, but no luck! There must be something I’m not understanding about the “cooked” and/or category description. Very confusing and I’ve found no docs, but I’ll keep looking, if anyone has ideas on what to try, please let me know.

您好,挖出这个旧话题……

有没有办法弄清楚 description_textdescription 之间的区别?
我的意思是,除了自己去发现 :slight_smile:
也许 @alank 已经弄清楚了!
谢谢!

1 个赞

如果其他地方没有记录,在 discourse 存储库的 spec 文件中进行搜索,就能解答类似这样的问题,只要对编程语言的工作原理有基本了解即可。

在这种情况下,category spec 中的以下几行表明 description_text 是从 description 派生出来的,并在设置 description 后立即可用。

这对我来说意味着 description 是描述的原始字符串,在设置它之后,它会被处理并赋值给 description_text。或者 description_text 可以在访问时计算,但我对此表示怀疑,无论如何 description 是要设置的那个。

1 个赞

谢谢 Simon,这让我明白了这两个参数的区别。

不过,我刚试过,这两个参数实际上在类别描述(或“关于类别”帖子)中都没有设置。所以 OP 的问题依然存在。

补充说明:我在 categories.json 文件中看到了我想要的这些描述参数,但我无法通过 API 设置它们!

如果 POST 请求负载未记录这些参数,那么即使它们碰巧有效,您也不能指望能够这样设置它们。

我怀疑应该这样做:在创建类别后,查看响应中的 topic_url,然后使用该 URL 中的 ID 获取单个主题。查看响应中的 post_stream,其中包含一个帖子数组,我猜它将只有一个元素。

获取该帖子的 ID,然后 更新单个帖子 以添加您的描述。这应该会像在 Discourse 中编辑帖子一样触发类别描述的更新,遵循相同的首行规则。

1 个赞

我正要发帖,这正是我所做的 :slight_smile:

    ## 创建类别
    ret_cat = client.create_category(name, color = color, **kwargs)

    ## 获取第一个帖子 ID 
    cat_id = ret_cat["category"]["id"]
    print("Cat id", cat_id)
    topics = client.category_topics(cat_id)
    first_post_id = client.topic_posts(topics["topic_list"]['topics'][0]["id"])['post_stream']['posts'][0]['id']
    ## 修改第一个帖子
    ret_post = client.update_post(first_post_id, cat["about_msg"],"set up")
3 个赞