我有以下从auth扩展的models.py -
from django.db import models
# Create your models here.
from django.contrib.auth.models import User
class FileIndex(models.Model):
filename = models.CharField(max_length=256)
filetype = models.CharField(max_length=16)
vendorid = models.IntegerField()
vendorname = models.CharField(max_length=256)
tablename = models.CharField(max_length=256)
class Meta:
db_table = 'file_index'
verbose_name = 'File/Vendor Index'
verbose_name_plural = 'File/Vendor Indicies'
def __str__(self):
return self.filename
class UserFile(models.Model):
userid_id = models.ManyToManyField(User)
fileid_id = models.ManyToManyField(FileIndex)
grant_date = models.DateTimeField()
revoke_date = models.DateTimeField(blank=True)
class Meta:
db_table = 'auth_files'
verbose_name = 'User File Matrix'
verbose_name_plural = 'User File Matricies'
然后我的admin.py -
抱怨from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from django.db.models import Q
from api.models import *
# Register your models here.
class FileIndexAdmin(admin.ModelAdmin):
# ...
list_display = ('vendorname', 'filename')
list_filter = ['vendorname', 'filename']
search_fields = ['filename']
class UserFileAdmin(admin.ModelAdmin):
# ...
list_display = ('userid', 'fileid', 'grant_date', 'revoke_date')
list_filter = ['userid']
admin.site.register(UserFile, UserFileAdmin)
admin.site.register(FileIndex, FileIndexAdmin)
然后崩溃并显示以下消息 -
django.core.exceptions.ImproperlyConfigured: UserFileAdmin.list_display[0], 'userid_id' is not a callable or an attribute of 'UserFileAdmin' or found in the model 'UserFile'.
如果我删除' userid'和' fileid'从list_display开始,它会加载,但在尝试选择要添加/删除的用户/文件时会中断。
这些模型的SQL如下 -
CREATE TABLE auth_files
(
id serial NOT NULL,
userid_id integer NOT NULL,
fileid_id integer NOT NULL,
grant_date timestamp with time zone NOT NULL,
revoke_date timestamp with time zone NOT NULL,
CONSTRAINT auth_files_pkey PRIMARY KEY (id),
CONSTRAINT auth_files_fileid_id_fkey FOREIGN KEY (fileid_id)
REFERENCES file_index (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT auth_files_userid_id_fkey FOREIGN KEY (userid_id)
REFERENCES auth_user (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED
)
WITH (
OIDS=FALSE
);
ALTER TABLE auth_files
OWNER TO rdsadmin;
CREATE TABLE file_index
(
id serial NOT NULL,
filename character varying(256) NOT NULL,
filetype character varying(16) NOT NULL,
vendorid integer NOT NULL,
vendorname character varying(256) NOT NULL,
tablename character varying(256) NOT NULL,
CONSTRAINT file_index_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE file_index
OWNER TO rdsadmin;
基本上,auth_files / UserFile将Auth / User表与文件信息相关联,因此两个方向都很多。
我在SQL或模型中做错了吗?
答案 0 :(得分:1)
基本上userid_id有一个很多用户的域,因此您将创建另一个名为api_UserFile_userid_id的表,当您在管理列表视图中显示时,您将必须传递用户模型的整个查询集以选择用户ID,因此您可以相应地查询在PK或用户名等取决于约束
因此将UserFile模型类定义为:
class UserFile(models.Model):
userid_id = models.ManyToManyField(User)
fileid_id = models.ManyToManyField(FileIndex)
grant_date = models.DateTimeField()
revoke_date = models.DateTimeField(blank=True)
class Meta:
db_table = 'auth_files'
verbose_name = 'User File Matrix'
verbose_name_plural = 'User File Matricies'
def get_userids(self):
return "\n".join([u.pk for u in self.user.all()])
def get_fileids(self):
return "\n".join([f.pk for f in self.fileindex.all()])
在admin.py中更改:
class UserFileAdmin(admin.ModelAdmin):
# ...
list_display = ('userid', 'fileid', 'grant_date', 'revoke_date')
list_filter = ['userid']
要
class UserFileAdmin(admin.ModelAdmin):
# ...
list_display = ('get_userids', 'get_fileids', 'grant_date', 'revoke_date')
list_filter = ['userid_id']