【问题标题】:Django : Storing an array of strings ( filepaths) in a model field in sqlite3Django:在 sqlite3 的模型字段中存储字符串数组(文件路径)
【发布时间】:2019-12-23 15:05:42
【问题描述】:

我正在尝试存储一组图像路径(最初是随机创建的),以便稍后按原样检索,然后将其传递给 html 模板。 我已经尝试使用很多答案中给出的 Arrayfield 选项,但我不断收到错误,因为可能我没有使用 postgresql:

from django.contrib.postgres.fields import ArrayField
lst=ArrayField(
        models.IntegerField(null=True,blank=True),
        size=5,
        default=list,
        null=True
    )

错误

from psycopg2.extras import DateRange, DateTimeTZRange, NumericRange
ModuleNotFoundError: No module named 'psycopg2'

【问题讨论】:

    标签: django sqlite django-models


    【解决方案1】:

    只有当您的数据库是 Postgresql 时,您才能使用 ArrayField。 Django 的默认 db (sqlite) 不支持 ArrayField。

    【讨论】:

    • 有什么办法可以为存储在 sqlite 中的模型设置一个 arrayfield 类型字段?
    • 很遗憾,没有,但是您可以尝试将它们存储在 CharField 中,方法是将列表转换为字符串 (",".join(my_list)) 并在读取时返回字符串 (my_model.list.split (","))
    猜你喜欢
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 2017-08-26
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多