Django-registration combine databases - python

I've installed the django-registration app succesfully and it works great.
But now I want that when people are logged in, they have to fill in more data about theirself.
Do I need to create an new django-app, so with a new database, or is it possible to save the data in the djano-registration app database (the database with username, password and e-mail)?
And how should I link these two databases, so everybody have his own, unique "index"-page with correct 'place'/link in/to the database?
Thanks a lot!

Look at the User-Profiles section of the auth application. It explains how to create a Profile object (basically a new table, not database), that is connected to the User object, and has whatever data you add to it. It can be retrieved with get_profile().
Just to highlight something in the docs - get_profile() does not automatically create a Profile for you, you need to manually create one each time a User is created. This Answer gives sample code for using Signals to create a profile - after a User is created, it sends a signal to any registered process. You would need to code, and register a profile-creation function with the signal.

The author of django-registration has also done a nice profile app. Wrapping around the User-Profiles.
Check it out at:
https://bitbucket.org/ubernostrum/django-profiles/

Related

How do I pre join data for django web application

On my iOS app, when a user logs in I start a series of firebase listeners for my users data... Profile, Accounts, Campaigns, etc..
How and where would I do something like this is django/python? I have a firebase db. I configure it in the top of my views.py, I log the user in on my login view. At this point in the iOS app, I add it to an AppState File and can call it from any view. In django, I ended up calling the data and creating my "joins" in the actual view like so:
campaign_list = []
results = db.child('campaigns').get(refresh_token)
for campaign in results.each():
campaign = models.Campaign().db_snapshot(campaign)
result = db.child('accounts').child(campaign.userUUID).get(refresh_token)
campaign.account = models.Account().db_snapshot(result)
Now I have my campaign object and attached the account object. db is set from the config at the top of my views.py. I believe this the correct way? I know its weird to use firebase and django, and I'm slowly switching to postgresql, but in the mean time, I have to continue to use firebase.
So how do I create a so called snapshot of my user, then access the data when they visit specific views. Aka, create a list of accounts, then on the accounts view, show the list. Instead of creating the list when the user visits the view.
Is this caching? haha
I've read two tutorials on elastic search and that seems like one of the tools I will need. But I also create lists of the data. I.e. I add analytics to the account data. Like a join. Is this possible in elastic search?
The other talked about create_command core. This was interesting and potentially what I need, however, if I placed my listeners in there, it would start on the WebApp startup, but wouldn't know the logged-in users info, so I would have to create every list upfront...? The tutorials that I read on django didn't really go into this a whole lot. And I know Im supposed to keep sessions light.
Any help and links are appreciated!
Denis Angell

OneToOneField(user) imply recreating all account?

I extended user class with a new class "Profile" using OneToOneField. It worked but now I can't reuse old account. (So user,superuser and admin). I get a "RelatedObjectDoesNotExist" Error since my old account do not have any "Profile". I suppose their is an other way than recreating accounts (Because I can't imagine it in production every new version) but I can't figure out how to update every old account. Does someone has any clue?
In some case, when you update your model(s), you have to write data migrations in addition to the schema migrations - see https://docs.djangoproject.com/en/1.10/topics/migrations/#data-migrations for additional information
In your case, you should create a data migration to create Profile object for every existing user. Follow the example in the documentation - it is pretty good.
One important note - you CAN'T just import your Profile model in the migration. You must do it like that:
Profile = apps.get_model("yourappname", "Profile")

How do I attach a different database to each user in Django?

I am building a calendar and appointment website in Django. I want each user to have their own database. I want to know firstly how do I create and save user details and then how do I attach each user to their database??
Thanks in advance.
To attach multiple databases/users, you just need to specify them in the settings.py, see https://docs.djangoproject.com/en/1.9/topics/db/multi-db/.
If you want to create initial data for each users and for each database, you need to use the fixtures, see https://docs.djangoproject.com/en/1.8/howto/initial-data/

Adding custom attributes to django scheema

I am trying to authenticate my django application written in python with okta IDP. I have almost configured everything at SP side and IDP side too. Now I need to pass a custom variable from IDP which assert SP that user is a publisher,editor or admin and further save this to the django format database (in auth_user_groups table). Anyone have tried doing this, or anyone has idea about this?
I am able to get the custom variable values by attributes mappings from IDP. But this allows me to save the custom attributes only on the user table. please let me know if i have not made myself clear here about my question.
Once again I have a privilege to answer my own question. So hear is the solution.
Django has a user profile module which is to be turned on by giving the module location in the settings.py
i.e -
"AUTH_PROFILE_MODULE = appTitle.UserProfile"
The UserProfile needs to be specified in modules.py specifying the required structure of user profile u need for your app.
Now doing sync -db django creates the Database table for your user profile and further on the same user profile pysaml adds the value (CustomAttribute) which come on the saml Assertion.
more explanations on this can be found on django documentations too.
If any one still faces any issue please let me know.

How do you find the login provider in Django-Allauth?

I saw the code in the accepted answer for this question:
How to access user names and profiles with django-allauth
But when I run a template with {{user.get_provider}}, nothing appears. I was expecting it to say either "LinkedIn Oauth2" or maybe "native". (Those are my two ways to log in.)
Are there special things you need to get the template calls working? Other template items are working fine, such as account.get_avatar_url.
To my knowledge the user profile doesn't record which credential was used to establish the current session, nor as far as I am aware does a list of a particular account's associated credential types automatically populate into the user context object (I'm not sure which you were trying to get from the question you asked).
You can access what credentials an account has available to it in python & export these to the context. See the socialaccount/connections.html template that comes with django-allauth as an example.

Categories