Django error on forms when running makemigrations

I'm getting the following error when trying to make migrations for my models. This is against a clean DB so it is trying to generate the initial migrations.

File "/Users/luketimothy/Library/Mobile Documents/com~apple~CloudDocs/LifePlanner/LifePlanner/LifePlanner/urls.py", line 20, in <module>
    from . import views
  File "/Users/luketimothy/Library/Mobile Documents/com~apple~CloudDocs/LifePlanner/LifePlanner/LifePlanner/views.py", line 7, in <module>
    from .forms import AppUserForm, IncomeSourceForm, AccountForm, SpouseForm, DependentForm
  File "/Users/luketimothy/Library/Mobile Documents/com~apple~CloudDocs/LifePlanner/LifePlanner/LifePlanner/forms.py", line 32, in <module>
    class AccountForm(forms.ModelForm):
  File "/Users/luketimothy/Library/Mobile Documents/com~apple~CloudDocs/LifePlanner/LifePlanner/ProjectEnv/lib/python3.11/site-packages/django/forms/models.py", line 312, in __new__
    fields = fields_for_model(
             ^^^^^^^^^^^^^^^^^
  File "/Users/luketimothy/Library/Mobile Documents/com~apple~CloudDocs/LifePlanner/LifePlanner/ProjectEnv/lib/python3.11/site-packages/django/forms/models.py", line 237, in fields_for_model
    formfield = f.formfield(**kwargs)
                ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/luketimothy/Library/Mobile Documents/com~apple~CloudDocs/LifePlanner/LifePlanner/ProjectEnv/lib/python3.11/site-packages/django/db/models/fields/related.py", line 1165, in formfield
    "queryset": self.remote_field.model._default_manager.using(using),
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'using'

This is the form in my forms.py:

class AccountForm(forms.ModelForm):
    class Meta:
        model = Account
        fields = ['account_name','owner','account_provider','balance']
        labels = {'account_name': 'Name','account_provider': 'Account Provider','balance': 'Balance','owner': 'Owner'}

And here are the relevant Models in models.py:

class Projectable(models.Model):
    class Meta:
        abstract = True

    def project_annual_values(self, years):
        raise NotImplementedError("Subclasses should implement this method.")

    def project_monthly_values(self, months):
        raise NotImplementedError("Subclasses should implement this method.")

class AccountProvider(models.Model):
    name = models.CharField(max_length=64)
    web_url = models.CharField(max_length=256)
    login_url = models.CharField(max_length=256)
    logo_file= models.CharField(max_length=64)
    
    def __str__(self):
        return f"{self.name}"

class Account(Projectable):
    account_name = models.CharField(max_length=100)
    balance = models.DecimalField(max_digits=15, decimal_places=2)
    owner = models.ForeignKey(Agent, on_delete=models.CASCADE)
    account_provider = models.ForeignKey(AccountProvider, on_delete=models.SET_NULL)

    def get_balance(self):
        return self.balance

    def get_account_type(self):
        raise NotImplementedError("Subclasses should implement this method.")

I have searched all over and I cannot quite find any example of the same issue. I think it might be the way I have the inheritance set up on the models but I'm not really sure, as far as I'm aware this should work.

It is happening because for account provider field in account model, on on_delete=models.SET_NULL, you are setting field value to null on deletion, but you haven't specified if null values are allowed. By default django doesn't allow null values to fields, but you can do it by adding parameter null=True before on_delete=models.SET_NULL to make it work.

class Account(Projectable):
    account_name = models.CharField(max_length=100)
    balance = models.DecimalField(max_digits=15, decimal_places=2)
    owner = models.ForeignKey(Agent, on_delete=models.CASCADE)
    account_provider = models.ForeignKey(AccountProvider, null=True, on_delete=models.SET_NULL)

    def get_balance(self):
        return self.balance

    def get_account_type(self):
        raise NotImplementedError("Subclasses should implement this method.")

Back to Top