我在
中有以下型号from django.db import models
# Create your models here.
class Post(models.Model):
text = models.TextField()
def __str__(self):
return self.text[:50]
POWER_CHOICES= (('O', 'ON'),
('F','OFF')
)
STATE_CHOICES =(('AV','Available'),
('U','In Use '),
('NU','Do Not Use')
)
class Device(models.Model):
name = models.CharField(unique=True,max_length=50 )
ipaddress=models.ForeignKey('Ipaddress', on_delete=models.CASCADE,)
devicetype= models.ForeignKey('DeviceType', on_delete=models.CASCADE)
model= models.ForeignKey('DeviceModel', on_delete=models.CASCADE)
description= models.TextField(max_length=400)
location= models.ForeignKey('Location', on_delete=models.CASCADE)
group= models.ForeignKey('DeviceGroup', on_delete=models.CASCADE)
managment= models.CharField(max_length=100)
power= models.CharField(max_length=6, choices=POWER_CHOICES)
offtime= models.CharField(max_length=30)
state= models.CharField(max_length=10, choices=STATE_CHOICES)
user= models.CharField(max_length=100)
# department= models.ForeignKey('Department',on_delete=models.CASCADE)
comments= models.CharField(max_length=200)
def __unicode__(self):
return self.name[:50]
class DeviceType(models.Model):
name= models.CharField(max_length=100, unique=True)
def __unicode__(self):
return self.name[:50]
class DeviceModel(models.Model):
name= models.CharField(max_length=100, unique=True)
def __unicode__(self):
return self.name[:50]
class Ipaddress(models.Model):
address=models.CharField(max_length=20, unique=True)
owner=models.CharField(max_length=200)
loation=models.CharField(max_length=200)
note =models.TextField()
def __unicode__(self):
return self.address[:50]
class Department:
name= models.CharField(max_length=100, unique=True)
def __unicode__(self):
return self.name[:50]
class DeviceGroup:
name= models.CharField(max_length=100, unique=True)
def __unicode__(self):
return self.name[:50]
class Location:
description= models.CharField(max_length=100, unique=True)
def __unicode__(self):
return self.description[:50]
我的管理页面如下所示:
from django.contrib import admin
from pages.models import Post, Device, DeviceType, DeviceModel, Ipaddress, DeviceGroup, Location
admin.site.register(Post)
admin.site.register(Device)
admin.site.register(DeviceType)
admin.site.register(DeviceModel)
admin.site.register(Ipaddress)
#admin.site.register(Department)
admin.site.register(DeviceGroup)
admin.site.register(Location)
我看到以下错误,不确定是什么原因导致此错误。 有人可以给我一些想法吗?请
...
...
File "C:\Users\mohiuddin_rana\labmcproject\pages\admin.py", line 10, in <module>
admin.site.register(DeviceGroup)
File "C:\Users\mohiuddin_rana\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\admin\sites.py", line 102, in register
for model in model_or_iterable:
TypeError: 'type' object is not iterable
答案 0 :(得分:3)
对于某些模型,您忘记了从models.Model
继承它:
class Department(models.Model):
name= models.CharField(max_length=100, unique=True)
def __unicode__(self):
return self.name[:50]
class DeviceGroup(models.Model):
name= models.CharField(max_length=100, unique=True)
def __unicode__(self):
return self.name[:50]
class Location(models.Model):
description= models.CharField(max_length=100, unique=True)
def __unicode__(self):
return self.description[:50]
此更改还将导致一些迁移,因为到目前为止,Django(可能)还不了解这些类实际上是模型,因此不是构造数据库等效项。
在将模型注册到admin.site
变量时,Django需要一个模型,但是检查失败。作为一种“后备”机制,它检查您是否提供了可迭代的模型,但这又失败了,因为它只是一个“香草”类。
当我们检查source code of the register(..)
function [GitHub]时,可以确认此行为:
def register(self, model_or_iterable, admin_class=None, **options): # ... admin_class = admin_class or ModelAdmin if isinstance(model_or_iterable, ModelBase): model_or_iterable = [model_or_iterable] for model in model_or_iterable: # ... pass
(我删除了一些部分以使语义更清晰)。
如您所见,它将检查您传递的model_or_iterable
是否为模型。如果是这种情况,它将把它包装在一个列表中,否则就假定它是可迭代的,并旨在对其进行迭代。
注意:您似乎使用
__unicode__
,但同时使用Python-3.x。据我所知,__unicode__
在Python-3.x数据模型中不再具有任何意义。您应该改用__str__
。
答案 1 :(得分:2)
set.seed(1680)
library(dplyr)
library(ISLR)
library(cluster)
college_clean <- College %>%
mutate(name = row.names(.),
accept_rate = Accept/Apps,
isElite = cut(Top10perc,
breaks = c(0, 50, 100),
labels = c("Not Elite", "Elite"),
include.lowest = TRUE)) %>%
mutate(isElite = factor(isElite)) %>%
select(name, accept_rate, Outstate, Enroll,
Grad.Rate, Private, isElite)
gower_dist <- daisy(college_clean[,-1],
metric = "gower",
type = list(logratio = 3))
pam_fit <- pam(gower_dist, diss = TRUE, k = 3)
newdf=data.frame(name=c("x_university","y_university","z_university"),
accept_rate=c(.73,.50,.98),Outstate=c(10000,15000,5000),
Enroll=c(500,1000,200),Grad.Rate=c(80,65,73),
Private=c("Yes","No","No"),isElite=c("Elite","Not Elite", "Elite"))
将(models.Model)包含在您的所有班级中