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.
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 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
I want to create a two-sided benefit referral system, with different types of referrals in django using the anafero app.
I looked at this article and looked at the documentation for anafero and I cannot make sense of it. Can someone please give an simple example of:
What database changes need to be made, i.e. which models need to
be created and how the user object needs to be changed
Which view needs to be created
How to display the referral url in the template
Anafero is outdated. Instead of it you can use pinax_referrals
Install the package as it described in documentation, add middleware etc, synchronize database.
Depending on what do you use for signing up hack the view. In my case I have reimplemented SignUp view and added following lines inside:
profile = user.userprofile
referral = Referral.create(
user= user,
redirect_to= "/"
)
profile.ref = referral
profile.save()
You see that when User signs up the Refferal object is created and saved into User profile. It means that you have to change the model for UserProfile and add additional field:
Import:
from pinax.referrals.models import Referral
Inside model:
ref = models.OneToOneField(
Referral,
null=True,
on_delete=models.CASCADE,
)
Make migrations, migrate, check in the admin if new fields are added, try to sign up and see whether the new Referral object is created and saved.
Hey guys I have created a messanging app in django in which i have User.auth,Profile and message app which works fine. Now i need is to add contact category in every user accounts. So they can add contacts like email with there firstname and last name?
You should be able to update your Profile model to include a many-to-many relationship with other Users. Then you can access the related user models for a given profile's contacts through that profile. In the profile model:
contacts = models.ManyToManyField(User)
Set blank/null as is appropriate.
After authentification with social auth plugin, plugin create new user with not beauty usernames like sergey.kostin.345, I know that some users has a nice shorturls on the social media platforms and its ok for default behavior but I want to give user ability to change user names. As far as I understand django auth system does not let me to change User.username field by using methods. I also tried to change this field by using this code, but it seems to be ignoring in django.
owner = User.objects.get (id=request.user.id)
owner.username = newusername
owner.save()
owner is authenticated user
That would work, but there's no need to get the user again. request.user is already the user object.
owner = request.user
owner.username = newusername
owner.save()