I'm new to Django and I'm trying to figure out which path to take to solve a problem.
Many-to-one OR Django Groups?
I have a cat model and now I want to associate any new cats entered into my app with cat litters. (think new kittens)
I've been reading about Django Groups but only see this associated with the User model. Can Django Groups be used for any models I create? Is that the best path to get to what I want or should I just rely on a regular Many-to-One model relationship and build out regular models that way?
Groups: A generic way of applying labels and permissions to more than one user.
In your case I would use,
one to many: assuming each kitten belongs to one cat.
many to many: if you link kitten to utmost two cats.
You should use groups for user authentication things, like admin group or specific user group and assign permission or rights to specific groups.
So use accordingly.
more info on user authentication and groups
Related
In my wen application i mad different login for different person like for HR ,for engineer ,for owner using django user creation form , and authenticate them using Django authentication ,now everything is work very well i am able to add user from the front add
but the problem is that everyone get same power and authority , i can change it from the admin penal but it will not be beneficial for my client/user I WANT ALLOW DIFFERENT KIND OF PERMISSIONS TO DIFFERENT GROUP OF USER(daynamicaliy with using django penal) show How can i do that.
(i'm beginner so its request to all of you to give answer in some detail format)
THANK YOU
If you refer to the docs here, there is already a default system where you can add or remove permissions to a specific group of users. What you should do next is to create different subclasses for different types of users.
So your models will look something like this eventually:
class User(models.Model):
# just keep whatever you had here
# Engineer and Manager will both be subclasses of User
class Engineer(User):
# add whatever permissions you have
class Manager(User):
# add different permissions
In this way, you will be able to apply custom permissions to different groups of people.
I have a Django app that uses the stock User and Group models. I have two groups, group A and group B. I would like to prevent any user from being in both group A and group B, being in just one group or the other is fine. what is the best way to go about this? I was looking into model validation but I would like to do this without modifying or sub classing the stock User model. It would even be enough for me to be able to check and make sure that this wasnt being set on the django admin edit user page.
I'd like to add the capability for users to create their own groups and invite others to join the group (or leave the group) or for users to request access to a group to a django app.
The group in this case is basically a pool of potential players for a football match, from which the actual players will be chosen.
Is the standard django auth groups system the correct thing to use here or is there another way? I'd need it to be able to do invitations and stuff, obviously.
I can obviously write my own but is there another app out there that already does this kind of thing nicely?
If not, has anyone got any tips on how to go about this?
Creating your own model will give you more control to add extra information to those groups, such as the invitation system you described. The auth groups models was designed for classifying users by what level of control they have over the data on the website, which isn't what you want a groups model for. A new model will be much easier for you work with than trying to extend the existing groups model; which has built in functionality that you won't use, and probably has security features that will make it difficult to work with.
I'm a bit unsure what to use Django user groups for.
I have an application where every user belongs to a different organisation. The organisations don't have anything to do with read/write permissions. It's just a way to separate groups of users. Every organisation needs some additional fields, like a name, URL, and email address. New organisations will be added to the system over time.
Within every organisation, users can have different permissions for moderation and administration, for which I (also) want to use user groups.
My question: Should I use Django's user groups to define the organisations, or should I just make an 'Organisation' model with a relation to the user?
Nope. User groups are made for different reasons. You CAN use them to define organisations but I think you should think bit further ahead:
will the organisation require more fields than just name?
perhaps you will need permissions in the future to define users roles within organisations?
I'm sure you can come up with more things to think of. But if you answered yes to one of those questions then just create your Organisation model.
1) You need to add group from django admin side under group table.
2) And while creating new user, assign specific group to user using user_obj.groups.add(group_id). Or Let user select group at frontend.
and then user_obj.save()
in Group table, you can create organization
OR
You can create individual organization table and assign assign user to specific organization.
In my Django setup I have a group called "authors". I want to show their posts on the front page, sorted by date. All of the template examples I can find for groups involve checking the group membership of the currently logged in user, which is not what I want. I simply want to aggregate based on group membership globally, not authenticate or display based on group membership of the user. This seems like it should be a straightforward and common approach, but I'm having a hard time finding a way to do it (and I am also a novice, which does not help I'm sure).
Suggestions welcome.
You can do this:
from django.contrib.auth.models import Group
group = Group.objects.get(name='authors')
authors = group.user_set.all()
Now, you have the list of authors, you can query the list of posts by these users and display it on the home page.