django management command: using LabelCommand - django

i want to pass argument along with this management command. i run this code from command line as
python manage.py example1 amita
where example1 is name of my file and amita is argument. on running this i am getting error.i am pasting the traceback:
Traceback (most recent call last):
File "manage.py", line 79, in <module>
execute_manager(settings)
File "/usr/lib/python2.7/dist-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/usr/lib/python2.7/dist-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib/python2.7/dist-packages/django/core/management/__init__.py", line 261, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/usr/lib/python2.7/dist-packages/django/core/management/__init__.py", line 68, in load_command_class
return module.Command()
AttributeError: 'module' object has no attribute 'Command'
the code for example1.py is below
from django.core.management.base import LabelCommand
from django.core.management.base import BaseCommand
def hello(name):
print name
def hello1(name):
print name
class LabelCommand(BaseCommand):
"""
A management command which takes one or more arbitrary arguments
(labels) on the command line, and does something with each of
them.
Rather than implementing ``handle()``, subclasses must implement
``handle_label()``, which will be called once for each label.
If the arguments should be names of installed applications, use
``AppCommand`` instead.
"""
args = '<label label ...>'
label = 'label'
def handle(self, *labels, **options):
if not labels:
raise CommandError('Enter at least one %s.' % self.label)
output = []
for label in labels:
label_output = self.handle_label(label, **options)
if label_output:
output.append(label_output)
return '\n'.join(output)
def handle_label(self, label, **options):
"""
Perform the command's actions for ``label``, which will be the
string as given on the command line.
"""
hello(label)
hello1(label)
raise NotImplementedError()

Django already has a LabelCommand class that you should use:
from django.core.management.base import LabelCommand
Then you just have to override the handle_label command.

Related

Django command throws TypeError: handle() got an unexpected keyword argument

I'm using Django 1.10.4 and Python 3.52. When I try to run a Django command via python manage.py my_command I get the following error:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "path_to_envs/envs/env_name/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "path_to_envs/envs/env_name/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "path_to_envs/envs/env_name/lib/python3.5/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "path_to_envs/envs/env_name/lib/python3.5/site-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
TypeError: handle() got an unexpected keyword argument 'verbosity'
I can run a local django server and interact with the admin pages. The app that contains that command is in my settings.py file.
Below is the contents of the django command:
from django.core.management import BaseCommand
from my_module import MyClass
class Command(BaseCommand):
def handle(self):
my_class = MyClass()
my_class.my_method()
At the time of error, the options dictionary contains {'verbosity': 1, 'no_color': False, 'settings': None, 'pythonpath': None, 'traceback': False}. Depending on the random ordering of the dictionary no_color, traceback, and the others will throw the same TypeError. After a day of googling I still can't figure out what the issue is. Has anyone seen this before?
After lots of googling and pulling my hair out, the issue was an incorrect number of arguments to handle().
This:
def handle(self):
Should be:
def handle(self, *args, **options):
If your command needs no arguments, try a subclass of BaseCommand
NoArgsCommand.handle_noargs(**options)

'FlaskDB' object has no attribute 'sequences'

