gpt4 book ai didi

python - 使用旧数据库时 Django MultipleObjectsReturned

转载 作者:太空宇宙 更新时间:2023-11-03 14:26:31 26 4
gpt4 key购买 nike

我正在使用 Djano 开发一个简单的 Web 应用程序来显示和管理数据库数据。我连接了 MySQL 数据库并使用 InspectDB 根据数据库表自动生成模型,这就是我得到的结果,看起来不错。

# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
# * Rearrange models' order
# * Make sure each model has one field with primary_key=True
# * Make sure each ForeignKey has `on_delete` set to the desired behavior.
# * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
from __future__ import unicode_literals
from django.core.exceptions import MultipleObjectsReturned
from django.db import models


class Booking(models.Model):

class Meta:
managed = False
db_table = 'Booking'
unique_together = (('hotelno', 'guestno', 'datefrom'),)

hotelno = models.OneToOneField('Hotel', models.DO_NOTHING, db_column='hotelNo', primary_key=True) # Field name made lowercase.
guestno = models.IntegerField(db_column='guestNo') # Field name made lowercase.
datefrom = models.DateTimeField(db_column='dateFrom') # Field name made lowercase.
dateto = models.DateTimeField(db_column='dateTo', blank=True, null=True) # Field name made lowercase.
roomno = models.OneToOneField('Room', models.DO_NOTHING, db_column='roomNo') # Field name made lowercase.

list_display =
#def __str__(self):
# return ("".join(hotelno) + "".join(guestno) + "".join(datefrom))

class Guest(models.Model):
guestno = models.AutoField(db_column='guestNo', primary_key=True) # Field name made lowercase.
guestname = models.CharField(db_column='guestName', max_length=255) # Field name made lowercase.
guestaddress = models.CharField(db_column='guestAddress', max_length=255, blank=True, null=True) # Field name made lowercase.

class Meta:
managed = False
db_table = 'Guest'


class Hotel(models.Model):
hotelno = models.AutoField(db_column='hotelNo', primary_key=True) # Field name made lowercase.
hotelname = models.CharField(db_column='hotelName', max_length=255, blank=True, null=True) # Field name made lowercase.
city = models.CharField(max_length=255, blank=True, null=True)

class Meta:
managed = False
db_table = 'Hotel'


class Room(models.Model):
roomno = models.IntegerField(db_column='roomNo', primary_key=True) # Field name made lowercase.
hotelno = models.ForeignKey(Hotel, models.DO_NOTHING, db_column='hotelNo') # Field name made lowercase.
type = models.CharField(max_length=255, blank=True, null=True)
price = models.IntegerField(blank=True, null=True)

class Meta:
managed = False
db_table = 'Room'
unique_together = (('roomno', 'hotelno'),)

在这个应用程序的 admin.py 文件中,我包含了这样的模型,这样我至少可以看到那里的数据。

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin

# Register your models here.

from .models import Hotel, Room, Guest, Booking

admin.site.register(Hotel)
admin.site.register(Room)
admin.site.register(Guest)
admin.site.register(Booking)

当我访问默认的 Django 管理页面时,我会看到 tables registered on admin page.我单击“预订”并看到 multiple records ,没有名字(出于其他原因),但如果我点击其中之一,我会得到 MultipleObjectsReturned Error

我已经阅读了我能找到的所有内容,并且我能找到的最接近原因的原因与某些模型中存在复合键有关。但是,我又不知道这是否是真正的原因,我也可能错过了一些东西?我不知道。

最佳答案

我猜原因是 Booking 表的数据与您的模型声明不一致。 Django 的管理详细信息 View 通过主键检索模型。

您将 hotelno 标记为 PK:

hotelno = models.OneToOneField('Hotel', models.DO_NOTHING, db_column='hotelNo', primary_key=True)

由于 Booking 表中已存在一些数据,因此您必须确保 hotelno 值(hotelNo 列)是唯一的,否则您将得到 MultipleObjectsReturned code> 非唯一 pk 值的异常(exception)。另请确保您已阅读文档的这一部分 https://docs.djangoproject.com/en/1.11/ref/models/options/#managed

关于python - 使用旧数据库时 Django MultipleObjectsReturned,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47602225/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com