Getting 'No field found in Account with column name ""' when creating data for project using django-multitenant library

I'm trying to create a data migration script to insert initial data

I have a migration file as below

Account = apps.get_model("quickstart", "Account") 
Account.objects.using(db_alias).bulk_create([   
    Account(name="johndoe", domain="domain", subdomain="subdomain",     country=Country.objects.get(name="USA")),             
    Account(name="jilldoe", domain="domain", subdomain="subdomain", country=Country.objects.get(name="USA")),             
    Account(name="milldoe", domain="domain", subdomain="subdomain", country=Country.objects.get(name="USA")),             
    Account(name="velidoe", domain="domain", subdomain="subdomain", country=Country.objects.get(name="Turkiye")),             
    Account(name="alidoe", domain="domain", subdomain="subdomain", country=Country.objects.get(name="Turkiye")),             
    Account(name="pierredoe", domain="domain", subdomain="subdomain", country=Country.objects.get(name="France")),         
    ])

When I execute ./manage migrate I got the error below

Traceback (most recent call last):
File "/home/gurkanindibay/tenants/tests/django-multi-tenant/tutorial/manage.py", line 22, in \<module\>
main()
File "/home/gurkanindibay/tenants/tests/django-multi-tenant/tutorial/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/gurkanindibay/tenants/tests/django-multi-tenant/tutorial/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
utility.execute()
File "/home/gurkanindibay/tenants/tests/django-multi-tenant/tutorial/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/gurkanindibay/tenants/tests/django-multi-tenant/tutorial/venv/lib/python3.9/site-packages/django/core/management/base.py", line 402, in run_from_argv
self.execute(\*args, \*\*cmd_options)
File "/home/gurkanindibay/tenants/tests/django-multi-tenant/tutorial/venv/lib/python3.9/site-packages/django/core/management/base.py", line 448, in execute
output = self.handle(\*args, \*\*options)
File "/home/gurkanindibay/tenants/tests/django-multi-tenant/tutorial/venv/lib/python3.9/site-packages/django/core/management/base.py", line 96, in wrapped
res = handle_func(\*args, \*\*kwargs)
File "/home/gurkanindibay/tenants/tests/django-multi-tenant/tutorial/venv/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 349, in handle
post_migrate_state = executor.migrate(
File "/home/gurkanindibay/tenants/tests/django-multi-tenant/tutorial/venv/lib/python3.9/site-packages/django/db/migrations/executor.py", line 135, in migrate
state = self.\_migrate_all_forwards(
File "/home/gurkanindibay/tenants/tests/django-multi-tenant/tutorial/venv/lib/python3.9/site-packages/django/db/migrations/executor.py", line 167, in \_migrate_all_forwards
state = self.apply_migration(
File "/home/gurkanindibay/tenants/tests/django-multi-tenant/tutorial/venv/lib/python3.9/site-packages/django/db/migrations/executor.py", line 252, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/gurkanindibay/tenants/tests/django-multi-tenant/tutorial/venv/lib/python3.9/site-packages/django/db/migrations/migration.py", line 130, in apply
operation.database_forwards(
File "/home/gurkanindibay/tenants/tests/django-multi-tenant/tutorial/venv/lib/python3.9/site-packages/django/db/migrations/operations/special.py", line 193, in database_forwards
self.code(from_state.apps, schema_editor)
File "/home/gurkanindibay/tenants/tests/django-multi-tenant/tutorial/tutorial/quickstart/migrations/0002_initial_data.py", line 95, in forwards_func
TenantNotIdModel(tenant_column=1, name="TenantNotIdModel 1"),
File "/home/gurkanindibay/tenants/tests/django-multi-tenant/tutorial/venv/lib/python3.9/site-packages/django_multitenant/mixins.py", line 58, in __init__
super(TenantModelMixin, self).__init__(\*args, \*\*kwargs)
File "/home/gurkanindibay/tenants/tests/django-multi-tenant/tutorial/venv/lib/python3.9/site-packages/django/db/models/base.py", line 470, in __init__
self.\_state = ModelState()
File "/home/gurkanindibay/tenants/tests/django-multi-tenant/tutorial/venv/lib/python3.9/site-packages/django_multitenant/mixins.py", line 63, in __setattr__
attrname in (self.tenant_field, get_tenant_field(self).name)
File "/home/gurkanindibay/tenants/tests/django-multi-tenant/tutorial/venv/lib/python3.9/site-packages/django_multitenant/utils.py", line 59, in get_tenant_field
raise ValueError(
ValueError: No field found in Account with column name ""

My Account code is as below and tenant_id is defined

class Account(TenantModel):
    name = models.CharField(max_length=255)
    domain = models.CharField(max_length=255)
    subdomain = models.CharField(max_length=255)
    country = models.ForeignKey(Country, on_delete=models.CASCADE)
    tenant_id = "id" 

Even if tenant_id is defined I'm having problems. What could be the problem?

Found out the solution for the problem

When using django-multitenant, we must import classes directly. We shouldn't get the classes using app.get_model since django-multitenant fails to identify the tenant_id column from the class instance returned from app.get_model I removed the app.get_model line and added the import statement as below

from ..models import *
Account.objects.using(db_alias).bulk_create([   
    Account(name="johndoe", domain="domain", subdomain="subdomain",     country=Country.objects.get(name="USA")),             
    Account(name="jilldoe", domain="domain", subdomain="subdomain", country=Country.objects.get(name="USA")),             
    Account(name="milldoe", domain="domain", subdomain="subdomain", country=Country.objects.get(name="USA")),             
    Account(name="velidoe", domain="domain", subdomain="subdomain", country=Country.objects.get(name="Turkiye")),             
    Account(name="alidoe", domain="domain", subdomain="subdomain", country=Country.objects.get(name="Turkiye")),             
    Account(name="pierredoe", domain="domain", subdomain="subdomain", country=Country.objects.get(name="France")),         
    ])
Back to Top