Django not recognizing library

Hello, I’m using GeoDjango for one the apps in my Django project. Anyway, it’s not recognizing OSMGeoAdmin in my admin.py here

from django.contrib.auth import get_user_model
from django.contrib import admin
from .models import Homeowner, Arborist, ArboristReview, ServiceType, ArboristCompany
from django.contrib.gis.admin import OSMGeoAdmin

# Get the user model
User = get_user_model()
admin.site.register(User)

# Register your models here.

@admin.register(Homeowner)
class HomeownerAdmin(OSMGeoAdmin):
    list_display = ('bio', 'profile_pic')  # Adjust fields as per your model

@admin.register(Arborist)
class ArboristAdmin(OSMGeoAdmin):
    list_display = ('location', 'arborist_city', 'arborist_state', 'years_experience')  # Adjust as per your model fields

@admin.register(ArboristReview)
class ArboristReviewAdmin(admin.ModelAdmin):
    list_display = ('rating', 'review_text', 'reviewed_on')

@admin.register(ServiceType)
class ServiceTypeAdmin(admin.ModelAdmin):
    list_display = ('name',)  # Single field `name` for service types

@admin.register(ArboristCompany)
class ArboristCompanyAdmin(admin.ModelAdmin):
    list_display = ('company_name', 'company_logo', 'company_city', 'company_state', 'experience', 'website_link')  
  

    # Add many-to-many fields for display if needed
  # Add a horizontal filter widget for many-to-many relationships

Basically it can’t reference this imported OSMGeoAdmin in admin. I installed both sudo aptitude install gdal-bin libgdal-dev

sudo aptitude install python3-gdal

I’m on Ubuntu Desktop and in my Python virtual environment. Here is my model

# Import ServiceType model
from django.contrib.gis.db import models


class ArboristCompany(models.Model):
    company_name = models.CharField(max_length=40)
    location = models.PointField()
    company_logo = models.ImageField(upload_to='company_logo', blank=True, default="")
    company_city = models.CharField(max_length=70, default="")
    company_state = models.CharField(max_length=40, default="")
    experience = models.CharField(max_length=50, default="")
    services = models.CharField(max_length=400, default="")
    website_link = models.URLField(max_length=100, db_index=True, blank=True, null=True)
    
    
    def __str__(self):
        return f'{self.company_name} ArboristCompany'

And finally settings

INSTALLED_APPS = [
    'daphne',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.gis',
    'rest_framework',
    'haystack',
    'django_extensions',
    'corsheaders',
    'rest_framework_simplejwt',
    'django_rest_passwordreset',
    'channels',
    'arborfindr',
    'arborchat',
]

This is the error I get when I run python manage.py makemigrations

Traceback (most recent call last):
  File "/home/corey-james/Arborhub/MyProject/manage.py", line 24, in <module>
    main()
  File "/home/corey-james/Arborhub/MyProject/manage.py", line 19, in main
    execute_from_command_line(sys.argv)
  File "/home/corey-james/venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/home/corey-james/venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 416, in execute
    django.setup()
  File "/home/corey-james/venv/lib/python3.12/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/corey-james/venv/lib/python3.12/site-packages/django/apps/registry.py", line 124, in populate
    app_config.ready()
  File "/home/corey-james/venv/lib/python3.12/site-packages/django/contrib/admin/apps.py", line 27, in ready
    self.module.autodiscover()
  File "/home/corey-james/venv/lib/python3.12/site-packages/django/contrib/admin/__init__.py", line 52, in autodiscover
    autodiscover_modules("admin", register_to=site)
  File "/home/corey-james/venv/lib/python3.12/site-packages/django/utils/module_loading.py", line 58, in autodiscover_modules
    import_module("%s.%s" % (app_config.name, module_to_search))
  File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/home/corey-james/Arborhub/MyProject/arborfindr/admin.py", line 4, in <module>
    from django.contrib.gis.admin import OSMGeoAdmin
ImportError: cannot import name 'OSMGeoAdmin' from 'django.contrib.gis.admin' (/home/corey-james/venv/lib/python3.12/site-packages/django/contrib/gis/admin/__init__.py)

Hello there!
This is probably happening because you’re using OSMGeoAdmin, that was deprecated on Django 4.0 and removed on Django 5.0.

Have you upgraded your project to Django 5.0? If so, then you can read more about the deprecation of this class and use the GISModelAdmin class instead. Or if that’s not an option, go back to the version that supported it (< 5.0)

No I haven’t, so if I upgrade to 5.0, will I still be able to use OSMGeoAdmin for GeoDjango?

Basically I want to know if I upgrade with pip install --upgrade django==5.0, will it mess up the other dependencies within my Django project?

It’s likely, but without knowing which are the dependencies, it’s hard to tell.

I see that you haven’t even clicked on the links that I’ve linked. If you do upgrade, you won’t be able to use that specific class, but will be able to the other one that I’ve mentioned.

It will be hard to give any following instructions without seeing anymore details. Which version of django are you using right now? You can check this information with the following command (it looks like you’re using linux):
pip freeze | grep Django

or:
python3 -m pip freeze | grep Django

Django==5.1.7

So it looks like I always had version 5 or higher

So you say OSMGeoAdmin was deprecated in Django earlier version, but I have 5.1.7.

So what do you suggest I do?

Again, you haven’t read the links that I’ve sent you. There are the instructions.