与 django 中的旧数据库集成

发布于 2024-10-21 07:43:43 字数 613 浏览 3 评论 0 原文

models.py 是从旧数据库创建的。我已经创建了 models.py 并为应用界面创建了admin.py code> 和 installed app 中的应用程序名称,我有大约 76 个表(models.py 中的 76 个类)。

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)

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

我已将 admin.py 放置在应用程序目录和项目目录中,但应用程序仍然没有注册管理

models.py was created from legacy database.i have created a models.py and created admin.py for app interface and the app name in installed app ,i have around 76 tables (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)

In above case Students and ApplyLeaves classes are not getting registerd in admin interface.

I have placed admin.pyin app directory as well as project directory,still app is not getting registered for administration

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眸中客 2024-10-28 07:43:43

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

这是因为,虽然 django admin 可以仅基于模式公开所有模型,但它有很多花哨的功能来使模式表示成为有意义的真正解决方案

也就是说,就您而言,您不应该将此admin.py保留在项目级别,而应仅保留在应用程序级别。 StudentsApplyLeaves 是否位于您传递给 get_models 的同一个 db 数据库中?

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

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中。

If you want to register all models into django admin, you should (although you dont have to) create an instance of the admin.

This is because, although django admin can expose all models based just on the schema alone, it has so many bells and whistels to make that schema representation meaningful real solution.

That said, in your case, you should not keep this admin.py in project level, but just at the application level. Is the Students and ApplyLeaves in the same db database you have passed to the get_models?

The elegant solution to what you are going to do would be as follows:

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)

In your urls.py

 from bare_tables import new_admin

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

That way you can continue to build meaningful interfaces in /admin while you have all tables exposed in /bare-tables.

温柔一刀 2024-10-28 07:43:43

@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”你可以在上一篇文章中找到

@gladysbixy
models.py
{
`
from django.db import models

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'  

}`
I have skipped lots of tables cos bored to indent...

`admin.py' you can find in previous post

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文