Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Django 1.7 support. #15

Merged
merged 3 commits into from
Nov 23, 2014
Merged

Add Django 1.7 support. #15

merged 3 commits into from
Nov 23, 2014

Conversation

charettes
Copy link
Owner

No description provided.

@arescope
Copy link

So far, its migrating and creates the schema properly when create a new Tenant.

Regarding ModelForm and Create, Update, Detailview, where I need to specify the model that will use...do you have any suggestion or example of how to make it the cleanest way?

Thank you so much in advance

@charettes
Copy link
Owner Author

@arescope, I suggest you take a look at tenancy.views.TenantModelFormMixin.

You should be able to define a normal ModelForm subclass and simply add this mixin to your Create|Update|DeleteView subclass. Use tenancy.views.TenantObjectMixin for your DetailView subclasses.

@arescope
Copy link

hi @charettes . Yes I tried that and it is working properly. Thank you.

So far, it is working, but I need to test much more, I've around 100 models to test. I will let you know during this days.

@arescope
Copy link

@charettes actually I discover a bug...or something like that... When I created an initial migration it happens this

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Notice',
            fields=[
            ],
            options={
                'ordering': ['-pub_date'],
                'managed': False,
            },
            bases=(models.Model,),
        ),
    ]

is it normal???

@charettes charettes mentioned this pull request Sep 19, 2014
@charettes charettes force-pushed the django-1.7 branch 6 times, most recently from bc4847e to e390387 Compare November 23, 2014 09:06
@coveralls
Copy link

Coverage Status

Coverage increased (+0.08%) when pulling 85ac2f2 on django-1.7 into 84237ad on master.

charettes added a commit that referenced this pull request Nov 23, 2014
@charettes charettes merged commit d21fd0e into master Nov 23, 2014
@charettes
Copy link
Owner Author

@arescope let me know if this works for you. I'll move the migration issue to another ticket.

@arescope
Copy link

Hi @charettes, I got this error when running runserver

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/apps/config.py", line 202, 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/arescope/Projects/bwcored/appointments/models.py", line 28, in <module>
    class Role(TimeStampedModel):
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/tenancy/models.py", line 204, in __new__
    any(issubclass(base, TenantSpecificModel) for base in bases)):
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/tenancy/models.py", line 204, in <genexpr>
    any(issubclass(base, TenantSpecificModel) for base in bases)):
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/abc.py", line 161, in __subclasscheck__
    ok = cls.__subclasshook__(subclass)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/tenancy/models.py", line 182, in __subclasshook__
    tenant_model = get_tenant_model(False)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/tenancy/__init__.py", line 11, in get_tenant_model
    from .settings import TENANT_MODEL
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/tenancy/settings.py", line 54, in <module>
    lazy_class_prepared(app_label, model_name, is_tenant_user_model)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/tenancy/signals.py", line 15, in lazy_class_prepared
    seed_cache=False, only_installed=False
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/tenancy/utils.py", line 30, in get_model
    return apps.get_model(app_label, model_name)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/apps/registry.py", line 199, in get_model
    self.check_models_ready()
  File "/Users/arescope/.virtualenvs/bw/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.

@arescope
Copy link

The problem is with my CustomUserModel that it seems not loaded yet. Even in the INSTALLED_APPS is declared before.

@arescope
Copy link

about the migrations, I did this and so far, is working.
I took from your first approach for south

https://gist.github.com/arescope/e18d5c226c7b81335f68

@charettes charettes deleted the django-1.7 branch November 24, 2014 19:58
@charettes
Copy link
Owner Author

@arescope could you make sure f708167 fixed your issue and add the migration reference to #24.

@arescope
Copy link

