Django broken OneToOneField - django

I have an app running and I made a huge mistake, I've deleted a user from the admin site of my Django app before deleting it's UserProfile.
Now, when I try to see the UserProfiles from the admin site, I keep getting an error, for what I've seen it's a record not found exception.
I've got this problem because I cannot use any backup as the latest one is half a month old and all my users will get really mad if they loose all data.
So, how can I do it so I can enter in the UserProfile (It would be best if using the admin site) to delete the UserProfile?
Thanks

Try doing it from the shell!
You need some way to find that UserProfile, do you have an ID of the user or maybe of the UserProfile itself?
python manage.py shell
>>> from your_app_thing.models import UserProfile
>>> user_profile = UserProfile.objects.get(id=<user profile id>)
>>> user_profile.remove()
Alternatively, you could add UserProfile to the admin panel?
from django.contrib import admin
from .models import UserProfile
admin.site.register(UserProfile)

Related

How to login to Django Admin panel with user I created in this panel? User model was extended

The model itself:
from django.db import models
from django.contrib.auth.models import AbstractUser
class UserModel(AbstractUser):
class UserType(models.TextChoices):
MANAGER = 'm', 'Manager'
CUSTOMER = 'c', 'Customer'
first_name = models.CharField(max_length=120)
last_name = models.CharField(max_length=120)
type = models.CharField(choices=UserType.choices, max_length=1)
USER_MODEL is registered in settings.py.
in admin.py it's registered as:
from django.contrib import admin
from .models import UserModel
admin.site.register(UserModel)
I can create new model in the panel with existing superuser I have. i.e. - it works.
I can add new super users with manage.py and they appear in the same place in the panel.
But later on I can't login with users I created in that panel.
saff status - checked.
The problem might be with passwords, because those created with createsuperuser shown hashed in the panel or I don't know even.
If I did smth completely wrong - let me know, I only need to extend users to have a couple of additional fields.
Django version 3.2
No worries django doesn't store raw passwords it always hash the password for security reasons, imagine someone hacked into your database people usually use the same password for all websites, not nice huh?!
so try python manage.py changepassword <user_name> and write your new password
and access the shell to check is_staff attr

wrong url to admin django after adding profile

I had a login page that I would change it to another login page and I follow these instructions.
I added this code and when I tried to login to my user admin that I added before it sends me to wrong url http://localhost:8050/admin/login/?next=/admin/ and it throws :
RelatedObjectDoesNotExist at /admin/login/
User has no profile
The profile instance is generated on post_save signal, that is, you must save your User at least once after you added that Profile class.
The easiest workaround in your case would be to create a new admin user using python manage.py createsuperuser.
The error seems like, You have a User in DB, but it doesn't have any profile relation now.
Solution:1
Set profile instance for currently existing User's in db through django shell.
1. log into django shell by python manage.py shell
2. run these commands,
from django.contrib.auth.models import User
from myapp.models import Profile
for user in User.objects.all():
Profile.objects.create(user=user)
3. then login to the django admin
Solution:2
Drop your database, (if you are using sqlite, the delete the corresponding file) and migrate and then create a new super user by python manage.py createsuperuser command

Django: 'no such table' after extending the User model using OneToOneField

(Django 1.10.) I'm trying to follow this advice on extending the user model using OneToOneField. In my app 'polls' (yes, I'm extending the app made in the 'official' tutorial) I want to store two additional pieces of information about each user, namely, a string of characters and a number.
In my models.py I now have the following:
from django.contrib.auth.models import User
class Employee(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
stopien = models.CharField(max_length=100)
pensum = models.IntegerField()
and in admin.py the following:
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from polls.models import Employee
class EmployeeInline(admin.StackedInline):
model = Employee
can_delete = False
verbose_name_plural = 'employee'
class UserAdmin(BaseUserAdmin):
inlines = (EmployeeInline, )
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
When adding a user using the admin panel my two new fields display correctly. However, when I click 'save', or if I don't add any user and just click on the name of my sole admin user in the admin panel, I get the following error:
OperationalError at /admin/auth/user/1/change/
no such table: polls_employee
I see some questions and answers related to similar problems, but they seem to be relevant for older version of Django. Could anyone give me a tip as to what I should do? Ideally I'd want my two additional fields display in the admin panel, though I suspect this might be a task for the future.
I have to confess I do not understand this paragraph from the documentation just following the advice I'm using:
These profile models are not special in any way - they are just Django models that happen to have a one-to-one link with a User model. As such, they do not get auto created when a user is created, but a django.db.models.signals.post_save could be used to create or update related models as appropriate.
Do I need to tie this 'post-save' to some element of the admin panel?
I'd be very greatful for any help!
You need run makemigrations to create a migration for your new model, and then migrate to run the migration and create the database table.
./manage.py makemigrations
./manage.py migrate

Migrate model while adding user foreign key

So i have this model where I added user field as foreign key to User model
from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
class Post(models.Model):
url = models.URLField()
title = models.CharField(max_length=140)
user = models.ForeignKey(settings.AUTH_USER_MODEL)
Than I run
python3 manage.py makemigrations
And get:
You are trying to add a non-nullable field 'user' to comment without a default;
we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
I understand that I need to give it default user but I don't get how to do that.
P.S. Django version - 1.8.3
Ok, so it happens migration utility's question hadn't appeared intuitive enough for me. What migration utility was really asking were required fields of User model. So basically you need to type in some data 3 times (for username, password and email). The problem is utility isn't really saying what field is it expecting.

add an image field to admin.auth admin site in django

Hi ive not be able to find any one trying to solve this issue yet so i thought id ask the question here. Basically im trying to add an image field to the admin.auth users admin site that relates to my userprofile model.
Aswell as users that register to the site, the admins need to be able to specify a profile picture when the create a user in the admin site that will be used for posts they make to news and blogs etc.
Is this at all possible, i have the userprofile model working which will allow me to add the image but i want to include this in the admin user panel when an admin creates a new user.
Any help is greatly appreciated!!
Thanks in advance
You can add your UserProfile (with the extra image field) as an inline to the User adminModel. So when you go to the admin.auth panel to add a new user, there will be an inline below it with all the UserProfile fields.
Something like this should do it (in admin.py of whatever app your userprofile is in):
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from django.contrib import admin
class UserProfileInline(admin.StackedInline):
model = UserProfile
class UserModelAdmin(UserAdmin):
inlines = [UserProfileInline,]
admin.site.unregister(User)
admin.site.register(User,UserModelAdmin)