I am using the django user model and want to create a logic that which user has been registered by which user like admin as a user can only register another employee or admin as a user.
Means an entry in the user model can be created by another user(user in the same user model) under some business logic.
I want to reference/know which user belongs to which user.and don't wanna create a new model to do this until no way left.
please help me with this.
Thank you
You can self reference the same model by using
created_by = models.ForeignKey('self', on_delete=models.CASCADE)
Related
I'm at a loss... I'm just learning Django and I am really rather confused about how to make a field work the way I would like it to.
I understand that Django has a native "Groups" model. However, I am looking to build my own teams model for customization and practice.
Here is my models.py file for my Users app:
from django.db import models
from django.contrib.auth.models import User
class Team(models.Model):
members = models.ManyToManyField(User)
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
admin = models.BooleanField("Admin Status")
Here's where I'm confused. I would like to be able to call the team that the user is part of directly from User.Profile. So, I want to add a field to my Profile class that will automatically populate with the team name when a user is added to a team.
A potential problem I can see is that, currently, I can assign a user to multiple teams. This doesn't bother me, perhaps I can have a Profile model field that automatically populates with a list of all the teams that the user is associated with. Regardless, I can't figure out what type of field I would need to use, or how to do this.
Does that make sense?
A potential problem I can see is that, currently, I can assign a user to multiple teams.
Indeed, you can however easily retrieve the Teams the myprofile object is a member of with:
Team.objects.filter(members__profile=myprofile)
You thus can make a property for the Profile model:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
admin = models.BooleanField("Admin Status")
#property
def teams(self):
return Team.objects.filter(
members__profile=self
)
Then you thus access the Teams of a myprofile with myprofile.teams.
So, I want to add a field to my Profile class that will automatically populate with the team name when a user is added to a team.
From my limited knowledge of database, you can add a name field to your Team model.
Keeping in mind your requirement as mentioned in question, i would suggest you to use django reverse relations to get all the teams the profile is associated with
user_teams = User.objects.get(id='user_id').profile_set.all()[0].team_set.all()
to know more about django ORM reverse relation, here is a very short article
I'm trying to create a customized User class inheriting from django User. Problem is I already have some users in database which can not be deleted whatsoever and also I have another class (let's say reports) which have a foreignkey to User. My question is: Is there any way to create my new User class and keep the old data too?
thanks in advance.
You can create related model that links back to User. This is a common approach if you have different types of users, but there are also other use cases.
class SpecialUserProfile(models.Model):
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.SET_NULL)
special_feature = models.CharField(max_length=100, null=True, blank=True)
etc.
What you also need to do is create this profile, when new user is added to User. You can do this with post_save signal.
#receiver(post_save, sender=User)
def create_special_user_profile(sender, instance, created, **kwargs):
if created:
SpecialUserProfile.objects.create(user=instance)
You create profiles for existing user with a command or write and run a temporary function that does that for existing users in User.
Now you can use ORM in the sense of user.specialuserprofile.special_feature.
This way you'll keep using User model as a base, it won't mess with build-in user related functionalities, won't have think about new and old users and you can use this new model for any additional information about users.
I need to have 2 user roles in my project: client and employer
Question number one: There is an option called 'groups' in Django admin panel.Is it the same stuff like user roles?
Question number 2:
Let's say that I need to add custom field like company_name to users with role customer
I have read some question like this Extending the User model with custom fields in Django
And I got from that I should use a property called OneToOneField(User)
But still I still confused.
So how can I add this custon field company_name in model?
Answer to Q1:
Groups are are for controlling the Admin interface access, with different permissions. If you try creating a group you will see that you can select permissions as well for that particular group. So I would say it is not what you want. But if you create groups without permissions you can use for your purpose as well.
Answer to Q2:
Please refer to documentation for more details and examples.
Here is just a basic example. For your case you need to create new model which has one to one relation to you auth user model and extends more fields as you want, for example:
USER_TYPE = {0: 'Client', 1: 'Employer'}
class MySpecialUser(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)
type = models.IntegerField(choices=USER_TYPE.items())
# used only for Client type
customer_name - models.CharField(..., null=True)
I have a question about django's user inheritance.
I have gone through docs of django where I have seen inheritence of User model if I want to register the user through email and password.
My questions :
What will be the difference on default User model and Custom User model that I will make ?
Can I use the permissions and add group in the Custom User model ?
And also what if I want to inherit django's default User in my model ?
class Person(User):
address = models.CharField(max_length=100)
def __unicode__(self):
return self.address
Here the user model is django's default User model ?
Can I inherit like that ?
based on django specifying a custom user model
The easiest way to construct a compliant custom User model is to inherit from AbstractBaseUser. AbstractBaseUser provides the core implementation of a User model, including hashed passwords and tokenized password resets.
Alse Extending the existing User model can be a good option.
I saw a couple of ways extending user information of users and decided to adopt the model inheritance method.
for instance, I have :
class Parent(User):
contact_means = models.IntegerField()
is_staff = False
objects = userManager()
Now it is done, I've downloaded django_registration to help me out with sending emails to new users. The thing is, instead of using registration forms to register new user, I want to to invoke the email sending/acitvation capability of django_registration. So my workflow is:
1. add new Parent object in admin page.
2. send email
My problem is, the django-registration creates a new registration profile together with a new user in the user table. how do I tweak this such that I am able to add the user entry into the custom user table.
I have tried to create a
modelAdmin
and alter the save_model method to launch the create_inactive_user from django_registration, however I do not how to save the user object generated from django_registration into my Parent table when I have using model inheritance and I do not have a Foreign key attribute in my parent model.
It's probably something like:
p = Parent()
p.user_ptr = user
p.contact_means = ...
p.save()
(Django creates the foreign key for you when doing model inheritance, plus the attribute ending in _ptr)