I tried peewee with flask for two days, but I failed till now. The code is as the follows:
import click
from flask import Flask
from flask.cli import FlaskGroup
from playhouse.flask_utils import FlaskDB
from models import *
from config import config
flask_db = FlaskDB()
def create_app(config_name):
application = Flask(__name__)
application.config.from_object(config[config_name])
flask_db.init_app(application)
flask_db.connect_db()
flask_db.database.create_tables([User])
flask_db.database.close()
#application.route('/')
def index():
return "hello world!"
return application
def create_cli_app(info):
return create_app("develop")
#click.group(cls=FlaskGroup, create_app=create_cli_app)
def cli():
pass
#cli.command()
def initdb():
flask_db.connect_db()
flask_db.database.create_tables([User])
flask_db.database.close()
if __name__ == "__main__":
cli()
When I run it with the CLI: python manage.py run, I got the following errors:
(venv) ➜ /Users/yw/Documents/web git:(master) ✗ p manage.py run
Traceback (most recent call last): File "manage.py", line 46, in
cli() File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/click/core.py",
line 716, in call
return self.main(*args, **kwargs) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/flask/cli.py",
line 345, in main
return AppGroup.main(self, *args, **kwargs) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/click/core.py",
line 696, in main
rv = self.invoke(ctx) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/click/core.py",
line 1060, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/click/core.py",
line 889, in invoke
return ctx.invoke(self.callback, **ctx.params) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/click/core.py",
line 534, in invoke
return callback(*args, **kwargs) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/click/decorators.py",
line 64, in new_func
return ctx.invoke(f, obj, *args[1:], **kwargs) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/click/core.py",
line 534, in invoke
return callback(*args, **kwargs) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/flask/cli.py",
line 388, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading) File
"/Users/yw/Documents/web/venv/lib/python3.4/site-packages/flask/cli.py",
line 124, in init
self._load_unlocked() File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/flask/cli.py",
line 148, in _load_unlocked
self._app = rv = self.loader() File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/flask/cli.py",
line 201, in load_app
rv = self.create_app(self) File "manage.py", line 30, in create_cli_app
return create_app("develop") File "manage.py", line 19, in create_app
flask_db.database.create_tables([User]) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/peewee.py",
line 3765, in create_tables
create_model_tables(models, fail_silently=safe) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/peewee.py",
line 5175, in create_model_tables
m.create_table(**create_table_kwargs) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/peewee.py",
line 4845, in create_table
if db.sequences and pk is not False and pk.sequence: AttributeError: 'FlaskDB' object has no attribute 'sequences'
Indeed, I just want to initiate the DB by using flask.cli tool. As you see, if I use the command “python manage.py initdb”, I can only get the same error output as above.
So what is the meaning of "'FlaskDB' object has no attribute ‘sequences'"? What should I do?
Thanks for your help!
It seems that your User model doesn't have correct database.
With FlaskDB, your User model should inherit FlaskDB().Model
instead of defining class Meta: database = database.
database = FlaskDB()
class User(database.Model):
pass

Correctly import in Django management command

