参考字段的相对增量ID [英] Relative incremental ID by reference field

查看:93
本文介绍了参考字段的相对增量ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子来存储某些事件的预约;相关部分是:

  class Reservation(models.Model):
#django创建自动增量字段id默认
event = models.ForeignKey(Event)

#某些其他预留专用字段..
first_name = models.CharField(max_length = 255)

现在,我想检索相对于同一事件的预留的给定预留的顺序ID。 p>

免责声明:当然,我们假设保留从不删除,或者他们的相对位置可能会改变。

示例:



  + ---- + ------- + ------------ + -------- + 
| ID |事件|名字| Rel.ID |
+ ---- + ------- + ------------ + -------- +
| 1 | 1 | AAA | 1 |
| 2 | 1 | BBB | 2 |
| 3 | 2 | CCC | 1 |
| 4 | 2 | DDD | 2 |
| 5 | 1 | EEE | 3 |
| 6 | 3 | FFF | 1 |
| 7 | 1 | GGG | 4 |
| 8 | 1 | HHH | 5 |
+ ---- + ------- + ------------ + -------- +

最后一列是相同事件的所有保留的相对ID,即没有间隙的顺序号。 p>

现在,没有必要手动计算每个导入的相对ID(我不喜欢),最好的方法是什么?我使用postgresql作为底层数据库,但是我宁愿坚持使用django抽象层来保持这种便携式(即没有数据库特定的解决方案,比如触发器等)。

解决方案

我讨厌总是回应自己的问题,但是我解决了这个问题:

  class Reservation(models.Model):

#...

def relative_id(self):
return self.id - Reservation.objects.filter(id__lt = self.id).filter(〜Q(event = self.event))。all()。count()


我在想任何缺点,但我没有找到任何。


I have a table to store reservations for certain events; relevant part of it is:

class Reservation(models.Model):
    # django creates an auto-increment field "id" by default
    event = models.ForeignKey(Event)

    # Some other reservation-specific fields..
    first_name = models.CharField(max_length=255)

Now, I wish to retrieve the sequential ID of a given reservation relative to reservations for the same event.

Disclaimer: Of course, we assume reservations are never deleted, or their relative position might change.

Example:

+----+-------+------------+--------+
| ID | Event | First name | Rel.ID |
+----+-------+------------+--------+
|  1 |     1 | AAA        |      1 |
|  2 |     1 | BBB        |      2 |
|  3 |     2 | CCC        |      1 |
|  4 |     2 | DDD        |      2 |
|  5 |     1 | EEE        |      3 |
|  6 |     3 | FFF        |      1 |
|  7 |     1 | GGG        |      4 |
|  8 |     1 | HHH        |      5 |
+----+-------+------------+--------+

The last column is the "Relative ID", that is, a sequential number, with no gaps, for all reservations of the same event.

Now, what's the best way to accomplish this, without having to manually calculate relative id for each import (I don't like that)? I'm using postgresql as underlying database, but I'd prefer to stick with django abstraction layer in order to keep this portable (i.e. no database-specific solutions, such as triggers etc.).

解决方案

I hate always being the one that responds its own questions, but I solved using this:

class Reservation(models.Model):

    # ...

    def relative_id(self):
        return self.id - Reservation.objects.filter(id__lt=self.id).filter(~Q(event=self.event)).all().count()

Assuming records from reservations are never deleted, we can safely assume the "relative id" is the incremental id - (count of reservations before this one not belonging to same event).

I'm thinking of any drawbacks, but I didn't find any.

这篇关于参考字段的相对增量ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