I want to create a user for my website that can submit a request form
but I'm wondering which is suitable choice :
To use a user object that already created
Create a user model from scratch
Note: I will let the users to login in my website from the active directory
Only If your User object need more than primary attributes like Nick Name, Skype or Phone No, etc... whatever.
In this case you can extend to use your own User object by creating ...
class Profile(Model):
user = models.ForeignKey(User)
nike_name = models.CharField(max_length=200)
For more details User Object
Related
I created a model having one-to-one relation with default User model to extend it for providing some permission for tabs in my website. Now I want to not show the tab options to the users not having permissions to access them.
user permission model:
from django.contrib.auth.models import User
class userpermissions(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
Checking whether the user has permission or not I created a function which is supposed to get the userpermission model instance of the logged in user. For which I used the following code:
def check(user):
up = User.userpermissions_set.get(id=user.id)
Now when user logs in the login function will call this check function and redirect to the tab that the user can access and also send the up variable containing all the permissions of the user so that the Navbar template can hide those tabs which the user has no access to.
Now the problem that I am facing is when I run the server it shows this error:
AttributeError at /
type object 'User' has no attribute 'userpermissions_set'
I saw on YouTube that to get the instance of child model (I don't know what to call it) we need to use it's name all in small case with _set (e.g. parent.child_set.all())
As per the error on browser it seems the wrong way to get the child model data (again - I don't know what to call it).
I was expecting to get all child model data but it did not work.
I am looking for a tool to track user actions like:
user logged in
user changed password
user got bill via email
user logged
user uploaded image
user send message
...
which I can include into my Django project. Afterwards I want to build queries and ask the system stuff like:
how often did a user a message within a month
how often did a user login within a month
does the user uploaded any images
and I would like to have some kind of interface. (Like google analytics)
Any idea? I am pretty sure that this is a common task, but I could not find anything like this.
There are many ways to achieve that. Try reading this link first. Also, you can use LogEntry for tracking the creation, deletion, or changes of the models you have. Also, it shows you the information you need in the admin panel, or also you can use some other third-party packages.
Or you may want to create your own Model to create logs for your application and this link may help you, but do not reinvent the wheel and analyze your situation.
from django.contrib.admin.models import LogEntry, ADDITION
LogEntry.objects.log_action(
user_id=request.user.pk,
content_type_id=get_content_type_for_model(object).pk,
object_id=object.pk,
object_repr=force_text(object),
action_flag=ADDITION
)
Create a model to store the user actions. OP will want the Action model to have at least two fields - user (FK to the user model) and action (user action).
from django.db import models
class Action(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
action = models.CharField(max_length=255)
Create a way to store the user actions.
def store_user_action(user, action):
action = Action(user=user, action=action)
action.save()
Then if one wants to store when the user changed password, one will go to the view that deals with the change password and call our method store_user_action(request.user, 'changed password') when successful.
To then visualise the user actions, OP can see the records in the Django Admin, create views and templates, ... There are different possibilities.
I have an model named Customers(username,password ..etc) and also an model named User(username,password...etc).
I want to create two different APIs with different authentication.
One should authenticate with the User username,password
and the second should authenticate using the Customers username,password.
Any idea on how can I do this?
Thank you!
I suggest the following options:
1.
I am assuming User model is the "real" user of your app. If this is true use the django's default User model class. It will work out of the box.
For the Customer model, make it inherit from AbstractBaseUser, this will give you password functionality out of the box and you can add other fields as per your need.
Now you can create 2 different urls for login. 1 url for user which checks in the User model and the other for the customer model. This avoids any confusion for everyone.
If you prefer a single url, you have to mention the model class along with username and password to know in which table to verify them.
2.
Create two profile models: UserProfile and CustomerProfile
Each will have a one to one relationship with the django's default User model.
Basically a User can have the profile of a "real" user or of a customer.
In this case when you are creating any User you have check if you want to attach a UserProfile or a CustomerProfile.
In this case it makes sense to just use a single login url. From the user's login information you can first fetch the user from the User table and then check if it is a customer or not by running a query in the CustomerProfile table.
I recommend you to use the django.contrib.auth.user class for your classical authentication. You can either inherit from that class or add a OneToOne relation to your own model as follows
from django.contrib.auth.models import User
class YourUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
For the rest of your question you should add some more details and even some pieces of your code.
I'm new to Django, and I'm trying to build an app where a user can post a "trip" (while logged in with django-allauth, facebook social account) and I want that trip model to be tied into the social account of the user whom created it.
I'm having problems saving the trip to the database. I've had to modified the inputted data with Javascript so I'm using an ajax POST request, and I grab the user's social id with <p class="social-id">{{ user.socialaccount_set.all.0.uid }}</p>
My model is:
class Trip(models.Model):
driver = models.OneToOneField(allauth.socialaccount.models.SocialAccount)
and I get an error that says: Cannot assign "'10206730564687878'": "Trip.driver" must be a "SocialAccount" instance.I tried making the model driver field reference the allauth.socialaccount.models.SocialAccount.uid but that didn't work either.
You are using a OneToOneField. Can every user only post one trip? If you want them to be able to post more than one, you should use a ForeignKey() instead.
Also, is there a reason you are linking it to the SocialAccount instance, instead of to the main User instance? I would use
class Trip(models.Model):
driver = models.ForeignKey(User, models.CASCADE, related_name='trips')
Now, the reason you are getting the error is, because you are trying to assign a str (text string) to the OneToOneField field. But that field expects an instance of SocialAccount.
To fix it, instead of doing trip.driver = "10206730564687878", you would need to fetch an instance of SocialAccount (for example for the current user) and then assign that to the driver:
soc_acc = SocialAccount.objects.get(user=request.user)
trip.driver = soc_acc
trip.save()
That would give a SocialAccount instance to trip.driver.
And another thing: if it is the logged in user who adds himself, there is no need to send any SocialAccount ID with the form. Rather, get it from request.user object of the logged in user.
I am using Django 1.7.
I need to develop a user registration model in which at the time of registration, user will have to enter an employer also along with username, email and password. The employer name should be there in the database. Besides the custom user model, I am using another model named employer.
What would be the best way to implement this through a custom registration?
the cleanest way, using OneToOneField(User)
class Employee(models.Model):
user = models.OneToOneField(User)
//here goes your others employee attributes
So when you create your user model,also create its employee instance.