Integrating with legacy db in django
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,开发者_如何学PythonApplyLeaves
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.py
in app directory as well as project directory,still app is not getting registered for administration
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
.
@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
精彩评论