Hi,
getting this now

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/apps/registry.py", line 109, in populate
    app_config.import_models(all_models)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/apps/config.py", line 201, 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/arescope/Projects//bwcored/appointments/models.py", line 28, in <module>
    class Role(TimeStampedModel):
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/tenancy/models.py", line 204, in __new__
    any(issubclass(base, TenantSpecificModel) for base in bases)):
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/tenancy/models.py", line 204, in <genexpr>
    any(issubclass(base, TenantSpecificModel) for base in bases)):
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/abc.py", line 161, in __subclasscheck__
    ok = cls.__subclasshook__(subclass)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/tenancy/models.py", line 182, in __subclasshook__
    tenant_model = get_tenant_model(False)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/tenancy/__init__.py", line 21, in get_tenant_model
    "been installed" % (app_label, object_name)
django.core.exceptions.ImproperlyConfigured: TENANCY_TENANT_MODEL refers to model 'estates.Estate' that has not been installed

As far as i saw,ur method get_modelcalls apps.get_model.

This method check if all the models have been loaded, that is not true at this point.
So, I tried to change to apps.get_registered_model, which creates again a deadlock.

As far as I understand the code, at the beginning apps.populate is called. That acquires apps._lock, so any time remove_from_app_cache is called it will try to acquire apps._lock, action which create a dead lock.

So the solution is not using that lock, or use another way to clean the cache.

Mostly this happen with multi-app projects I think.

I hope all my input help you to fix the problem.

@charettes
Copy link
Owner Author

0bdabb5 should deal with this.

@arescope
Copy link

Hi,

Having this now...assume that Role is Tenant Specific Model.

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/apps/config.py", line 201, 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/arescope/Projects//bwcored/appointments/models.py", line 28, in <module>
    class Role(TimeStampedModel):
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/tenancy/models.py", line 292, in __new__
    tenant_model = get_tenant_model(False)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/tenancy/__init__.py", line 21, in get_tenant_model
    "been installed" % (app_label, object_name)
django.core.exceptions.ImproperlyConfigured: TENANCY_TENANT_MODEL refers to model 'estates.Estate' that has not been installed

@charettes
Copy link
Owner Author

Ok nailed it down in a2c75a8.

@arescope
Copy link

Hi, that seems fixed.

But still entering into the deadlock

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/django/apps/config.py", line 201, 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/arescope/Projects//bwcored/cases/models.py", line 25, in <module>
    class Case(TimeStampedModel):
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/tenancy/models.py", line 273, in __new__
    remove_from_app_cache(through)
  File "/Users/arescope/.virtualenvs/bw/lib/python2.7/site-packages/tenancy/utils.py", line 42, in remove_from_app_cache
    raise Exception('Here it comes the deadlock')
Exception: Here it comes the deadlock

This model has manytomany fields.

and apps.populate(settings.INSTALLED_APPS) also acquires the lock. So maybe we should consider another way to clear the app_ache

@charettes
Copy link
Owner Author

I see, the new apps._lock is not re-entrant like it used to be.

I guess I'll need to prevent acquisition if the apps are not ready yet.

@charettes
Copy link
Owner Author

e41f2df should do.

@arescope
Copy link

Hi,

There is no deadlock anymore! Thank you.

However, I find a new issue, and is that the related_name in TenantMeta is not propagated to the tenant. That is because when call get_model, it is not ready the model still. I think for D1.7 changing from apps.get_model to apps.get_registered_model could be a solution. I tried by myself and it seems working.

Thank you

@charettes
Copy link
Owner Author

I'm not sure I understand what you mean.

TenantModelDescriptor seems to be correctly attached on my side, and they're also tested.

@charettes
Copy link
Owner Author

Ahhh I think I understand what you mean!

If the class has been prepared but has not been registered the related the descriptor will never be attached.

Fixed in 1b879a6.

@arescope
Copy link

Yup!! Thats it! Sorry for being so cryptic sometimes :).
I will test carefully but I think now everything should be ok.
About the migrations, you can take a look to my gist if you want.

@arescope
Copy link

Oh, Still the issue I reported on #16

I applied this patch https://gist.github.com/arescope/3e422d14cd480fc8ca69 and seems to work

@charettes
Copy link
Owner Author

Let's move the discussion on #16, I don't think the issue is related to Django 1.7 only.

Unfortunately your patch completely removes the generic relation instead of making it work correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants