fix: lazy import ArrayField to avoid requiring psycopg2#129
Open
zkksdk wants to merge 1 commit into
Open
Conversation
ArrayField was unconditionally imported from django.contrib.postgres.fields, which requires psycopg2 even when not using PostgreSQL. This causes ModuleNotFoundError when using django-seed with non-PostgreSQL databases. Use try/except ImportError pattern (same as JSONField in this file), and guard the isinstance check for when ArrayField is unavailable. Fixes Brobin#100
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix for Issue #100
Problem
django_seed/guessers.pyunconditionally importsArrayFieldfromdjango.contrib.postgres.fields, which requires thepsycopg2package even when the project is not using PostgreSQL as its database. This causesModuleNotFoundError: No module named 'psycopg2'when runningseed.pyon projects using SQLite or MySQL.Root Cause
Fix
Use
try/except ImportError(same pattern already used forJSONFieldin this file), and guard theisinstancecheck for whenArrayFieldis unavailable:And in the
isinstancecheck:Testing
Run
seed.pyon a Django project that does NOT havepsycopg2installed and does NOT use PostgreSQL. Previously this failed withModuleNotFoundError. Now it works.Fixes #100