"That username is already taken." but there is no entry in DB - python

In my environment I'm trying to create a new superuser via python .\manage.py createsuperuser.
However, when I enter the name I get the following error: Error: That username is already taken..
So I check the auth_user table in PgAdmin and I can see there are no entries.
Moreover, the QuerySet is empty when I do the following:
from django.contrib.auth.models import User
superusers = User.objects.filter(is_superuser=True)
superusers
>>> <QuerySet []>
I'm using a managed cloud-hosted PostgreSQL server with the postgresql_psycopg2 client, and have checked that all credentials are correct.
What am I doing incorrectly? I'm using Django 4.1.2.

Related

How to run management command from django view.py

I want to run my custom command from django view which create new user. Here is my command
python manage.py tenant_command createsuperuser --schema=schema_name
Here schema name my be change
Above command is same as
python manage.py createsuperuser
Here i didn't know how to pass username, email and password and confirm password any suggestion would be appreciated
There are two ways to achieve what you trying to do:
Using code to change the user object and save it to database
Calling the command you mentioned
According to django docs you can call your commands in code using call_command function, it takes arguments and pass them to command too:
call_command("custom_command", arguments..)
In the other way you have access to User model in your view so you can import it directly and create the user:
from django.contrib.auth impor get_user_model
get_user_model().objects.create()

password authentication failed for user "saleor"

password authentication failed for user "saleor"
FATAL: password authentication failed for user "saleor"
when migrating the first time
if it's something related to database please someone help me to figure this out
I'm using Postgres as the saleor documentation says but I can't pass this migrations point
I read somewhere I have to create a database called saleor with saleor as a user with password 'saleor' who is a superuser and if that is the solution tell me how to do that
DATABASES = {
"default": dj_database_url.config(default="postgres://postgres:12345#localhost:5432/saleor", conn_max_age=600)
}
Postgres is my username, and 12345 is my password for username i.e postgres and saleor is database name. you can change database name to any. If you did not change username,password, and database name the it takes default one.
Step 1: Remove everything inside migration folder for app, except the init.py file.
Step 2: Delete or Truncate Your Current Database which is 'saleor'
Step 3: Create the initial migrations and generate the database schema using folowing commands
python manage.py makemigrations
python manage.py migrate
Step 4: Now you can Create new Super user using following command
python manage.py createsuperuser

Extending AbstractUser in django

I was trying to subclass AbstractUser and stuck in an error on running migrate, and makemigrations says No changes detected
django.db.utils.ProgrammingError: relation "auth_group" does not exist
model:
class SubClient(AbstractUser):
client_id = models.ForeignKey(Client)
phone = models.CharField(max_length=15)
added in settings.py:
AUTH_USER_MODEL = 'myadmin.SubClient'
This error means the auth_group table does not exist in your database. Which means you did not run Django's migration files (python files describing database structure and its changes over time).
As you have your own models, you first need to create migration files for them by running python manage.py makemigrations.
Then run python manage.py migrate to run all migrations (Django's + yours), this will create all database tables (including auth_croup).
Read the doc to lean more about migrations.
when using AbstractUser could i use django's user's builtin password-reset workflow such as password-reset, password-reset-done etc.
the reason i am asking is that i extended user model using AbstractUser but these built-in function not working and i do not get any error but it redirects me to search page and there is no documentation on the internet regarding this issue:
from django.contrib.auth import views as auth_views
path('password-reset/', auth_views.PasswordResetView.as_view(template_name='accounts/password_reset.html'),
name='password-reset'),
path('password-reset/done/',
auth_views.PasswordResetDoneView.as_view(template_name='accounts/password_reset_done.html'),
name='password-reset-done'),
path('password-reset-confirm/<uidb65>/<token>/',
auth_views.PasswordResetConfirmView.as_view(template_name='accounts/password_reset_confirm.html'),
name='password-reset-confirm'),
path('password-reset-complete/s',
auth_views.PasswordResetCompleteView.as_view(template_name='accounts/password_reset_complete.html'),
name='password-reset-complete')

Get user object using userid in django

Hello i am new to django,
i am creating an authentication system using django.
Once a user is logged in i am storing the value in a session.
user = authenticate(username=username, password=password)
request.session['mid'] = user.id
and when i refresh i can receive the session id
uid = request.session['mid']
But i am not sure how to get the userdatas from the user id. can any one tell me how can get the user object using the user id.
Use simple .get() query.
try:
uid = request.session['mid']
userobj = User.objects.get(id=uid)
except User.DoesNotExist:
#handle case when user with that id does not exist
...
Of course, you can store the user id in request.session, and query the id
with django ORM manually.
But after installing the SessionMiddleware and AuthenticationMiddleware middlewares, on a higher level, Django can hook this authentication framework into its system of request objects. I believe most django projects will use the code below to get authenticated user from web requests.
if request.user.is_authenticated():
user = request.user

How to confirm user created by create_user in flask security mongoengine app?

I have a python flask app using mongoengine and flask-security built up from the examples to expose all of the confirmation, registration, tracking, and recovery functionality.
Everything works except that a user created imperatively in the code using:
MongoEngineUserDatastore.create_user(...)
cannot login. That is, when you try to login with this user, you get an error message:
"Email requires confirmation"
Since the email with a hashed URL has not been sent, there is no way to confirm. Is there a parameter I can pass somewhere to confirm this user on creation or set the confirmed flag somewhere?
Here's my code:
I figured it out by confirming a newly registered user and examining mongodb to see what fields were added. Turns out that the required field for confirmation is confirmed_at, which must have a datetime so:
import datetime
# Create a user to test with
#app.before_first_request
def create_user():
user_datastore.create_user(
email='me#mydomain.com',
password=utils.encrypt_password('password'),
confirmed_at=datetime.datetime.now())
I've updated the gist here:
https://gist.github.com/davidthewatson/327776905ef30815c138
When you create your test user you need to make them active eg:
#app.before_first_request
def create_user():
user_datastore.create_user(
email='me#mydomain.com',
password=utils.encrypt_password('password'),
active=True)

Categories