I would like to have a Custom User Model in my django application, featuring a column to hold reference to Customer objects coming from stripe, being synced via dj-stripe.
So my approach was to extend as follows:
from django.contrib.auth.models import AbstractUser, UserManager
from django.db import models
class StripeUserModel(AbstractUser):
objects = UserManager()
customer = models.ForeignKey("djstripe.Customer", null=True, blank=True, on_delete=models.SET_NULL)
Setting the standard django User model to:
AUTH_USER_MODEL = "billing_stripe_app.StripeUserModel"
When I start with a clean new database by deleting the SQLite file and running python manage.py makemigrations
, I observe following error:
django.db.migrations.exceptions.CircularDependencyError: billing_stripe_app.0001_initial, djstripe.0001_initial, djstripe.0008_2_5, djstripe.0009_2_6, djstripe.0010_alter_customer_balance, djstripe.0011_2_7
Without the customer field in StripeUserModel
, everything works.
I wonder how to resolve this. I have run into circular dependency in Python before.
It completely makes sense. In my application (billing_stripe_app), I reference dj-stripe customer field. And apparently this one relies on the AUTH_USER_MODEL user model itself again, creating that circular dep, as can be seen from the djstripe migration file 0001_initial.py (excerpt below).
...
DJSTRIPE_SUBSCRIBER_MODEL: str = getattr(
settings, "DJSTRIPE_SUBSCRIBER_MODEL", settings.AUTH_USER_MODEL
) # type: ignore
# Needed here for external apps that have added the DJSTRIPE_SUBSCRIBER_MODEL
# *not* in the '__first__' migration of the app, which results in:
# ValueError: Related model 'DJSTRIPE_SUBSCRIBER_MODEL' cannot be resolved
# Context: https://github.com/dj-stripe/dj-stripe/issues/707
DJSTRIPE_SUBSCRIBER_MODEL_MIGRATION_DEPENDENCY = getattr(
settings, "DJSTRIPE_SUBSCRIBER_MODEL_MIGRATION_DEPENDENCY", "__first__"
)
DJSTRIPE_SUBSCRIBER_MODEL_DEPENDENCY = migrations.swappable_dependency(
DJSTRIPE_SUBSCRIBER_MODEL
)
if DJSTRIPE_SUBSCRIBER_MODEL != settings.AUTH_USER_MODEL:
DJSTRIPE_SUBSCRIBER_MODEL_DEPENDENCY = migrations.migration.SwappableTuple(
(
DJSTRIPE_SUBSCRIBER_MODEL.split(".", 1)[0],
DJSTRIPE_SUBSCRIBER_MODEL_MIGRATION_DEPENDENCY,
),
DJSTRIPE_SUBSCRIBER_MODEL,
)
...
Any ideas on how to approach this? If you look at this guide on saaspegasus, it should be completely fine to go the way of extending the User model with a reference to a Customer
.
Version Info:
Django 4.1.6
dj-stripe 2.7.3
stripe 4.2.0
python 3.11