I am using Zinnia Blog module with my Django app. I have a Django app and have a custom User model. I have added the following setting:
AUTH_USER_MODEL = 'account.Member'
However, when I try to create a new entry in the blog I get the Foreign Key constraint error.
Exception Type: IntegrityError
Exception Value:
(1452, 'Cannot add or update a child row: a foreign key constraint fails (`example`.`zinnia_entry_authors`, CONSTRAINT `author_id_refs_id_410656e2874b02ff` FOREIGN KEY (`author_id`) REFERENCES `auth_user` (`id`))')
It references the default auth model (which doesn't exist in the database) instead of the specified auth model. How do make Zinnia use the correct table.
Thanks.
Related
I have created a custom model using ABSTRACTBASE USER in django. Now i am trying to generate auth token and it is showing Foreign key constraint fail error. I a able to login and register the user to the custom model. My AUTH_USER_MODEL is set to the custom model i have created
In the form to add new objects in Django admin, sometimes there are linked foreign keys that are selectable in a dropdown. Is there a way to include a searchbox for the foreign keys within the form?
For example, let's say I have a Book model with a foreign key to Author object. Instead of scrolling through all the author ids when I create a new Book object, can I search for Author "James" specifically?
Use autocomplete fields in your custom admin.
Django Documentation - Autocomplete Fields
Item has foreign key to User and User has OneToOne to Profile.
I have admin for Profile. How can I show all items as inlines?
I do not have foreign key from Profile to Item, so I need something like user__itemset but it seems it can't be used in Django admin.
Unable to complete migration for rest_framework.authtoken
Running migrations for authtoken:
- Migrating forwards to 0001_initial.
authtoken:0001_initial
FATAL ERROR - The following SQL query failed: ALTER TABLE "authtoken_token" ADD CONSTRAINT "user_id_refs_id_14b35167" FOREIGN KEY ("user_id") REFERENCES "users_user" ("id") DEFERRABLE INITIALLY DEFERRED;
Error in migration: authtoken:0001_initial
DatabaseError: relation "users_user" does not exist
Using version djangorestframework-2.3.8
Because of the custom user table name the migration isn't happening. Go to the initial migration and specify your user table using db_table in the migration where the code is trying to access your custom table. That should work.
are you using a custom user model?
if this was the case the
migration code for the app implementing the user model should look like:
class Migration(SchemaMigration):
needed_by = (
('oauthtoken', '0001_initial'),
)
I have a Django 1.4 site that uses the Django comments app. I'm upgrading my dev version to Django 1.5 and extending the User model - I have a Person model which extends AbstractBaseUser, and AUTH_PROFILE_MODULE = 'membership.Person' in my settings.
At the moment the django_comments postgresql database table has a column user_id which references auth_user(id):
"django_comments_user_id_fkey" FOREIGN KEY (user_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY DEFERRED
There's nothing in that column because, so far, there are no user accounts in the system, other than my own admin user. I think I need to update the table to refer to my new Person model (which is stored in the membership_person table). In the future users will be able to create accounts and post logged-in comments.
What ALTER TABLE command should I use to change django_comments to refer to my new Person model? I'll have to do similar to the django_comment_flags table. Is there anything else I should watch out for?
I ended up doing this for the comments table:
ALTER TABLE django_comments DROP CONSTRAINT django_comments_user_id_fkey;
ALTER TABLE django_comments ADD CONSTRAINT django_comments_user_id_fkey FOREIGN KEY (user_id) REFERENCES membership_person(id) DEFERRABLE INITIALLY DEFERRED;
And similar for the comment flags table:
ALTER TABLE django_comment_flags DROP CONSTRAINT django_comment_flags_user_id_fkey;
ALTER TABLE django_comment_flags ADD CONSTRAINT django_comment_flags_user_id_fkey FOREIGN KEY (user_id) REFERENCES membership_person(id) DEFERRABLE INITIALLY DEFERRED;
Hopefully that does the job and doesn't cause any problems. *crosses fingers*