Access User fields using UserProfile object - django

I want to access User model fields using UserProfile object.
I have a 'role' field in Userprofile,
class UserProfile(models.Model):
user = models.OneToOneField(User)
role = models.CharField(max_length=30)
Each user has a role, 'supervisor' or 'admin' now I want to get the user objects whose role=supervisor. I want to be able to access the username of those users with role supervisor which is in User model customized.
I cant get the query right. Help please.

supervisor_usernames = [up.user.username for up in UserProfile.objects.filter(
role='supervisor')]

Related

Django getting superuser to use in a model class

I want to get all users information to set up user profiles including superuser, but somehow my code doesn't get superuser data . Please have a look
from django.contrib.auth.models import User
from django.db import models
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
as per the docs, Instead of referring to User directly, you should reference the user model using django.contrib.auth.get_user_model(). This method will return the currently active user model – the custom user model if one is specified, or User otherwise. So use User = get_user_model() or settings.AUTH_USER_MODEL if you a custom user

Permissions from 2 django projects are merged. How to separate them?

Good day to all)
I created a custom user model with custom permissions. I have several projects and their permissions merged into one table('auth_permission'). Is it possible to somehow customize the table itself for these permissions to separate projects?(like db_table = '"schema"."table"' to the models.).Google did not give answers.
class TestUser(AbstractUser):
phone = PhoneNumberField(null=False, blank=False, unique=True)
email = CharField(unique=True, max_length=35, null=False, blank=False)
class Meta:
db_table = '"fyzzys"."users"'
permissions = [
("can_see_payments", "payments"),
("can_see_analytics", "analytics"),
]
UPD: Here is a screenshot that shows the permissions of two completely different projects at the same time from admin panel.
In order to give a user permissions, you will want to add the PermissionsMixin to the User model like so.
class TestUser(AbstractBaseUser, PermissionsMixin):
If you are using the AbstractUser you are actually already inheriting the PermissionsMixin.
The permissionmixin is a model within django.contrib.auth.models. It adds to the user model the following fields (columns in the user table); is_superuser, groups, user_permissions. The permissions mixin allows you to add (and remove) 0..*(many) individual or group permissions to a user.
To add permissions to a user you can then use.
from django.contrib.auth.models import Permission
# The permission needs to exist first.
permission = Permission.objects.get(name='Can view poll')
u.user_permissions.add(permission)
The code for for the django.contrib.auth.models.permissionsmixin can be found following the link.

Django Form save - circular dependency

I have 2 models Account and User. An Account can have multiple users.
When the first User(Owner) is created at the same time the Account will be created.
Because User have a ForeignKey to Account, I need first to create the Account, and after the User.
But Account had a field created_by which is request.user. So is a circular problem.
I think I need to create first the Account and created_by to be a superuser(first) and than create the User, and update the Account with the new user.
class Account(MetaData):
name = models.CharField(max_length=255)
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True,
related_name='%(app_label)s_%(class)s_created_by', on_delete=models.CASCADE)
class User(AbstractBaseUser):
account = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, related_name='owner')
How can I do that ?
I need this only first time, for the first user, because after that for the other users the Account will exist.
When I'm saying first user, I'm not referring to staff users (as superusers), but normal users for which an account is mandatory thru a register form.
blank=True is just for staff members.
Your User model's account field allows nulls, so nothing prevents you from first creating the user without an account, then creating the account with this user, and finally updating the user with the account:
with transaction.atomic():
user = User.objects.create(account=None, ...)
account = Account.objects.create("test", created_by=user)
user.account = account
user.save(update_fields=["account"])
Since it's only a problem for the first user, you might want to override the method create_superuser of the manager to add the account. Also, since you've defined the ForeignKeys with null=True, it should not be a problem to first create a superuser (using super().create_superuser()) and then create the account and attach it to the superuser.
When deploying your site the first time, you'll have to manually run the ./manage.py createsuperuser command.

Django models: database design for user and follower

In Django model I am making a table 'followers', which has:
user's id. (this is followed by)
user's id (this is follower)
that's simple a user can follow other users.
How should I define the model in Django?
I tried this, but does not work:
user = models.ForeignKey('self')
follower_id = models.ForeignKey('self')
How should this be done?
thanks
The 'self' argument won't work unless you have a model called self.
Assuming that your assignment model is called Following, and you're using the built in User model then you can do:
class Following(models.Model):
target = models.ForeignKey('User', related_name='followers')
follower = models.ForeignKey('User', related_name='targets')
This will likely need some further uniqueness and validation logic.
Note the related_name attribute, see https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.ForeignKey.related_name. This means that for a given user object you can do user.targets.all() to get users they follow, and user.followers.all() to get users who follow them.
Note also that Django returns target model instances, not IDs, in the ORM. This means that even though the underlying table may be called follower_id, in the python code following.follower will return an actual User object.
Seeing as Following is actually the through table for the many-to-many relationship between Users. I would create a Profile model which extends the Django User model, and then declare the many-to-many relationship (using ManyToManyField).
from django.contrib.auth.models import User
from django.db import models
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
following = models.ManyToManyField(User, related_name='followers')
Use the many to many field.
followers = models.ManyToManyField('self', symmetrical=False)

Django save model with anonymous user

I have a Django model:
class Project(models.Model):
user = models.ForeignKey(User)
zipcode = models.CharField(max_length=5)
module = models.ForeignKey(Module)
In my views.py:
def my_view(request):
...
project = Project.objects.create(
user=request.user,
product=product_instance,
...
)
project.save()
I want to be able to save user as an authenticated user OR an AnonymousUser (which I can update later). However, if I'm not logged in I get this error:
ValueError: Cannot assign "<django.utils.functional.SimpleLazyObject object at 0x1b498d0>": "Project.user" must be a "User" instance.
I guess that Django won't save the AnonymousUser because it is not a User as defined in the User model. Do I need to add the anonymous user to the User model, or am I missing something?
Any assistance much appreciated.
The user field is a ForeignKey. That means it must reference some user.
By definition, the AnonymousUser is no user: in Django, there is no AnonymousUserA and AnonymousUserB. They're all the same: AnonymousUser.
Conclusion: you can't put an AnonymousUser in a User ForeignKey.
The solution to your issue is pretty straightforward though: when the User is anonymous, just leave the field blank. To do that, you'll need to allow it:
user = models.ForeignKey(User, blank = True, null = True)