姜戈 + Postgres:如何指定字段的顺序
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不这么认为,但是有一张票(带有可能有用的补丁?):
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.
Django 中的这个补丁怎么样: http://code.djangoproject.com/ticket/8901 ?您真的需要序列的名称吗?从版本 8.2 开始,PostgreSQL 具有 RETURNING,您可以在 INSERT(以及 UPDATE 和 DELETE)中使用它来获取创建的 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:
I have no idea if Django can handle this (fetching an INSERT-result), but it's a nice thing and also good for performance.