Django MultipleObjects在使用旧数据库时返回

时间:2017-12-01 22:13:49

标签: python django django-models django-views legacy-database

我正在使用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

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

2 个答案:

答案 0 :(得分:1)

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

您将hotelno标记为PK:

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

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

答案 1 :(得分:0)

看起来您的预订表没有主键,并且当影响到预订表的hotelno列时,inspectdb猜错了。

当管理员尝试通过它的ID获取记录时,它会得到多个结果,因为不同的预订可以多次引用同一个酒店。

我会在预订模式上做些什么:

  • 将hotelno和roomno更改为ForeignKey
  • 删除hotelno
  • 上的主要内容

新问题现在你有一个没有主键的模型。 Django不允许这样做。如果您可以更改MySQL表,则向其添加主键列并相应地更改预订模型。如果你不能改变表格,我认为没有简单的方法可以使它有效。