在这里,我正在努力处理异常,这里是我的数据模型的详细信息。
路线模型 -
class Routes(models.Model):
serial_no = models.IntegerField(primary_key=True)
toll = models.CharField(max_length=1000, blank=True, null=True)
cost = models.CharField(max_length=300, blank=True, null=True)
t = models.ForeignKey('Tollmaster', models.DO_NOTHING, blank=True, null=True)
r = models.ForeignKey(Routeinfo, models.DO_NOTHING)
class Meta:
managed = True
db_table = 'routes'
unique_together = (('serial_no', 'r'),)
但在管理员面板中我收到错误
MultipleObjectsReturned at /admin/tollm/routes/27/change/
get() returned more than one Routes -- it returned 2!
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/tollm/routes/27/change/
Django Version: 1.11.5
Exception Type: MultipleObjectsReturned
Exception Value:
get() returned more than one Routes -- it returned 2!
Exception Location: C:\Users\prash\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py in get, line 384
Python Executable: C:\Users\prash\AppData\Local\Programs\Python\Python36-32\python.exe
Python Version: 3.6.2
我正在使用Postgresql数据库,这是此表的架构,
Table "public.routes"
Column | Type | Modifiers
-----------+-------------------------+-----------
serial_no | integer | not null
toll | character varying(1000) |
cost | character varying(300) |
t_id | integer |
r_id | character varying(300) | not null
Indexes:
"sno_rid_pkey" PRIMARY KEY, btree (serial_no, r_id)
Foreign-key constraints:
"r_rinfo_fk" FOREIGN KEY (r_id) REFERENCES routeinfo(r_id)
"t_id_fk" FOREIGN KEY (t_id) REFERENCES tollmaster1(tid)
任何帮助将不胜感激。
答案 0 :(得分:1)
您的模型与表架构不匹配。表模式在serial_no和r_id上有一个多列主键,而您的模型将serial_no指定为单列主键。
Django不支持多列主键see here for the details
我建议您更改架构以添加单独的自动递增主键列“id”,并将当前主键更改为唯一约束