python - Ordered lists of generic foreign keys in Django -
i have app hierarchical course, note , task models. is, each course has bunch of note instances pointing foreignkey, , each note has bunch of task instances pointing it.
i want content in specific order, added sequence
attribute course , note via parent model. allows them hold ordered list of pk's of linked instances (e.g. [1, 4, 6, 2, 3]), can edit order of.
i added global_sequence
course, holds list of ('class_name', pk) tuples strings e.g. ["('note', 1)", "('task', 19)", "('task', 31)", "('note', 2)", "('task', 22)",].
class sequence(models.model): sequence = django.contrib.postgres.fields.arrayfield(models.integerfield()) class course(sequence): title = models.charfield(max_len=80) global_sequence = django.contrib.postgres.fields.arrayfield( models.charfield(max_len=30) ) class note(sequence): body = models.textfield() course = models.foreignkey(course, related_name='content') class task(models.model): q = models.charfield(max_len=100) = models.charfield(max_len=100) note = models.foreignkey(note, related_name='content')
i feel approaches building sequence
, global_sequence
may naive since sequences have manually updated whenever linked instance created or deleted (i using signals this). also, strings in global_sequence
error-prone , have use eval() them back, arraylist can't store tuples. have considered using contenttype
model, don't know how use in ordered list.
my question is: is there less error-prone or safer (i.e. non-eval()) way keep linked instances - of identical or mixed models - in specific order?
the closest thing found this, says ordered list of integers not terrible if not updated, sequences updated frequently.
Comments
Post a Comment