在django中与遗留数据库集成

时间:2011-03-10 06:56:07

标签: django django-models django-admin

models.py是从旧版数据库创建的。我创建了models.py并为应用admin.py创建了interface,并在installed app创建了应用名称,我有大约76个表(76 classes in models.py)。

admin.py:

#! /usr/bin/env python  
from django.contrib import admin  
from django.db import models  
from interface.models import Students,ApplyLeaves  
site.register(ApplyLeaves)  
site.register(Students)  
for m in get_models(db):  
       site.register(m)

在上述情况下,Student和ApplyLeaves类未在管理界面中注册。

我已将admin.py放在app目录以及项目目录中,仍未将app注册为管理

2 个答案:

答案 0 :(得分:2)

如果您想将所有模型注册到django admin,您应该(尽管您不必须)创建管理实例。

这是因为,尽管django admin可以仅基于模式公开所有模型,但是它有很多花哨和小气来使模式表示有意义的真实解决方案

那就是说,在你的情况下,你不应该将这个admin.py保留在项目级别,而只是在应用程序级别。 StudentsApplyLeaves在同一个db数据库中是否已传递给get_models

您将要做的优雅解决方案如下:

project-folder/bare_tables.py

from django.db.models import get_models
from django.contrib.admin import AdminSite, ModelAdmin

class BareTables(AdminSite):
    pass

new_admin = BareTables(name='bare_tables')

for el in get_models():
    new_admin.register(el,ModelAdmin)

