'QuerySet' 对象没有属性 '_meta',Django 有 ajax
'QuerySet' object has no attribute '_meta', Django with ajax
我正在为一家纹身店建立网站。在我的 index
视图中,我首先显示每位艺术家的所有纹身,但我试图让用户按艺术家过滤纹身,然后使用 jquery/ajax.[=26= 更新显示所选艺术家纹身的页面]
Artist
和 Tattoo
型号:
class Artist(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
# etc
class Tattoo(models.Model):
artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
name = models.CharField(max_length=150)
image = models.ImageField(upload_to='images/')
date_created = models.DateTimeField(auto_now_add=True)
forms.py:
class FilterArtForm(ModelForm):
class Meta:
model = Tattoo
fields = ['artist']
widgets = {
'artist': forms.Select(attrs={'id': 'filter_by_artist'}),
}
views.py:
def index(request):
tattoos = Tattoo.objects.order_by('-date_created')
filter_art_form = FilterArtForm()
artist_id = request.GET.get('artist')
if artist_id:
artist = Artist.objects.get(pk = artist_id)
tattoos = tattoos.filter(artist=artist) # trying to return this as a JsonResponse is causing some kind of issue
return JsonResponse({'tattoos': model_to_dict(tattoos)}, status=200)
return render(request, 'core/index.html', {'tattoos':tattoos, 'filter_art_form': filter_art_form})
我是使用 ajax 的新手,所以我只是在摆弄我的观点,如果我尝试 return 只有 Artist
对象本身:
def index(request):
tattoos = Tattoo.objects.order_by('-date_created')
filter_art_form = FilterArtForm()
artist_id = request.GET.get('artist')
if artist_id:
artist = Artist.objects.get(pk = artist_id)
return JsonResponse({'artist': model_to_dict(artist)}, status=200)
return render(request, 'core/index.html', {'tattoos':tattoos, 'filter_art_form': filter_art_form})
这会起作用,我会得到以下响应:
{"artist": {"id": 1, "first_name": "First", "last_name": "Last"}}
但是当我尝试检索与上述 Artist
相关联的 Tattoo
对象时,我得到了这个 'QuerySet' object has no attribute '_meta'
错误。我认为这与 Tattoo
模型的 ImageField
有关,或者与 returning 多个对象而不只是一个对象的事实有关,但我不确定。感谢您对此的任何帮助。
这样试试
if artist_id:
tattoos = tattoos.filter(artist__id=artist_id).values()
return JsonResponse({'tattoos_by_artist': list(tattoos)}, status=200)
我正在为一家纹身店建立网站。在我的 index
视图中,我首先显示每位艺术家的所有纹身,但我试图让用户按艺术家过滤纹身,然后使用 jquery/ajax.[=26= 更新显示所选艺术家纹身的页面]
Artist
和 Tattoo
型号:
class Artist(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
# etc
class Tattoo(models.Model):
artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
name = models.CharField(max_length=150)
image = models.ImageField(upload_to='images/')
date_created = models.DateTimeField(auto_now_add=True)
forms.py:
class FilterArtForm(ModelForm):
class Meta:
model = Tattoo
fields = ['artist']
widgets = {
'artist': forms.Select(attrs={'id': 'filter_by_artist'}),
}
views.py:
def index(request):
tattoos = Tattoo.objects.order_by('-date_created')
filter_art_form = FilterArtForm()
artist_id = request.GET.get('artist')
if artist_id:
artist = Artist.objects.get(pk = artist_id)
tattoos = tattoos.filter(artist=artist) # trying to return this as a JsonResponse is causing some kind of issue
return JsonResponse({'tattoos': model_to_dict(tattoos)}, status=200)
return render(request, 'core/index.html', {'tattoos':tattoos, 'filter_art_form': filter_art_form})
我是使用 ajax 的新手,所以我只是在摆弄我的观点,如果我尝试 return 只有 Artist
对象本身:
def index(request):
tattoos = Tattoo.objects.order_by('-date_created')
filter_art_form = FilterArtForm()
artist_id = request.GET.get('artist')
if artist_id:
artist = Artist.objects.get(pk = artist_id)
return JsonResponse({'artist': model_to_dict(artist)}, status=200)
return render(request, 'core/index.html', {'tattoos':tattoos, 'filter_art_form': filter_art_form})
这会起作用,我会得到以下响应:
{"artist": {"id": 1, "first_name": "First", "last_name": "Last"}}
但是当我尝试检索与上述 Artist
相关联的 Tattoo
对象时,我得到了这个 'QuerySet' object has no attribute '_meta'
错误。我认为这与 Tattoo
模型的 ImageField
有关,或者与 returning 多个对象而不只是一个对象的事实有关,但我不确定。感谢您对此的任何帮助。
这样试试
if artist_id:
tattoos = tattoos.filter(artist__id=artist_id).values()
return JsonResponse({'tattoos_by_artist': list(tattoos)}, status=200)