Custom management command, oauth.py, needs a model from another module. When I include "from appname.authentication.models import Contact" I get "AttributeError: 'module' object has no attribute 'models'." - Im stuck on django 1.6 until I able to build an test suite to help with the upgrade.
How do I correctly import Contact?
Other notable SO answers:
Circular Import
Import Settings
Each directory other than /app has an __init__.py . /app is in sys.path/ django directory, /app:
util
-management
--commands
---oauth.py
appname
-authentication
--models.py
extouth.py
extoauth.py is standalone script with the same import and works, but only in manage.py shell. The custom management command will be better.
oauth.py:
import sys
from optparse import make_option
from provider.oauth2.models import Client
from appname.authentication.models import Contact
from django.core.management.base import BaseCommand
class Command(BaseCommand):
help = 'Creates OAUTH user and gets access token.'
option_list = BaseCommand.option_list + (
make_option('--create-client',
dest='create_client',
help='''Returns tuple of <id,secret>...'''),
make_option('--get-access-token',
dest='get_access_token',
help='''Returns time limited access token...'''),
)
def handle(self, *args, **options):
if options['create_client']:
return self.create_client(options['create_client'])
elif options['get_access_token']:
self.get_access_token()
def create_client(self, user):
return user
def get_access_token(self):
pass
Console out:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 75, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
File "/app/waapiutil/management/commands/oauth.py", line 4, in <module>
from wowza.authentication.models import Contact
File "/app/wowza/authentication/models.py", line 80, in <module>
class
SalesforceModel(with_metaclass(salesforce.models.SalesforceModelBase, models.Model)):
AttributeError: 'module' object has no attribute 'models'
hypo - settings is not getting imported
So my settings must be getting set just as they do with the manage.py shell usage because if I include at the top of my file:
from django.conf import settings
settings.configure()
I get:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 75, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
File "/app/waapiutil/management/commands/oauth.py", line 2, in <module>
settings.configure()
File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 89, in configure
raise RuntimeError('Settings already configured.')
RuntimeError: Settings already configured.
hypo - deeper syntax error (that should have broken production anyway)
searching for occurrences of models.model in my app files yields four results, each has the correct capitalization of models.Model.
hypo - Contact is already imported
When I comment out the import and run the command i get:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/app/waapiutil/management/commands/oauth.py", line 23, in handle
return self.create_client(options['create_client'])
File "/app/waapiutil/management/commands/oauth.py", line 32, in create_client
c = Client(user=Contact.objects.get_by_email(e), name=n,
NameError: global name 'Contact' is not defined
Snippets from authentication/models.py for hynekcer's comment
# Core Django imports
from django.db import models
from django.core.validators import MinLengthValidator
from django.utils.six import with_metaclass
# Third-party imports
import pycountry
from rest_framework.compat import oauth2_provider
import salesforce
from salesforce import fields
from salesforce.backend import manager
...
class SalesforceManager(manager.SalesforceManager):
"""
Override the default Salesforce manager so we can get some proper REST framework exceptions
"""
def get(self, *args, **kwargs):
try:
result = self.get_queryset().get(*args, **kwargs)
except self.model.MultipleObjectsReturned:
raise MultipleUniqueRecords()
except Exception as e:
logger.warning("SalesForce exception %s", str(e))
raise NoRecord()
return result
class SalesforceModel(with_metaclass(salesforce.models.SalesforceModelBase, models.Model)):
"""
Abstract model class for Salesforce objects.
"""
_base_manager = objects = SalesforceManager()
_salesforce_object = True
class Meta:
managed = False
abstract = True
Id = fields.SalesforceAutoField(primary_key=True)
def clean_fields(self, *args, **kwargs):
# Override the default clean_fields method so we can catch validation exceptions
try:
super(SalesforceModel, self).clean_fields(*args, **kwargs)
except Exception as validation_exception:
detail = ''
for field, message in validation_exception.error_dict.items():
detail += field + ': ' + message[0].messages[0]
raise ValidationError(detail)
def save(self, *args, **kwargs):
# Override the default save method so we can remove fields that Salesforce manages
if self._meta.model_name in ['contact', 'account', 'license', 'sslcertificate']:
if not self.Id:
for field in self._meta.fields:
if field.attname == 'created_date' or field.attname == 'certificate_id':
self._meta.fields.remove(field)
else:
update_fields = self._meta.get_all_field_names()
remove_list = []
if self._meta.model_name == 'contact':
remove_list = ['created_date', 'accesstoken', 'refreshtoken', 'oauth2_client', 'grant', 'Id', 'entitlement_plan']
elif self._meta.model_name == 'account':
remove_list = ['created_date', 'account', 'Id']
elif self._meta.model_name == 'license':
remove_list = ['created_date', 'Id']
elif self._meta.model_name == 'sslcertificate':
remove_list = ['certificate_id', 'created_date', 'Id']
for remove_field in remove_list:
if remove_field in update_fields:
update_fields.remove(remove_field)
kwargs['update_fields'] = update_fields
# Retry five times if there's a SalesforceError
delay = 1
for retry in range(5):
try:
super(SalesforceModel, self).save(*args, **kwargs)
break
except Exception as e:
logger.error("Saving {0} resulted in an error {1}, retry {2}".format(str(self),str(e),retry))
if retry < 4 and "SERVER_UNAVAILABLE" in str(e):
time.sleep(delay)
delay *= 2
else:
(This is a comment requiring much place, not the answer yet.)
I thought my eyes deceiving when I saw a line from my database backend in your application code, maybe also with a concrete model in the same module. I see that your pasted code is based on an older django-salesforce 0.5.x code. OK, hopefully you use also the package 0.5.x. It probably can not work with the current django-salesforce 0.6 or nobody should expect it
The most safe style is to use the public API. A more fragile style is to use also undocumented features. The most fragile is to copy-paste code from the core of other package and to forget that you started to use a new version. You must have a sufficiently serious reason and enough time if you do such things. (I did it six times for every Django version because the django-salesforce backend is interesting enough for me, but I don't reccomend it to you.)
You can write simply:
class MySalesforceModel(salesforce.models.SalesforceModel):
def ... # your custom method
class Meta:
managed = False
abstract = True
The advantage is that this will use the right current SalesforceModel even with the next version of Django and django-salesforce.
If you really need to customize a backend, please comply a sane separation of the backend and application code to different modules so that the backend can be imported without importing any concrete model. Otherwise you can easy get ugly dependencies. Also a nonempty 'init.py` can cause sily dependencies
You need no hypothesis ("hypo") for such simple things if you have the complete source code. You can simply log (print) that information e.g. put import sys; print("settings imported", "my_project.settings" in sys.modules) or verify that settins are correctly configured - put temporarily a line just before a problematic place: from django.conf import settings; print(settings.INSTALLED_APPS) # any valid name
You should check syntax errors before asking a help. It can be done easily for any specific Python version for a tree of subdirectories e.g. by python2.7 -m compileall $(pwd)
It is very insane to use a general handling except Exception: especially while you are debugging. Imagine that the code between try...except will raise an ImportError and you swallow it.
It can be easier to solve dependencies by refactoring the code than to maintain the fragile code later. Imagine that you import inside a function and you get a strange ImportError message from your friend only sometimes if the module is imported from a thread only in Apache, but it succeeds if you import it from the main thread. Explicit import is favoured.

ImportError being generated when trying to run django-celery worker process

I'm trying to integrate django-celery into an existing site and I'm coming up against an error that I can't seem to get fixed.
For context, I went through the Django first steps and the test project was successful, ie everything worked as it should.
Now, in my existing project, I can't get the celery worker running from the command line:
manage.py celery worker --loglevel=info --settings=myproject.settings.dev_settings
When i run that I get the following stack trace and error:
Traceback (most recent call last):
File "C:\sites\corecrm\manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 453, in execute_from_command_line
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\djcelery\management\commands\celery.py", line 22, in run_from_argv
['%s %s' % (argv[0], argv[1])] + argv[2:],
File "C:\Python27\lib\site-packages\celery\bin\celery.py", line 901, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "C:\Python27\lib\site-packages\celery\bin\base.py", line 187, in execute_from_commandline
return self.handle_argv(prog_name, argv[1:])
File "C:\Python27\lib\site-packages\celery\bin\celery.py", line 893, in handle_argv
return self.execute(command, argv)
File "C:\Python27\lib\site-packages\celery\bin\celery.py", line 868, in execute
return cls(app=self.app).run_from_argv(self.prog_name, argv)
File "C:\Python27\lib\site-packages\celery\bin\celery.py", line 148, in run_from_argv
return self(*args, **options)
File "C:\Python27\lib\site-packages\celery\bin\celery.py", line 118, in __call__
ret = self.run(*args, **kwargs)
File "C:\Python27\lib\site-packages\celery\bin\celery.py", line 220, in run
return self.target.run(*args, **kwargs)
File "C:\Python27\lib\site-packages\celery\bin\celeryd.py", line 153, in run
return self.app.Worker(**kwargs).run()
File "C:\Python27\lib\site-packages\celery\apps\worker.py", line 162, in run
self.app.loader.init_worker()
File "C:\Python27\lib\site-packages\celery\loaders\base.py", line 130, in init_worker
self.import_default_modules()
File "C:\Python27\lib\site-packages\djcelery\loaders.py", line 138, in import_default_modules
self.autodiscover()
File "C:\Python27\lib\site-packages\djcelery\loaders.py", line 141, in autodiscover
self.task_modules.update(mod.__name__ for mod in autodiscover() or ())
File "C:\Python27\lib\site-packages\djcelery\loaders.py", line 176, in autodiscover
for app in settings.INSTALLED_APPS])
File "C:\Python27\lib\site-packages\djcelery\loaders.py", line 195, in find_related_module
return importlib.import_module('%s.%s' % (app, related_name))
File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "C:\sites\corecrm\people\tasks.py", line 15, in <module>
from people.models import Customer, CustomerCsvFile, CustomerToTag, get_customer_from_csv_row
File "C:\sites\corecrm\people\models.py", line 163, in <module>
UserProfile._meta.get_field_by_name('username')[0]._max_length = 75
File "C:\Python27\lib\site-packages\django\db\models\options.py", line 351, in get_field_by_name
cache = self.init_name_map()
File "C:\Python27\lib\site-packages\django\db\models\options.py", line 380, in init_name_map
for f, model in self.get_all_related_m2m_objects_with_model():
File "C:\Python27\lib\site-packages\django\db\models\options.py", line 469, in get_all_related_m2m_objects_with_model
cache = self._fill_related_many_to_many_cache()
File "C:\Python27\lib\site-packages\django\db\models\options.py", line 483, in _fill_related_many_to_many_cache
for klass in get_models(only_installed=False):
File "C:\Python27\lib\site-packages\django\db\models\loading.py", line 197, in get_models
self._populate()
File "C:\Python27\lib\site-packages\django\db\models\loading.py", line 75, in _populate
self.load_app(app_name)
File "C:\Python27\lib\site-packages\django\db\models\loading.py", line 96, in load_app
models = import_module('.models', app_name)
File "C:\Python27\lib\site-packages\django\utils\importlib.py", line 35, in import_module
__import__(name)
File "C:\sites\corecrm\booking\models.py", line 17, in <module>
from people.models import Customer, UserProfile
ImportError: cannot import name Customer
To try and work out what the booking/models.py script sees in people I added the following at the start:
import people
print 'path: %s' % people.__path__
for item in dir(people):
print item
and that gives me the following output:
path: ['C:\\sites\\corecrm\\people']
__builtins__
__doc__
__file__
__name__
__package__
__path__
path: ['C:\\sites\\corecrm\\people']
__builtins__
__doc__
__file__
__name__
__package__
__path__
however, when I run manage.py shell --settings=myproject.settings.dev_settings I get the following output:
path: ['C:\\sites\\corecrm\\people']
__builtins__
__doc__
__file__
__name__
__package__
__path__
path: ['C:\\sites\\corecrm\\people']
__builtins__
__doc__
__file__
__name__
__package__
__path__
models
As you can see the models module is available at the end of the 2nd list for the shell command (and I've confirmed this is also the case for manage.py commands other than celery). How would I make sure that module is available at the same point when I run the celery command?
EDIT: I've now also set up this project on an Ubuntu VM and I'm getting the same error when I try to run the worker manage command. Any ideas? Anyone?
ANOTHER EDIT: I've pasted the code for booking/models.py and people/models.py at http://pastebin.com/fTVVBtB4
I'm pretty sure this line is your problem:
File "C:\sites\corecrm\people\models.py", line 163, in <module>
UserProfile._meta.get_field_by_name('username')[0]._max_length = 75
While you're still busy importing from people.models, this line (in particular get_field_by_name) forces Django to evaluate the model and setup all relationships between that model and it's related models. This, in turn, forces an import of Customer in people.models, while you're still busy importing that exact model. This is what results in an ImportError.
For a working solution you'll need to post your models.py.
Why does this error only occur with celery? I can't say for sure without some more information, but my best guess is that Celery handles importing everything slightly different (Django probably doesn't import Customer, CustomerCsvFile, CustomerToTag and get_customer_from_csv_row all at once) and that this exposes the bug in your code.
EDIT/SOLUTION:
I would remove this line:
UserProfile._meta.get_field_by_name('username')[0]._max_length = 75
And move it to the instance level, into the __init__ method:
class UserProfile(AbstractUser):
def __init__(self, *args, **kwargs):
self._meta.get_field_by_name('username')[0]._max_length = 75
super(UserProfile, self).__init__(*args, **kwargs)
If the cause of the issue is indeed what I think it is, this will fix the circular import while providing the same functionality. If the max_length functionality gets broken somehow (most likely because internally a max_length validator is added to CharField and _max_length is changed too late) I would instead override the complete username field in the init method:
class UserProfile(AbstractUser):
def __init__(self, *args, **kwargs):
super(UserProfile, self).__init__(*args, **kwargs)
self._meta.get_field_by_name('username')[0] = models.CharField(max_length=75, etc.)

