how to create Groups programatically in Django - django

I am setting up a member site using django, in which members belong to groups that they create and then invite other users to join.
It seems that what I am looking for is to use the django groups functionality, but the documentation on how to go about this is minimal at best - at least I haven't found any. It basically talks about creating groups in the Admin console, which is not what I am trying to do. I would like to do it programatically/dynamically.
Another way to go about this would be to at a foreignkey to the User model up to a group model, however I can't add a foreignkey to the generic User model.
I have read lots of stuff that google threw up at me from my searches. none helpful.
How would I go about this?
Thanks

Well a Group is just another model in Django (one of the models defined in the Django library).
You can thus create a group with:
from django.contrib.auth.models import Group
g1 = Group.objects.create(name='Name of the group')
The Group model has two many-to-many relations: one to Users (with related name users), and one to Permission (with related name permissions). So we can use the corresponding managers to add and remove Users and Permissions.
Then you can for example populate the group with users like:
g1.user_set.add(user1, user2, user5, user7)
You can also add permissions to a group with:
g1.permissions.add(perm1, perm3, perm4)

Related

How do I create Groups for custom users in Django?

I created a Custom User Model by basically copying and pasting the full example given in the Django Docs. But in the docs, they unregistered groups. But in my project I will need groups because I'll have different uper types. So how can I still have groups and add my custom users to them?
Try using a customGroup class extending the Group class, or a custom group class from scratch according to requirements

How do we change Field Names in our existing Users table in django

I am trying to Edit Users Table in Django. I am using Users Table to login or register a users. I have to add a new field name Role in that Table but i can't find any option to edit that existing table in admin section.
i just try to field some files to field out where the code of that existing Table is but did't get it.
is there any way to Edit the Table or I have to Create a New Table and have to create a new method of registration.
i am not expert so it's hard to me understand things.
Well first, the Django admin interface it's just for performing CRUD operations over already existing models, you are not able to change in any way the database tables (at lest not using the "out of the box features") using the admin interface.
Said that in order to do what you want to do, with any model (not just User), you should:
Add the field to the model.
Instruct the admin interface to list this fields along the others.
Now the user model is kind of a special model here so I'll recommend a couple of readings you should complete before go forward with the model User customization.
References (User customization): Substituting a custom User model, Extending the User model.
And for the admin interface ...
Reference (admin interface): ModelAdmin options, special attention here to list_display

How to create a permissions model for accessing apps in a project?

I am creating a project with multiple services, each one represented as an app. I want to create a dashboard page where a user can see what apps they have access to, with staff users being able to add and remove apps via admin pages. What is the best model structure to do this? I.e. How should my models.py look? Is there a way to link such a table to the settings.py registered_apps tuple?
Sounds like what django admin do.
You can use django's permissions for that. Basically you assign permissions to groups and then you put your users in those groups (a user can be in several groups).

Django admin - how to store references to models in model field?

I'm making some big changes in Django Admin and need to make an application to group various models to groups and then show it in menu.
I have a function to generate list of all available admin models including permissions.
So I make 1 model to add group (id, name). Via Inlines I want to add specific models to groups.
What is the best way to store models references? Should I store it as string and during my menu is generated I should parse the name, find specific model and generate its url in admin?
Thanks for clues.
Sounds like contenttypes would help you.

django custom groups and custom permissions

I'm creating CMS and now facing some issue I need your advice. I have few different modules (apps) I can define custom permission to them - in model i define custom permissions like "view_store", "edit_store", "delete_store" and so on. And then I have defined different user groups (I want to have group based user access control) - admin, editor, vip, user.
I'm creating these groups when running one time command to initialize CMS (manage.py initcms) and I want of course all the right permissions will be added to the group in the same time.
initcms action is running after the syncdb, so all the models are in DB (info about permissions also of course).
I have something in my mind... If this is good way to go or you have the better one? Let me describe mine: I want to give for example for vip user all the permission from all the models (which have this permission defined) to "view_*". It means vip can view everything. I have an idea when initializing database just grab all entries (all permissions) which fits pattern "LIKE view_%" and then add these all to group's permissions.
But then the problem if the new module will be added... I need to re-run this action and check if all the permissions are right... Maybe there is some dynamic way to deal with group permissions?
One possible solution is to use Django Signals, which can be triggered before or after a model's save mothod has been called or after or before any M2M action takes places, after syncdb etc... You may select a proper signal that fits you best and then call a function that checks related permissions and add or remove any if necessary...