2017-02-21 39 views
0

我有一個數據庫,其中的字段是「僞」array。這個array保存整數值。我的實現如下:Django檢查整數是否存在於數據庫字段數組中

attendees = models.TextField(null=True) # declaring the integer array 

當我說假,我的意思是,我使用json,使之成爲一個數組。

attendees=json.dumps(members) 

現在attendees列將包含這樣的事情["1", "2", "3"]

所以我要檢查,如果參加者將包含的值,例如「1」。本質上,我想要這樣的事情:

eventList = Events.objects.all().filter(user_id in Event.attendees) # I know this isn't the correct syntax 

有關如何儘可能有效地做到這一點的任何想法?

回答

0

你需要使用__contains

.filter(attendees__contains='"{}"'.format(user_id)) 

雖然仍然是問題,爲什麼這不是一個單獨的模型或JSONField/ArrayField ...

相關問題