Circular import is only stopping Django command, not shell or web response

I have two classes which import each other:
profile/models.py
class Company(models.Model):
name = ...
class CompanyReview(models.Model):
company = models.ForeignKey(Company)
from action.models import CompanyAction
action = models.ForeignKey(CompanyAction)
action/models.py
from profile.models import Company
class CompanyAction(models.Model):
company = models.ForeignKey(Company, null = True, blank = True)
The circular import works when the Django app is executed on the server or when I call view functions in the shell. However, when I import one of the classes, Django command will fail with an error (see Traceback below).
Why is that the case and only causing a problem in the command method?
How can I avoid the error? I have tried a lazy import of the CompanyAction class, but it led to the same error message.
not working alternative:
class CompanyReview(models.Model):
company = models.ForeignKey(Company)
from django.db.models import get_model
_model = get_model('action', 'CompanyAction')
action = models.ForeignKey(_model)
Interestingly, the variable _model is empty if I execute my command function and the classes are imported. When I load ./manage.py shell, the variable contains the correct class name. Why is that the case?
Traceback
(virtual-env)PC:neurix$ python manage.py close_action
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/Development/virtual-re/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
File "/Users/Development/virtual-re/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/Development/virtual-re/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/Users/Development/virtual-re/lib/python2.7/site-packages/django/core/management/__init__.py", line 77, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/Users/Development/virtual-re/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/Development/project/apps/action/management/commands/close_action.py", line 2, in <module>
from action.models import CompanyAction
File "/Users/Development/project/apps/action/models.py", line 26, in <module>
from profile.models import Company
File "/Users/Development/apps/profile/models.py", line 436, in <module>
class CompanyReview(models.Model):
File "/Users/Development/project/apps/profile/models.py", line 446, in CompanyReview
action = models.ForeignKey(_model)
File "/Users/Development/virtual-re/lib/python2.7/site-packages/django/db/models/fields/related.py", line 993, in __init__
assert isinstance(to, six.string_types), "%s(%r) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string %r" % (self.__class__.__name__, to, RECURSIVE_RELATIONSHIP_CONSTANT)
AssertionError: ForeignKey(None) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string 'self'
Django has a system for stopping circular imports on foreign keys detailed here: https://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey
You would want to do something like:
class CompanyReview(models.Model):
company = models.ForeignKey(Company)
action = models.ForeignKey('action.CompanyAction')
class CompanyAction(models.Model):
company = models.ForeignKey('profile.Company', null = True, blank = True)