[Fixed]-Postgresql django: how to store an array of instances of variable type?


As discussed, a separate model is a feasible way to go to record all the fragments. We use one IntegerField to record the fragment order, so that later one the whole Post could be recovered.

Some caveats here:

  1. Use order_by, latest or slice n dice operations to sort/find elements.
  2. When insert/delete operations are needed, it’s going to break the overall sequence. We need to increase/decrease multiple elements to maintain the order. Use queryset and F() expression to change multiple records at once, like described in another SO post here.

There are some imperfections about the approach, but It’s the best solution I could come up so far(I encountered similar situation before). Linked list is a good way but it’s not database-friendly, as to get all fragments we need O(n) operations instead of O(1) with queryset.

Leave a comment