urls.py

 from bare_tables import new_admin

 urlpatterns = patterns('',
     (r'^bare-tables/', include(new_admin.urls)),
     (r'^admin/', include(admin.site.urls)),

这样,当/admin中显示所有表格时,您可以继续在/bare-tables中构建有意义的界面

答案 1 :(得分:0)

@gladysbixy
models.py
{ `
        来自django.db导入模型

class AdditionalExamGroups(models.Model):  
   id = models.IntegerField(primary_key=True)  
    name = models.CharField(max_length=255, blank=True)  
    batch_id = models.IntegerField(null=True, blank=True)  
    exam_type = models.CharField(max_length=255, blank=True)   
    is_published = models.IntegerField(null=True, blank=True)  
    result_published = models.IntegerField(null=True, blank=True)  
    students_list = models.CharField(max_length=255, blank=True)  
    exam_date = models.DateField(null=True, blank=True)    

    class Meta:  
        db_table = u'additional_exam_groups'  

class AdditionalExamScores(models.Model):  
    id = models.IntegerField(primary_key=True)  
    student_id = models.IntegerField(null=True, blank=True)  
    additional_exam_id = models.IntegerField(null=True, blank=True)  
    marks = models.DecimalField(null=True, max_digits=9, decimal_places=2, blank=True)  
    grading_level_id = models.IntegerField(null=True, blank=True)  
    remarks = models.CharField(max_length=255, blank=True)  
    is_failed = models.IntegerField(null=True, blank=True)  
    created_at = models.DateTimeField(null=True, blank=True)  
    updated_at = models.DateTimeField(null=True, blank=True)    
    class Meta:  
        db_table = u'additional_exam_scores'  

class AdditionalExams(models.Model):  
    id = models.IntegerField(primary_key=True)  
    additional_exam_group_id = models.IntegerField(null=True, blank=True)  
    subject_id = models.IntegerField(null=True, blank=True)  
    start_time = models.DateTimeField(null=True, blank=True)  
    end_time = models.DateTimeField(null=True, blank=True)  
    maximum_marks = models.IntegerField(null=True, blank=True)  
    minimum_marks = models.IntegerField(null=True, blank=True)  
    grading_level_id = models.IntegerField(null=True, blank=True)  
    weightage = models.IntegerField(null=True, blank=True)  
    event_id = models.IntegerField(null=True, blank=True)  
    created_at = models.DateTimeField(null=True, blank=True)  
    updated_at = models.DateTimeField(null=True, blank=True)  
    class Meta:  
        db_table = u'additional_exams'  

 class AdditionalFields(models.Model):  
    id = models.IntegerField(primary_key=True)  
    name = models.CharField(max_length=255, blank=True)  
    status = models.IntegerField(null=True, blank=True)  
    class Meta:  
       db_table = u'additional_fields'  

class ApplyLeaves(models.Model):  
    id = models.IntegerField(primary_key=True)  
    employee_id = models.IntegerField(null=True, blank=True)  
    employee_leave_types_id = models.IntegerField(null=True, blank=True)  
    is_half_day = models.IntegerField(null=True, blank=True)  
    start_date = models.DateField(null=True, blank=True)  
    end_date = models.DateField(null=True, blank=True)  
    reason = models.CharField(max_length=255, blank=True)  
    approved = models.IntegerField(null=True, blank=True)  
    viewed_by_manager = models.IntegerField(null=True, blank=True)  
    manager_remark = models.CharField(max_length=255, blank=True)  
    class Meta:  
        db_table = u'apply_leaves'  

 class ArchivedEmployeeAdditionalDetails(models.Model):  
    id = models.IntegerField(primary_key=True)  
    employee_id = models.IntegerField(null=True, blank=True)  
    additional_field_id = models.IntegerField(null=True, blank=True)  
    additional_info = models.CharField(max_length=255, blank=True)  
    class Meta:  
        db_table = u'archived_employee_additional_details'  

 class ArchivedEmployeeBankDetails(models.Model):  
    id = models.IntegerField(primary_key=True)  
    employee_id = models.IntegerField(null=True, blank=True)  
    bank_field_id = models.IntegerField(null=True, blank=True)  
    bank_info = models.CharField(max_length=255, blank=True)  
    class Meta:  
        db_table = u'archived_employee_bank_details'  




class Students(models.Model):  
    id = models.IntegerField(primary_key=True)  
    admission_no = models.CharField(max_length=255, blank=True)  
    class_roll_no = models.CharField(max_length=255, blank=True)  
    admission_date = models.DateField(null=True, blank=True)  
    first_name = models.CharField(max_length=255, blank=True)  
    middle_name = models.CharField(max_length=255, blank=True)  
    last_name = models.CharField(max_length=255, blank=True)  
    batch_id = models.IntegerField(null=True, blank=True)  
    date_of_birth = models.DateField(null=True, blank=True)  
    gender = models.CharField(max_length=255, blank=True)  
    blood_group = models.CharField(max_length=255, blank=True)  
    birth_place = models.CharField(max_length=255, blank=True)  
    nationality_id = models.IntegerField(null=True, blank=True)  
    language = models.CharField(max_length=255, blank=True)  
    religion = models.CharField(max_length=255, blank=True)  
    student_category_id = models.IntegerField(null=True, blank=True)  
    address_line1 = models.CharField(max_length=255, blank=True)  
    address_line2 = models.CharField(max_length=255, blank=True)  
    city = models.CharField(max_length=255, blank=True)  
    state = models.CharField(max_length=255, blank=True)  
    pin_code = models.CharField(max_length=255, blank=True)  
    country_id = models.IntegerField(null=True, blank=True)  
    phone1 = models.CharField(max_length=255, blank=True)  
    phone2 = models.CharField(max_length=255, blank=True)  
    email = models.CharField(max_length=255, blank=True)  
    immediate_contact_id = models.IntegerField(null=True, blank=True)  
    is_sms_enabled = models.IntegerField(null=True, blank=True)  
    photo_filename = models.CharField(max_length=255, blank=True)  
    photo_content_type = models.CharField(max_length=255, blank=True)  
    photo_data = models.TextField(blank=True)  
    status_description = models.CharField(max_length=255, blank=True)  
    is_active = models.IntegerField(null=True, blank=True)  
    is_deleted = models.IntegerField(null=True, blank=True)  
    created_at = models.DateTimeField(null=True, blank=True)   
    updated_at = models.DateTimeField(null=True, blank=True)  
    class Meta:  
        db_table = u'students'  


class Users(models.Model):  
    id = models.IntegerField(primary_key=True)  
    username = models.CharField(max_length=255, blank=True)  
    first_name = models.CharField(max_length=255, blank=True)  
    last_name = models.CharField(max_length=255, blank=True)  
    email = models.CharField(max_length=255, blank=True)  
    admin = models.IntegerField(null=True, blank=True)  
    student = models.IntegerField(null=True, blank=True)  
    employee = models.IntegerField(null=True, blank=True)  
    hashed_password = models.CharField(max_length=255, blank=True)  
    salt = models.CharField(max_length=255, blank=True)  
    reset_password_code = models.CharField(max_length=255, blank=True)  
    reset_password_code_until = models.DateTimeField(null=True, blank=True)  
    created_at = models.DateTimeField(null=True, blank=True)  
    updated_at = models.DateTimeField(null=True, blank=True)  
     class Meta:  
         db_table = u'users'  

}` 我已经跳过很多无聊的表格来缩进......

`admin.py'你可以在上一篇文章中找到