我正在使用djano开发一个简单的Web应用程序来显示和管理数据库数据。我连接了MySQL DB,并使用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管理页面时,我将在管理页面上查看注册的表。我单击预订并查看多个记录,没有名称(出于其他原因),但是如果我单击其中一个,我会得到多个项目的错误
我已经阅读了我能找到的所有内容,而最接近的东西是我能找到的原因,为什么发生这种情况与某些模型中有复合键有关。但是,同样,我不知道这是否是实际原因,我也可能缺少什么?我不知道。
我想原因是Booking
表的数据与您的模型声明不一致。Django的管理详细信息视图通过主键检索模型。
您将hotelno
标记为PK:
hotelno = models.OneToOneField('Hotel', models.DO_NOTHING, db_column='hotelNo', primary_key=True)
由于Booking
表中已经存在一些数据,因此您必须确保hotelno
值(Hotelno列)是唯一的,否则您将获得非唯一PK值的MultipleObjectsReturned
例外。另请确保您已经阅读了文档的此部分https://docs.djangoproject.com/en/1.11/ref/models/options/#managed
看起来您的预订表没有主要键,并且在将其影响到预订表的Hotelno专栏时,HexplyDB猜到了错误。
当管理员尝试通过ID获得记录时,它会获得多个结果,因为不同的预订可以多次引用同一酒店。
我将在预订模型上做什么:
- 将Hotelno和Roomno更改为外国
- 删除Hotelno上的初选
新问题现在您拥有一个没有主键的模型。Django不允许这样做。如果您可以更改MySQL表格,则在其上添加主键列,并相应地更改预订模型。如果您无法更改表,我看不到使它起作用的简单方法。