ホームページ内に検索フィールドを含めようとしています。一部のモジュールフィールドで機能します。私の問題は、ForeignKeyフィールドを使用するときです(間違っている場合は訂正してください)。
models.py
class Location(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
my_location = models.CharField(max_length=120, choices=LOCATION_CHOICES)
update_date = models.DateField(auto_now=True, null=True)
def __str__(self):
return self.my_location
class UserProfile(models.Model):
user = models.ForeignKey(User)
# The additional attributes we wish to include.
user_base = models.CharField(max_length=120, choices=LOCATION_CHOICES)
user_position = models.CharField(max_length=120)
user_phone = models.PositiveIntegerField()
def __unicode__(self):
return self.user.username
views.py
def search_by_location(request):
if 'q' in request.GET and request.GET['q']:
q = request.GET['q']
locations = Location.objects.filter(my_location__icontains=q).order_by('-update_date')
else:
locations = Location.objects.order_by('-update_date')
context = {'locations': locations}
return render(request, 'index.html', context)
私の問題は、エラーを受け取るuser
代わりに、フィルタークエリ内で使用する場合ですmy_location
。
関連フィールドの検索が無効です:icontains
トラブルシューティングの方法に関するアドバイスや、私が読むことができるドキュメントを教えてください。
icontains
テキストフィールドでルックアップを使用できます。user
関連(整数)フィールドです。user
使用する代わりにuser__username
。
locations = Location.objects.filter(user__username__icontains=q)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加