0
首先我知道有類似的問題已經被問到(我看過他們),但他們都沒有回答我的問題。如果這個問題已經回答,請給我發送鏈接,我會刪除這一個。創建一個列表並與QuerySet進行比較
所以我有這個功能,我想比較一個Word在列表/ QuerySets中的使用頻率。由於沒有相同的結構,我不能這樣做。
productArr = self.tags.all() # is a QuerySet
CustomerArr = self.user.userprofile.I_want.split(",")[:3]
productArr:(<QuerySet [<Tag: and>, <Tag: Jeans>, <Tag: Love>, <Tag: Leg>]
, CustomerArr:[u'Funny', u' and', u' 1234']
Model for CustomerArr:
class UserProfile(models.Model):
I_want = models.CharField(max_length=50, default=‚example‘)
Model for productArr:
class Product(models.Model):
tags = TaggableManager(verbose_name="tags",blank=False)
…
CustomerArr = self.user.userprofile.I_want.split(",")[:3]
productArr = self.tags.all()
def func(a, b):
global favVal
favVal = 0
for i in a:
if i in b:
#print('hey')
favVal += 1
return False
func(productArr, CustomerArr)
所以我試圖productArrlist = list(productArr)
和類似的東西,使這兩個對比,但沒有奏效。我希望有人能幫助我。
問候
我以前看到這個,它並沒有爲我工作。當我在productArr上這樣做時,我仍然得到一個querySet,並在CustomerArr上它不可用(我在管理中沒有得到任何結果)。 – hansTheFranz
這沒有工作的原因。你在productArr上做了什麼? 你能分享你的模特嗎?你在productArr中除了什麼? – Wilfried
請看我最新的答案。在productArr中,我有一個由用戶提供的標籤列表。在CustomerArr中,我有一個正常字符串的列表。我只是想,也許我應該將CustomerArr也轉換爲標籤,因此它也是一個querySet ... – hansTheFranz