I implemented Django Admin in my web for administering all tables in my models. It´s working correctly until I try to see (for editing, adding, deleting records) a random table without primary key. If I try to access to this table the message unknown column xxx.id in field list appears.
I know Django is adding id field to tables without primary key. What could work for me? I dont mind if I have to show id field in list_field but if I try Django warning me this field doesnt exist. Could I define a unique value for solve this (my tables which doesnt have PK, has unique together instead...)
I tried to add a primary key property to a random field in the model´s class with those issues. That is working but I need to not have restrictions over fields and if I add this PK I cant repeat this value in others records.
How can I solve this?
答案 0 :(得分:1)
If you've made changes to the model in question, you might need to migrate. In your project's root directory (or wherever the manage.py
file is) type
$ python manage.py makemigrations
Then type
$ python manage.py migrate
It could be that you added a model field to the model, but it's not in the database yet, so you get this error when you try to edit it in admin.
UPDATE
Based on this question and this one, it sounds like Django tried to auto-add an id
column since you didn't define a primary key on your model. That id
may not have propagated to the database. Therefore try
$ python manage.py dbshell
Now you can add the column straight to the database table
mysql> ALTER TABLE your_table_name ADD id integer AUTO_INCREMENT NOT NULL PRIMARY KEY FIRST;
mysql> exit;
Then try reloading your admin page...