Django can't delete user from my database - python

I have a problem with my Django project. I need to delete 1 or more users from my database but I can't do it because it returns this error:
__str__ returned non-string (type tuple)
I tried deleting it from my views but it didn't work. Than I went and used admin panel to try to delete it but the same problem happened.
If someone could give some advice or suggestion that would be great.
Thank you.

I found out what was the problem. I was returning tuple in my models __str__ function instead of a string which worked while users existed but it for some reason didn't when I tried to delete or as I later found out create a new user.

Related

Django DetailView pk error: can't match the last one

My new to django. I am creating a simple model for my to do app in django. Here, I have used DetailView to get the details and using the int "pk". When I'm running my server the /task/1/ is working fine but when I'm running task/2/ to check the other task it is shwoing error.
My views.py:
views.py
My app urls.py:
urls.py
template of detailview
See, when I'm running task/1/ there's no error.
This means the query of PK 2 doesn't not exists maybe you have deleted it.
Try number 3 or 4 to check.
Try going to your admin page to find Pk of each row.
So, I found the solution. As mentioned in the above comment that I might have deleted my task. Yes, that was deleted and even if we delete our task, the new task will not get the pk number that was assigned to the previous one. So, instead, if we'll use pk as the larger number than the previous number we won't get the error.

Django superuser doesn't have permission to delete models

I am working on upgrading a Django website from Django 1.8 to Django 2.2 and have come across a problem when trying to delete an object from the Django admin changelist view. When I try to delete an object, I am shown a screen with the words "Deleting the selected Records would result in deleting related objects, but your account doesn't have permission to delete the following types of objects". The problem is, I am a superuser and should have permission to delete any object I want, or so I thought.
I have read a decent amount of documentation on Django upgrades and permissions of superusers, however I haven't stumbled across anything that helps me (I could be missing something though). I have tried creating a new superuser just in case, still the same message occurs though. Anyway, Does anyone have any idea why this could be happening?
Note: I can't show any code because I am working for a company and I signed an NDA. Just some help pointing me in the right direction would be appreciated.
For anyone else who stumbles across this problem, dirkgroten was right and it turns out the has_delete_permission was overridden! Thanks for your help dirkgroten!
OPENEDX If You are facing This issue then do this thing from the backend.
Cannot delete user
Deleting the selected user would result in deleting related objects, but your account doesn't have permission to delete the following types of objects:
course enrollment
Open Mysql In EDX server like AWS
then
get user id based on username
then student_courseenrollment delete from this user
Example
select id from auth_user where username = 'exampleusername'
delete from student_courseenrollment where user_id = get user_id from 1 query set;
like delete from student_courseenrollment where user_id = 123;

Django: save() method in a different database

I'm trying to save an object in a non-default database. I have already configured a second connection.
I know that in order to get objects it is Class.objects.using('the_db').all
But when I try object.using('the_db').save() it doesn't work. (Error: "object has no attribute 'using'".
I also tried object.save('the_db') but it also does not work.
How can I achieve this? I couldn't find the correct syntax.
Try object.save(using='the_db')
Relative django documentation: https://docs.djangoproject.com/en/2.2/topics/db/multi-db/#moving-an-object-from-one-database-to-another

Getting KeyError in my django code

I'm new to both Django and Python so please forgive me if I come off as annoying....I'm just very much misinformed!
Error Code: http://i.gyazo.com/68d88cabf536b129dc37cde6c3ae319c.png
I've googled about this 'KeyError' and it seems to be related to clean(). However, the example my lecturer gave me worked ok without it but when I tried to recreate what he gave me I kept getting this error.
A bit of info: I had originally had a ForeignKey for the user for each submission so I changed it to a simple form to fill in (not a permanent solution) but I still get a KeyError.
Here is my models, views and forms:
http://pastebin.com/rAX5PDHQ
Sorry if I left something out. I'll respond ASAP if you all need more info.
Again, sorry if this is a silly question. But I'm utterly lost to be honest.
Thank you!
PS Sorry I really tried the code formatting but I kept getting an error saying it was incorrect thought the preview said it was ok. And I can't post more than one link.
You don't have a field named user in your form. Try changing the relevant line to:
bd = BloodData (respondent=cd['respondent'],
in your "storeBloodData" view.
The problem seems to be in your view storeBloodData,at this point.
bd = BloodData (respondent=cd['user'],
The form has no field named 'user'.You may replace it with a relevant field present declared in the form.
Also, it is better to use DICT.get(key)when you are not sure if the dict contains that particular key or not. This way you'll simply be returned None when the key is absent and you'll be able to dodge KeyError.

Django: After migration, all model fields with unique=True get IntegrityError Duplicate Entry for Key

I have a model with a uniqueblogname element that is set to Unique=True. In my views, I do something like:
try:
MyModel.objects.get(uniqueblogname=userinput) #i ask the user to input
#a name of a blog they want to own on the site (all blogs must have a unique name)
#if taken then prints some error messages that the blog name is taken.....
except MyModel.DoesNotExist:
MyModel.objects.create(uniqueblogname=userinput) #if no blog has that name then
#the blog object is created
I recently updated my db by doing dumpdata and loaddata to a brand new blank db and migrating new model changes to this db (I wanted to keep the old db how it was and archive it). Everything went smoothly. New migrations were made without errors. However, now whenever I execute the above check, if I search for any old blogname then I get this error:
1062, "Duplicate entry (the user's input here) for key 'uniqueblogname'"
However, if I search for a completely new blogname and the object gets created then I search for it again using this check, everything works fine. It seems as if the code for trying to retrieve the old MyModel objects does not get properly executed and django tries to create a new MyModel only to realize that it would be a duplicate and so throw this error.
The most confusing part about this error is like I said: If I create a completely new blogname and enter it, and the object is created freshly within this new db, this check condition works every time perfectly (so I don't suspect it is a logic issue in my code). However, I can't seem to check my older stuff that I used loaddata to populate.
Any ideas? Very appreciative of any suggestions. Thanks.
It's hard to comment on your specific case without knowing more. I'd start by looking at the database itself and seeing if there's anything different about your old rows as opposed to the newly created rows.
I do want to point out that your current code has a race condition, since another process could insert a row with the same name in between the get() and the create(). I suggest using get_or_create() instead.
This method is atomic assuming correct usage, correct database configuration, and correct behavior of the underlying database.

Categories

Resources