姜戈 + Postgres:如何指定字段的顺序

发布于 2024-08-26 11:55:17 字数 730 浏览 10 评论 0原文

我在 django 中有这个模型:

class JournalsGeneral(models.Model):
    jid = models.AutoField(primary_key=True)
    code = models.CharField("Code", max_length=50)
    name = models.CharField("Name", max_length=2000)
    url = models.URLField("Journal Web Site", max_length=2000, blank=True)
    online = models.BooleanField("Online?")
    active = models.BooleanField("Active?")
    class Meta:
        db_table = u'journals_general'
        verbose_name = "Journal General"
        ordering = ['code']
    def __unicode__(self):
        return self.name

我的问题是,在数据库(Postgres)中,连接到 jid 的序列名称不是 Django 预期的journals_general_jid_seq ,但它有一个不同的名字。

有没有办法指定 Django 必须为 AutoField 使用哪个序列?在我阅读的文档中我无法找到答案。

I have this model in django:

class JournalsGeneral(models.Model):
    jid = models.AutoField(primary_key=True)
    code = models.CharField("Code", max_length=50)
    name = models.CharField("Name", max_length=2000)
    url = models.URLField("Journal Web Site", max_length=2000, blank=True)
    online = models.BooleanField("Online?")
    active = models.BooleanField("Active?")
    class Meta:
        db_table = u'journals_general'
        verbose_name = "Journal General"
        ordering = ['code']
    def __unicode__(self):
        return self.name

My problem is that in the DB (Postgres) the name of the sequence connected to jid is not journals_general_jid_seq as expected by Django but it has a different name.

Is there a way to specify which sequence Django has to use for an AutoField? In the documentation I read I was not able to find an answer.

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

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

发布评论

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

评论(2

深海少女心 2024-09-02 11:55:17

我不这么认为,但是有一张票(带有可能有用的补丁?):

http://code.djangoproject.com/ticket/1946

编辑:

实际上,上面的链接有 HM 在线程上的评论,提供了比补丁本身更好的解决方案。该补丁是旧的,我不确定它是否适用于您正在使用的 Django 版本。

I don't think so, but there is a ticket open for that (with a possibly useful patch?):

http://code.djangoproject.com/ticket/1946

Edit:

Actually, that link above has a comment from HM on the thread with a better solution than the patch itself. The patch is old, I'm unsure if it will be applicable to the version of Django you are using.

辞旧 2024-09-02 11:55:17

Django 中的这个补丁怎么样: http://code.djangoproject.com/ticket/8901 ?您真的需要序列的名称吗?从版本 8.2 开始,PostgreSQL 具有 RETURNING,您可以在 INSERT(以及 UPDATE 和 DELETE)中使用它来获取创建的 id:

INSERT INTO foo(bar) VALUES('John') RETURNING id;

我不知道 Django 是否可以处理这个(获取 INSERT 结果),但它是一个好东西,对性能也有好处。

What about this patch in Django: http://code.djangoproject.com/ticket/8901 ? And do you realy need the name of the sequence? As of version 8.2, PostgreSQL has RETURNING that you can use in INSERT's (and UPDATE and DELETE) that can be used to get the created id:

INSERT INTO foo(bar) VALUES('John') RETURNING id;

I have no idea if Django can handle this (fetching an INSERT-result), but it's a nice thing and also good for performance.

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