2

I am trying to solve this problem whole day. My code is shown below. Actually, I created my tables in Pgadmin4. I created the table "atmpokhara" having 10 columns with id field as varchar(20) datatype. After that, I drop the id filed from my atmpokhara table. I render this table to django model using python manage.py inspectdb > models.py

All the row are already in tables. Everything is going good, but when I try to open my table inside my admin panel, I get following error:

operator does not exist: character varying = bigint
LINE 1: ...number" FROM "atmpokhara" WHERE "atmpokhara"."id" = 57173664...
                                                             ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.
Request Method: GET
Request URL:    http://localhost:8000/admin/webmap/atmpokhara/5717366409/change/
Django Version: 2.1.7
Exception Type: ProgrammingError
Exception Value:    
operator does not exist: character varying = bigint
LINE 1: ...number" FROM "atmpokhara" WHERE "atmpokhara"."id" = 57173664...
                                                             ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.
Exception Location: D:\coding time\Django+ GeoDjango\Django Enviroment\venv\lib\site-packages\django\db\backends\utils.py in _execute, line 85
Python Executable:  D:\coding time\Django+ GeoDjango\Django Enviroment\venv\Scripts\python.exe
Python Version: 3.7.2
Python Path:    
['C:\\Users\\Tekson\\Desktop\\Web-GIS-Django\\Project',
 'C:\\Program Files\\Hexagon\\ERDAS IMAGINE '
 '2015\\usr\\lib\\Win32Release\\python',
 'C:\\Users\\Tekson\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',
 'C:\\Users\\Tekson\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',
 'C:\\Users\\Tekson\\AppData\\Local\\Programs\\Python\\Python37\\lib',
 'C:\\Users\\Tekson\\AppData\\Local\\Programs\\Python\\Python37',
 'D:\\coding time\\Django+ GeoDjango\\Django Enviroment\\venv',
 'D:\\coding time\\Django+ GeoDjango\\Django '
 'Enviroment\\venv\\lib\\site-packages']
Server time:    Tue, 16 Jul 2019 12:23:38 +0000

Please help me to findout the solution.

models.py file

from django.contrib.gis.db import models as gis_models

# Create your models here.
class point(models.Model):
    title = models.CharField(max_length=30)
    location = gis_models.PointField(srid=4326)

    def __str__(self):
        return self.title

class Atmpokhara(models.Model):
    bankname = models.CharField(max_length=150, blank=True, null=True)
    nepalibankname = models.CharField(max_length=150, blank=True, null=True)
    address = models.CharField(max_length=150, blank=True, null=True)
    email = models.CharField(max_length=50, blank=True, null=True)
    banktype = models.CharField(max_length=30, blank=True, null=True)
    source = models.CharField(max_length=50, blank=True, null=True)
    operator = models.CharField(max_length=150, blank=True, null=True)
    geom = gis_models.PointField(blank=True, null=True)
    phonenumber = models.CharField(max_length=50, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'atmpokhara'

my admin.py file:

from .models import point,Atmpokhara
from leaflet.admin import LeafletGeoAdmin

# Register your models here.
class pointsAdmin(LeafletGeoAdmin):
    list_display = ('title', 'location')

class AtmpokharaAdmin(LeafletGeoAdmin):
    list_display = ('bankname', 'address', 'banktype')

admin.site.register(point,pointsAdmin)
admin.site.register(Atmpokhara, AtmpokharaAdmin)

my views.py:

from django.core.serializers import serialize
from django.http import HttpResponse
from .models import Atmpokhara

# Create your views here.
def index(request):
    return render(request, 'pages/index.html')

def atm(request):
    atmData = serialize('geojson', Atmpokhara.objects.all())
    return HttpResponse(atmData, content_type='json')

my url.py file:

from . import views
urlpatterns = [
    path('', views.index, name='webmap'),
    path('data/atm', views.atm, name= 'atm'),
]
Tek Kshetri
  • 2,129
  • 1
  • 17
  • 41
  • I think the issue is **id field's type**, change the id field type to integer and test again – hassanzadeh.sd Jul 16 '19 at 13:48
  • 1
    if doesn't solve, delete all file in your migrations directory expect __init__.py and migrate again. – hassanzadeh.sd Jul 16 '19 at 13:50
  • I already tried it, It doesn't work. My model.py file doesn't contain any id filed. But it showing error. I have no idea what is wrong with my code. I read that the drop column in sql doesn't remove the table permanently (https://stackoverflow.com/questions/43049990/drop-column-doesnt-remove-column-references-entirely-postgresql). I think this cause the error, But I am unable to solve this error. – Tek Kshetri Jul 16 '19 at 14:56
  • I suggest to you create another primary key in your model and test again. you must have one primary key in your model : import uuid id=models.UUIDField(primary_key=True, default=uuid.uuid4, help_text="Unique ID for this book") – hassanzadeh.sd Jul 16 '19 at 16:23
  • Thank you for your suggestion! Actually it also not solve my problem. The main admin panel shows all 444 data but when I clicked one of them, it shows the error message: `Atmpokhara with ID "5717366409" doesn't exist. Perhaps it was deleted?` – Tek Kshetri Jul 16 '19 at 18:39
  • you can execute Select query with '5717366409' IDNumber in your database and see actually your data was deleted or not, I think admin panel inheritance from detailView class when to show items and you must send pk correct number with correct type to this class, also I think your problem is your ID number value Or Type. – hassanzadeh.sd Jul 17 '19 at 06:04
  • Thank you for your cooperation! Finally I solve the issue. I won't able to solve this error without you. First I drop id filed from my table in sql, then I added the id filed with serial datatype and set this as a primary key, then I also delete all the migrations. After all of that I type `python manage.py makemigrations` and `python manage.py migrate`. – Tek Kshetri Jul 17 '19 at 06:49
  • yes bro , in addition , In the database: DELETE FROM django_migrations WHERE app = 'app_name'. with this delete all migrate log in your database. – hassanzadeh.sd Jul 17 '19 at 07:53

0 Answers0