没有运算符匹配您可能需要添加显式类型强制转换django postgres

时间:2019-07-02 06:18:43

标签: django postgresql

当我尝试domain.com/artist/3时,我尝试基于id访问artist_type时出现此错误:

operator does not exist: character varying = integer
LINE 1: ...main_site_artist" WHERE "main_site_artist"."artist_type" = 3
                                                                ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.

正如我在表中看到的那样,有2个ID为3的artist_type。 因为我正在根据artist_type过滤艺术家

这是models.py:

from django.db import models

class artist(models.Model):
  CHOICES = (
      (0, 'celebrities'),
      (1, 'singer'),
      (2, 'comedian'),
      (3, 'dancer'),
      (4, 'model'),
      (5, 'Photographer')
  )
  artist_name = models.CharField(max_length = 50)
  artist_type = models.IntegerField(choices = CHOICES)
  description = models.TextField(max_length = 500)

  def __str__(self):
    return self.artist_name

这是我的数据库表:

celeb=# select * from main_site_artist;

id | artist_name | artist_type |  description   
----+-------------+-------------+----------------
1 | test        | 1           | test
2 | aka         | 3           | aka
3 | test2       | 4           | aka
4 | sonu        | 3           | aka moth
5 | norma       | 0           | its norma here

urls.py:

from django.urls import path
from . import views



urlpatterns = [
 path('',views.index, name='mainsite-index'),
 path('about/',views.about, name='mainsite-about'),
 path('contact/', views.contact, name='mainsite-contact'),
 path('artist/<int:artist_type>/',views.talent, name='mainsite-talent'),
 path('book_artist/', views.artist_booking, name="artist_book")

]

views.py:

 def talent(request, artist_type):
        artists = artist.objects.filter(artist_type = int(artist_type))
        context = {
                 'aka': artists
        }
        return render(request, 'main_site/talent.html', context)

这是base.html,我将在其中单击:

 <ul class="dropdown-menu nav-font" style="margin-top: 7px">
            <li> <a href={% url 'mainsite-talent' artist_type='0' %} style="color:grey;padding-left:5px">Celebrities</a></li>
             <li> <a href={% url 'mainsite-talent'  artist_type='1' %} style="color:grey;padding-left:5px">Singer</a></li>
             <li> <a href={% url 'mainsite-talent' artist_type='2' %} style="color:grey;padding-left:5px">Comedian</a></li>
             <li> <a href={% url 'mainsite-talent' artist_type='3' %} style="color:grey;padding-left:5px">Dancer</a></li>
             <li> <a href={% url 'mainsite-talent' artist_type='4' %} style="color:grey;padding-left:5px">Model</a></li>
             <li> <a href={% url 'mainsite-talent' artist_type='5' %} style="color:grey;padding-left:5px">Photographer</a></li>
                                 </ul>

现在当我访问localhost:8000时出现此错误:

Reverse for 'mainsite-talent' with no arguments not found. 1 pattern(s) tried: ['artist/(?P<artist_type>[0-9]+)/$']

并使用localhost:8000 / artist / 3仍然是相同的错误:

You might need to add explicit type casts.

0 个答案:

没有答案