如何使用Python Django模型在PostgreSQL中获得数组字段的长度

发布于 2025-02-10 04:50:28 字数 595 浏览 0 评论 0原文

这是我的django模型:

class UserVpaUpiId(models.Model):
    user_id = models.IntegerField(db_index=True, unique=True)
    vpa_upi_id = models.TextField(null=False, blank=True)
    created_on = models.DateTimeField(auto_now_add=True, null=False)
    updated_on = models.DateTimeField(auto_now=True, null=False)

我想用特定的user_id删除一个条目,我可以成功地使用它:

UserVpaUpiId.objects.filter(user_id=user_id).delete()

想显示upis的数量,upis的数量存储在vpa_upi_id中:

["acbd.pq@icici", "9987654431@apl", "9876543210@apl"]

但是,在此之前,我 我可以实现吗?

This is my Django model:

class UserVpaUpiId(models.Model):
    user_id = models.IntegerField(db_index=True, unique=True)
    vpa_upi_id = models.TextField(null=False, blank=True)
    created_on = models.DateTimeField(auto_now_add=True, null=False)
    updated_on = models.DateTimeField(auto_now=True, null=False)

I want to delete an entry with a particular user_id, which I am able to do successfully using:

UserVpaUpiId.objects.filter(user_id=user_id).delete()

But, before that, I want to display the number of UPIs, which are stored in vpa_upi_id in the format:

["acbd.pq@icici", "9987654431@apl", "9876543210@apl"]

How can I achieve that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独自唱情﹋歌 2025-02-17 04:50:28
vpa_upa_ids = UserVpaUpiId.objects.filter(user_id=user_id).values('vpa_upi_id')[0]['vpa_upi_id']
arr = vpa_upa_ids[1:-1].split(",")
length = len(arr)

由于您已经必须使用User_id才能删除记录,因此您可以获取匹配记录,将结果转换为字符串并执行一些字符串操作以将其转换为阵列,然后您可以抓住长度。

vpa_upa_ids = UserVpaUpiId.objects.filter(user_id=user_id).values('vpa_upi_id')[0]['vpa_upi_id']
arr = vpa_upa_ids[1:-1].split(",")
length = len(arr)

Since you already have to user_id to delete the record you can grab the matching record(s), convert the result into a string and perform some string manipulations to turn it into an array where you can then grab the length.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文