Comments (6)
Without a stack trace, I'm going to guess you're seeing the error on line 134 here. Is that correct?
Are you removing the UserTenantPermissions
beforehand? Are you setting them at all? I would also ask how you're adding the user to the tenant as the Tenant.add_user()
creates the UserTenantPermissions
, so they should be there when attempting to remove.
I suppose this could be refactored to be a little safer in case the UserTenantPermissions
are removed ahead of time.
from django-tenant-users.
Yes the issue is line 134.
I am not removing nor setting them. I am adding them like:
userT = get_object_or_404(UserTenantPermissions, profile=user)
perm = Permission.objects.get(codename=request.POST.get('codename'))
userT.user_permissions.add(perm)
This snippet works. But for some reason removing it doesn't. I will try some variations to see how to remove it.
from django-tenant-users.
Since the removing fails on line 134, then there isn't a UserTenantPermissions
object for the user within the tenant you're trying to remove the user from. Have you verified the object exists?
from django-tenant-users.
I am facing same problem now , but i fix this issue by practicing the document way to add new user ,and bind a user to a company.
-
when you add new user you should use User.objects.create_user
class UserRegisterSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ["id", "username", "email", "password"]
read_only_fields = ["id"]def create(self, validated_data):
# disable the account activation by default
validated_data["is_active"] = False
instance = User.objects.create_user(**validated_data)
send_activate_email.send(
sender="UserRegisterSerializer.create",
request=self.context["request"],
instance=instance,
)
return instance
by the way you also need to decalre the User manager with the objects = UserProfileManager() so that you can use that method
-
when create new company(tenant) you should use provision_tenant to create ,this will do batch of stuff behind
-
when bind new user to a company, you should use TenantBase.add_user . this method will link the company-user m2m field , and create a record in UserTenantPermissions in the correct schema. the table name is permissions_usertenantpermissions.
@action(methods=[HTTPMethod.POST], detail=True)
def add_user(self, request, pk=None):
company = self.get_object()
email = request.data.get("user_email", None)
if email is None:
return Response(
status=status.HTTP_400_BAD_REQUEST, data={"detail": "user_email should not be null"}
)
user = User.objects.filter(email=email).first()
company.add_user(user)
return Response(status=status.HTTP_200_OK, data={"detail": "ok"})
for the transfer_ownership function ,you also make sure the company owner also have record in permissions_usertenantpermissions table ,
by the way , as i tried out , i find the django admin dashboard not calling the recommend method to create an new company or link the user to a company. even you override the Model.save() method .
from django-tenant-users.
@kusime - The formatting of your comment is a little off, seems you may have missed a backtick or something.
To your points:
- Use
User.objects.create_user()
- Yes, this is general good practice, but it's just a helper function that creates the User object and then adds the object to the Public Schema. Honestly, this whole function should be cleaned up a bit as it doesn't make sense to only require it to be run in the public schema, we can set that. - Use
provision_tenant()
- 100%. There's extra params set when interfacing withdjango-tenants
when creating the tenant. - Use
TenantBase.add_user()
- 100%. I think this is what @premudeshi is missing. This adds aUserTenantPermissions
object within the specific schema and is referenced when removing a user too. If this object doesn't exist, right now we're throwing an error. - Admin Dashboard - @kusime, this is a great call out and could maybe be something we add to the documentation. Having a template that can be used as the ModelAdmin that uses all the appropriate calls would be a great thing to have.
from django-tenant-users.
It seems my Django User models messed up the migrations, I am guessing because of incorrectly configured inheritance.
Deleted migrations and started fresh, it worked.
from django-tenant-users.
Related Issues (20)
- Multi-type Tenants support updated in the Docs (#483) but latest released version (v1.2.0) is behind HOT 2
- Where's the link to the documentation ? HOT 1
- Fully functioning example HOT 4
- access the table users_user_tenants HOT 8
- Create/modify a tenant user in django-admin page HOT 2
- How to successfully run it on the localserver? HOT 13
- Unable to pass the dictionary of password for the super user created with create_public_tenant HOT 3
- Wrapping my head around Permissions Reference HOT 5
- What is the proper way to use the @login_required decorator with django-tenant-users? HOT 5
- How to proceed with Django Tenants & Django Tenant Users for a Multi Level SAAS HOT 1
- Existing example with Django REST framework? HOT 1
- The question about the users that will be tenant based HOT 1
- Add management command shortcut for create_public_tenant
- CircularDependencyError HOT 14
- Users being able to login into any tenant schema HOT 5
- Middleware module is absent on the installable package. HOT 3
- TenantAccessMiddleware and unauthenticated users HOT 2
- cannot install on an existing django_tenants project HOT 7
- Django rest framework with django-tenant and django-tenant-users (TenantUser object has no atttribute tenant) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-tenant-users.