AppRegistryNotReady: lazy format_html()? - django

Why do I get this exception?
Traceback (most recent call last):
File "/path1/myapp-isu/myapp_isu/tests/unit/views/test_view_isu.py", line 8, in <module>
from myapp_isu.search_form import ISUSearchForm
File "/path1/myapp-isu/myapp_isu/search_form.py", line 87, in <module>
class ISUSearchForm(forms.Form):
File "/path1/myapp-isu/myapp_isu/search_form.py", line 108, in ISUSearchForm
foo_filter=forms.ModelChoiceField(FooFilter.objects.all(), label=format_html('%s', reverse_lazy('foo-filter'), FooFilter._meta.verbose_name))
File "/path1/dt/dt/utils/templateutils.py", line 127, in reverse
return urlresolvers.reverse(*args, **kwargs)
File "/path1/dt/dt/utils/urlresolverutils.py", line 49, in patched_reverse
base_url = orig_reverse(viewname, urlconf=urlconf, args=args, kwargs=kwargs, prefix=prefix, current_app=current_app)
File "/path2/django/core/urlresolvers.py", line 578, in reverse
return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/path2/django/core/urlresolvers.py", line 432, in _reverse_with_prefix
self._populate()
File "/path2/django/core/urlresolvers.py", line 284, in _populate
for pattern in reversed(self.url_patterns):
File "/path2/django/core/urlresolvers.py", line 401, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/path2/django/core/urlresolvers.py", line 395, in urlconf_module
self._urlconf_module = import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/path1/myapp-eins/myapp_eins/etc/rooturls.py", line 13, in <module>
admin.autodiscover()
File "/path2/django/contrib/admin/__init__.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/path2/django/utils/module_loading.py", line 67, in autodiscover_modules
for app_config in apps.get_app_configs():
File "/path2/django/apps/registry.py", line 137, in get_app_configs
self.check_apps_ready()
File "/path2/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
It only happens if I call the unittest via PyCharm, not if I use py.test on the shell.
I guess reverse_lazy() is not lazy here, since it gets used in format_html(). Any way to have a lazy format_html()?
Versions:
Django 1.8
Pycharm 5.0.4

Since there are things like url_patterns in the stacktrace, I assume that DJANGO_SETTINGS_MODULE is set correctly .
But, you still need to call django.setup() before running anything else.
import django
django.setup()
For me, that made this error message go away.

I've had some trouble using PyCharm, myself, and I'll assume you're using the the community edition (which I've been using).
If so, the problem is very likely that you're not properly configured for django. You can probably fix this with some hacks that might work for this.
I'd start off with this.
(importing django to ensure the django console is run)
Then maybe this.
There's also this:
Check "Edit Configurations" under the test you're running, and add DJANGO_SETTINGS_MODULE=<app-name-here>.settings to environment variables.

If all of the above fails, try initializing your form and doing the import in the constructor:
class ISUSearchForm(...):
...
foo_filter = forms.Field()
...
def __init__(*args, **kwargs)
from ... import reverse_lazy
from ... import FooFilter
self.fields['foo_filter'] = \
forms.ModelChoiceField(
FooFilter.objects.all(),
label=format_html(
'%s',
reverse_lazy('foo-filter'),
FooFilter._meta.verbose_name
)
)
super().__init__(*args, **kwargs)
The error may be caused by the exact import sequence performed by the different test runners and the fact that you use only class variables to customize your form.

In django 1.8, reverse() and reverse_lazy() now return Unicode strings instead of byte strings.
Please refer the link:
https://docs.djangoproject.com/en/1.8/releases/1.8/

Related

Accessing a django model in middleware (django>1.7)

I'm upgrading from django 1.6.5 to django 1.9, and in the process upgrading several middleware classes. Some of those middleware classes use models during the process_request or process_response phases. However, I'm getting a AppRegistryNotReady: Apps aren't loaded yet. error attempting to use them.
Is there a way to import models during middleware?
Should I move my import statements into the process_request / process_response methods?
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/newrelic-2.50.0.39/newrelic/api/web_transaction.py", line 1329, in _nr_wsgi_application_wrapper_
result = wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/newrelic-2.50.0.39/newrelic/api/web_transaction.py", line 1329, in _nr_wsgi_application_wrapper_
result = wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 158, in __call__
self.load_middleware()
File "/usr/local/lib/python2.7/dist-packages/newrelic-2.50.0.39/newrelic/common/object_wrapper.py", line 302, in _wrapper
result = wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 51, in load_middleware
mw_class = import_string(middleware_path)
File "/usr/local/lib/python2.7/dist-packages/django/utils/module_loading.py", line 20, in import_string
module = import_module(module_path)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/web/MyJobs/MyJobs/apache/../middleware.py", line 9, in <module>
from django.contrib.sites.models import Site
File "/usr/local/lib/python2.7/dist-packages/django/contrib/sites/models.py", line 83, in <module>
class Site(models.Model):
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 94, in __new__
app_config = apps.get_containing_app_config(module)
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 239, in get_containing_app_config
self.check_apps_ready()
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
AppRegistryNotReady: Apps aren't loaded yet.
You need to use the new API to get a WSGI handler:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
This will call django.setup() for you, which will populate the app registry.

Django, upgrading to 1.9

I evolve my Django version to 1.9 (before I had the 1.6 or 1.7), so I modify many obseltes things...
But I have a problem with theses lines in my urls.py :
import django
import main_app
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView, ListView
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from main_app.views import *
from main_app.views import password_reset_confirm
... # many urls
url(r'^authentification/$', django.contrib.auth.views.login),
url(r'^forget/send/$', django.contrib.auth.views.password_reset_done),
url(r'^password/$', django.contrib.auth.views.password_reset),
url(r'^password_forget/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$', main_app.views.password_reset_confirm),
url(r'^password-init/$', django.contrib.auth.views.password_reset_complete),
I have this error when I write "python manage.py runserver" :
Unhandled exception in thread started by <function wrapper at 0x7f5bcf01af50>
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 10, in check_url_config
return check_resolver(resolver)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 19, in check_resolver
for pattern in resolver.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/yb/web/carzuip/carzuip/urls.py", line 55, in <module>
url(r'^authentification/$', django.contrib.auth.views.login),
NameError: name 'django' is not defined
I don't understand why I have a problem just with those 5 urls ??!
Thank's
That code doesn't show you importing django: it shows you importing elements underneath it, but never the name itself. It is a fundamental principle of Python that you must import or define every name you use. In your case, import django at the top would work, although note you would then have another problem when the code gets to the password_reset URL since that is referenced from main_app which you again haven't imported.
Try to create a new project with django-admin startproject and copy over the needed settings / copy over manage.py and wsgi.py.
Some releases had breaking changes, i.e. on how the wsgi module imports certain django stuff. Most projects can easy be recreated and you have the newest django defaults in the files.
It's of course just a guess, but worth trying. If you're using a virtualenv, you may need to recreate some things inside, when your python was upgraded / you moved the virtualenv. Such stuff can be quite annoying sometimes.
Resolved !
Euh I think...
Just I add these lines :
from django.contrib.auth.views import login
from django.contrib import auth
And it's works !
It's normal hein ?

django-seo setting up, models aren't loaded yet

I'm trying to add django-seo into my site. But I can't cope with setting up. I followed instructions documentation, but error occurs.
This is what I did:
Installed django-seo packacge
Added rollyourown.seo to INSTALED_APPS
Created seo.py file in my site content app
And this is what I wrote into seo.py file:
from rollyourown import seo
class Metadata(seo.Metadata):
title = seo.Tag(head=True, max_length=68)
description = seo.MetaTag(max_length=155)
keywords = seo.KeywordTag()
heading = seo.Tag(name="h1")
class Meta:
seo_views = ('SiteContent',)
seo_models = ('SiteContent',)
When Meta class is removed, I can't add any meta tags to contnet via Django Admin Site( I registered it in admin site ). I've read that django-seo use get_absolute_url() to deal with it. But in my site app I don't use this function for provide more some utilities to multilanguage.
But if i add Meta class, i will get this error:
Traceback (most recent call last):
File "F:/Site/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "F:\Python27\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
utility.execute()
File "F:\Python27\lib\site-packages\django\core\management\__init__.py", line 354, in execute
django.setup()
File "F:\Python27\lib\site-packages\django\__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "F:\Python27\lib\site-packages\django\apps\registry.py", line 108, in populate
app_config.import_models(all_models)
File "F:\Python27\lib\site-packages\django\apps\config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "F:\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "F:\Python27\lib\site-packages\djangoseo-1.0-py2.7.egg\rollyourown\seo\models.py", line 10, in <module>
__import__(module_name)
File "F:\Site\SiteContent\seo.py", line 5, in <module>
class Metadata(seo.Metadata):
File "F:\Python27\lib\site-packages\djangoseo-1.0-py2.7.egg\rollyourown\seo\base.py", line 166, in __new__
options = Options(Meta, help_text)
File "F:\Python27\lib\site-packages\djangoseo-1.0-py2.7.egg\rollyourown\seo\options.py", line 19, in __init__
self._set_seo_models(meta.pop('seo_models', []))
File "F:\Python27\lib\site-packages\djangoseo-1.0-py2.7.egg\rollyourown\seo\options.py", line 96, in _set_seo_models
seo_models.extend(models.get_models(app))
File "F:\Python27\lib\site-packages\django\db\models\__init__.py", line 54, in alias
return getattr(loading, function_name)(*args, **kwargs)
File "F:\Python27\lib\site-packages\django\utils\lru_cache.py", line 101, in wrapper
result = user_function(*args, **kwds)
File "F:\Python27\lib\site-packages\django\apps\registry.py", line 168, in get_models
self.check_models_ready()
File "F:\Python27\lib\site-packages\django\apps\registry.py", line 131, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
I tried diffrent solutions but nothing helped.
Django-SEO is not compatible with Django 1.7, which is the first version of Django that includes the AppRegistry.
Either rollback to Django 1.6.x or remove Django-SEO.
I got it working by changing get_query_set() to get_queryset()(changed in django1.8) in file rollyourown\seo\backends.py
As original Django-SEO is not supported anymore, these guys made and support their own version / fork of Django-SEO. I use it with Django 1.8.8, Python 3 is supported too.
https://github.com/whyflyru/django-seo

Django 1.7 upgrade error: AppRegistryNotReady using serializers from rest_framework

I get this traceback:
Traceback (most recent call last):
File "./manage.py", line 38, in <module>
execute_from_command_line(sys.argv)
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/apps/config.py", line 197, in import_models
self.models_module = import_module(models_module_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/mgregory/Documents/cm_central/cmh_server/models.py", line 88, in <module>
class VersionSerializer(serializers.ModelSerializer):
File "/Users/mgregory/Documents/cm_central/cmh_server/models.py", line 89, in VersionSerializer
brzs= BrzSerializer(many=True)
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/rest_framework/serializers.py", line 198, in __init__
self.fields = self.get_fields()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/rest_framework/serializers.py", line 234, in get_fields
default_fields = self.get_default_fields()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/rest_framework/serializers.py", line 732, in get_default_fields
reverse_rels = opts.get_all_related_objects()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/db/models/options.py", line 498, in get_all_related_objects
include_proxy_eq=include_proxy_eq)]
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/db/models/options.py", line 510, in get_all_related_objects_with_model
self._fill_related_objects_cache()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/db/models/options.py", line 533, in _fill_related_objects_cache
for klass in self.apps.get_models(include_auto_created=True):
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
result = user_function(*args, **kwds)
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/apps/registry.py", line 168, in get_models
self.check_models_ready()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/apps/registry.py", line 131, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
from this code (models.py):
# Serializers for transmitting CMx install information over HTTP
class BrzSerializer(serializers.ModelSerializer):
class Meta:
model = Brz
fields=('filename',)
class VersionSerializer(serializers.ModelSerializer):
brzs= BrzSerializer(many=True)
class Meta:
model = Version
fields=('name', 'for_mac', 'for_windows', 'brzs')
It sounds like it's telling me "your VersionSerializer can't have a BrzSerializer, because you haven't registered that yet".
I've looked at other SO questions relating to AppRegisteryNotReady, but didn't find one that matches this symptom. Surely I have to be able to define a chain of dependent models like this?
It turns out that having the declaration of the serializers inside models.py is causing that application to be used before the app registry has finished being loaded.
models.py is actually the wrong place to declare these serializers (though I'm 99% certain I did it that way based on an example of how to use them).
The fix is to move the declaration of the serializers out into their own file (which makes sense, because they have nothing to do with the database schema, which models.py is defining), and import that from the view. By the time the view gets going, the app registry is ready.

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.)