Connect logged user with model - python

I'm working on small project with Django framework. And as I can implement usage of authentication mechanism, that I can find a solution how to use information about logged user with model I define.
In example. I have model that will store information about QSL cards, and I want to have option that depends on which user is logged, his/her QSL cars will be shown from database.
I search here and in docs.djangoproject.com but without success.
Thanks in advance for any tips or links.

If you try,
user = request.user
Variable user will have currently logged in user object. You can use this user to filter some models objects where user is foreign key. For e.g posts = Post.objects.filter(user=request.user). If you want to get any specific user information, for e.g username. You can try username = request.user.username

Related

What is a good way to store guest user information that may be saved to a permanent user after registration in django?

I have a RESTful django webapp that allows users to take quizzes in a progressive system where the quizzes become increasingly difficult. Their progress is saved when they answer a question. I'm using django-rest-framework.
class User(AbstractUser):
pass
class IntervalsProfile(models.Model):
# Belongs to User model in 1-to-1 relationship
# If User is deleted, then the corresponding IntervalsProfile will be deleted as well
user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name='profile', on_delete=models.CASCADE)
level = models.IntegerField(default=0)
current_level = models.IntegerField(default=0) # Level the user is currently viewing/on
I would like users to be able to play as a guest, and if they so choose, register in order to save their progress. I cannot figure out how to save this guest information or how to save it once they register.
I've searched thoroughly and all the solutions I see are either several years old or seem too cumbersome (such as adding a check for authentication in every view and having 2 cases for each view).
Any advice or guidance would be much appreciated.
if they so choose, register in order to save their progress
Subclass django's User model and add a new field called is_guest = BooleanField. (django has good documentation on how to do this already.)
Ask your users to input just a username when they visit for the first time. (or you can also assign them a random username using something like coolname).
Create a User instance for their chosen username with is_guest=True without any password and now you can save all the game information anyway corresponding to this user.
Tomorrow, when they choose to register, you can simply update the already existing User instance and mark is_guest=False.
EDIT:
If you don't want the added complexity of your own User model, you could also achieve the same without subclassing User model and just adding the is_guest column to the IntervalsProfile model

Is custom Django user model with only one field possible?

I need a custom user model in Django with only one field to authenticate. Because the users of my website will not have an email or password. They will be logged in with only one string (like token generated immediately). If the string matches with the another string/token defined by my system, the user should be logged in.
How could it be possible?
Thanks

extending the user model to create a user2 model

I have a requirement of two types of login in my django project where one login is for students and one login is for teachers.
I have gone through the Django documentation and other internet resources, and I have come up with a simple design solution but I am not sure about the pros and cons as I am still quiet new to Django.
Solution I thought to solve this Problem:
1) For student login, I have succesfully integrated django-allauth and it is working fine.
2) Now for the teacher login, I am thinking to build a model as follows:
class Teacher(models.Model):
teacher = models.OneToOneField(User,unique=True)
identifier = models.CharField(max_length=128)
#other fields
3) Then two forms over this model - Signup and Login for teachers and email verification form: Here I thought that I will create teacher object and student object on successful signup but I will set is_active=False and ask for email verification.On successful verification, I will set is_active=True so that a teacher can successfully login.
4) Avoid students from logging in the teachers section and teachers can login in the students section: Here I though of an identifier field to avoid authenticated students to login in the teachers section.
Please can anyone help me by providing your opinion on this solution or by suggesting some better alternative as I am still reading up more and more Django Documentation on this.
Thanks in Advance!
You could create a single model 'user' and define there permissions by adding them to a group.
And have your Django application for teachers check if there in the group teacher.
You could create separate forms or check based on email (name.student# or name# ) and
before saving the model adding the group.
Keeping is_active on False is alway a good idea if you want to verify that a 'user' has given a correct email.
I did an application which needed different permissions levels for Students, Teachers and a few different other User categories. I'm not sure if it's the best way to do it, but I did it by creating one UserProfile, as Eagllus mentioned, which had several categories for what kind of user profile it was. Something like:
PROFILE_CHOICES = (
('TE', 'Teacher'),
('ST', 'Student'),
)
class UserProfile(models.Model):
user = models.ForeignKey(User)
profile_type = models.CharField(max_length=2, choices=PROFILE_CHOICES)
'''other attributes'''
You can then decorate your views so only users of a certain type can access them.
Its not clear to me what you are trying to do in #3. It sounds like you want the user to be added as a teacher and student but only make them active by email verification?
I'm not familiar with django-auth but django-registration provides user registration where user account are activated via email. This may save you a significant amount of work.

Django password and authentication for non standard users

I have been asked to introduce an unusual case and I'm wondering how others would go about it.
I have users in my Django application. The model is a standard user model, authentication. etc. Each one of of these site users can add their own contacts to the system. However my new requirement is to allow their contacts to set a password (if they want to to) so that they can login to their status page (belonging to that user).
So my question is how would you do this? I already have the contact table (which belongs to one user), I'm thinking of adding in a password (optional) field, but then I'm unsure how to handle the authentication for this as they are not my users but members of my users (if that make sense).
One way would be to create another user model for contacts inheriting from AbstractBaseUser. And then creating custom auth backend that would look in both models to login user. Finaly you would have to distinguish between standard user and contact user before every action.
That is if contact user and standard user differ significantly in your application.
Or you could just create custom user in your application, that would contain is_contact attribute. This would be used for both types of users. You would set that as AUTH_USER_MODEL in settings and check before every action for the is_contact attribute to determine the outcome. You could return 403 for the contact user if he tries to access what he's not suppose to.
Or if you use permissions in your application, you could set the contact user's persmissions only to view statuses of the users that added him as a contact and nothing else.

How to authenticate user in Django Comment framework?

I'm using Django's comment framework as a part of my project. With default settings, anonymous users can make comments at will.
I want to change this feature such that only authenticated users can post comments. Moreover, I want this authenticated user's name to show up next to the comment.
How do I go about doing so? I've read up on the documentation, and I understand the pre-defined comment model has a "user" field which is a ForeignKey to the User model / user who posted the comment (Link here). However, I don't understand how to assign request.user (i.e. the current authenticated user) to this user field that belongs to the instance of the comment.
In other words, how does Django process the form data on the front-end to the Comment model in the back-end, and how can I edit this process such as to assign request.user to the user field in the comment model.
Thanks for the help!
Start from the documentation
Basically you need to (at least):
enable django.contrib.auth in your settings.py
define login view
use #login_required decorator on the views you want restrict
check if request.user.is_authenticated() in your form processing code.

Categories