Integrating with legacy db in django - 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,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

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

Related

It is not possible to migrate a new user element to the django table

I am faced with the problem that it is not possible to migrate the new user value to the database table. At first there were errors on related_name, but I fixed it, and now that this value cannot be zero, at the same time, if I write that null=True, then the user cannot be displayed in the records in the future.
class Writehelp(models.Model):
users = models.ForeignKey(User,on_delete = models.CASCADE,related_name='helpedman',null=False,verbose_name='Автор')
titles = models.CharField(max_length=200, verbose_name='Заголовок', blank=True)
descriptions = models.TextField(blank=True, verbose_name='Описание')
createdtimes = models.DateField(auto_now_add=True, db_index=True, verbose_name='Дата создания')
prices = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name='Цена')
course = models.IntegerField(null=True, blank=True, verbose_name='Курс')
semestr = models.IntegerField(null=True, blank=True, verbose_name='Семестр')
subjects = models.CharField(max_length=200, null=True, blank=True, verbose_name='Предмет')
institutes = models.CharField(max_length=200, null=True, blank=True, verbose_name='Институт')
However, when I migrated this model, there were no problems:
class UploadFile(models.Model):
user = models.ForeignKey(User,on_delete = models.CASCADE,related_name='file_created' ,verbose_name='Автор')
title = models.CharField(max_length=200, verbose_name='Заголовок',blank=True)
# uploadedfile = models.FileField(upload_to='files/',null=True, verbose_name='Файл')
description = models.TextField(blank=True, verbose_name='Описание')
createdtime = models.DateField(auto_now_add=True, db_index=True, verbose_name='Дата создания')
price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name='Цена')
number_course = models.IntegerField(null=True, blank=True, verbose_name='Курс')
number_semestr = models.IntegerField(null=True, blank=True, verbose_name='Семестр')
subjectt = models.CharField(max_length=200, null=True,blank=True,verbose_name='Предмет')
type_materials = models.CharField(max_length=200,null=True,blank=True, verbose_name='Тип работы')
institute = models.CharField(max_length=200, null=True,blank=True, verbose_name='Институт')
Add defult=None to uploadedfile field in your UploadFile model.

How to get user object in template?

model.py
class User(AbstractUser):
is_agree = models.CharField(max_length=1, blank=True)
is_verify = models.CharField(max_length=1, blank=True)
type = models.CharField(max_length=1, blank=True)
name = models.CharField(max_length=255, blank=True)
address1 = models.CharField(max_length=255, blank=True)
address2 = models.CharField(max_length=255, blank=True)
bank_account = models.CharField(max_length=255, blank=True)
phone_number = models.CharField(max_length=255, blank=True)
I want to get users type in template.
I tried {{ user.type }}. but was not worked

FOREIGN KEY constraint failed in signals

I tried to insert data into the table Account, but I keep getting this error message.
I delete all my migrations and make migrations again but I keep getting this error.
Here is my models.
class Student(models.Model):
user = models.OneToOneField(Account, on_delete=models.CASCADE)
name = models.CharField(max_length=50, null=True, blank=True)
phone = models.CharField(max_length=15, blank=True, null=True)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True, null=True)
section = models.ForeignKey(Section, on_delete=models.CASCADE, default=1)
dept = models.ForeignKey(Dept, on_delete=models.CASCADE, blank=True, null=True)
batch = models.CharField(max_length=15, null=True, blank=True)
USN = models.CharField(primary_key='True', max_length=100)
DOB = models.DateField(default='1998-01-01')
profile_image = models.ImageField(upload_to='student/profile_images/%Y/%m/%d/', blank=True, null=True)
address = models.CharField(max_length=300, null=True, blank=True)
nationality = models.CharField(max_length=100, null=True, blank=True)
guardian_name = models.CharField(max_length=200, null=True, blank=True)
guardian_number = models.CharField(max_length=15, null=True, blank=True)
guardian_address = models.CharField(max_length=300, null=True, blank=True)
blood_group = models.CharField(max_length=3, null=True, blank=True)
exam_name = models.CharField(max_length=300, null=True, blank=True)
background = models.CharField(max_length=300, null=True, blank=True)
passing_year = models.CharField(max_length=8, null=True, blank=True)
score = models.CharField(max_length=10, null=True, blank=True)
school_name = models.CharField(max_length=300, null=True, blank=True)
country = models.CharField(max_length=100, null=True, blank=True)
certificate = models.FileField(upload_to='certificates/%Y/%m/%d/', null=True, blank=True)
Here is Account model.
class Account(AbstractBaseUser, PermissionsMixin):
unid = models.CharField(max_length=20, unique=True, null=True, blank=True)
email = models.EmailField(verbose_name="email", max_length=60, unique=True)
username = models.CharField(max_length=30, unique=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_student = models.BooleanField(default=False)
is_teacher = models.BooleanField(default=False)
group = models.ForeignKey(Group, on_delete=models.CASCADE, null=True, blank=True)
user_permissions = models.ManyToManyField(
Permission,
verbose_name=_('user permissions'),
blank=True,
null=True,
help_text=_('Specific permissions for this user.'),
related_name="user_set",
related_query_name="user",
)
date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
registered = models.BooleanField(default=False)
so what I did is that when I create account if the is_student is True I create a signals that going to create student model for that account instance automatically.
here is the signals
def create_student_or_teacher(sender, created, instance, **kwargs):
if created:
if instance.is_student:
Student.objects.create(
user=instance,
USN=instance.unid,
)
elif instance.is_teacher:
Teacher.objects.create(
user=instance,
id=instance.unid,
)
So when I create the account for student the tracker will highlight in the signals Student.objects.create(...)
If you have any idea I hope it would help me.
UPDATED:
When I'm creating teacher it is working find.
here is the teacher model
class Teacher(models.Model):
name = models.CharField(max_length=50, null=True, blank=True)
phone = models.CharField(max_length=15, blank=True, null=True)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True)
salary = models.IntegerField(default=1)
school = models.ForeignKey(School, blank=True, null=True, on_delete=models.CASCADE)
section = models.ForeignKey(Section, blank=True, null=True, on_delete=models.CASCADE)
user = models.OneToOneField(Account, on_delete=models.CASCADE, null=True)
id = models.CharField(primary_key=True, max_length=100)
dept = models.ForeignKey(Dept, on_delete=models.CASCADE, blank=True, null=True)
DOB = models.DateField(default='1980-01-01')
profile_image = models.ImageField(upload_to='teacher/profile_images/%Y/%m/%d/', blank=True, null=True)
nationality = models.CharField(max_length=300, null=True, blank=True)
address = models.CharField(max_length=300, null=True, blank=True)
Finally after few hours I solved my error it was simple
in the section I set default value which was not exist
so I just change the value to an exist one and that was it.

What am I missing in this Django model relationship

I have a CustomUser model, and a Blog model.
CustomUser model:
class CustomUser(AbstractUser):
email = models.EmailField(unique=True)
first_name = models.CharField(max_length=20, default="", blank=True)
last_name = models.CharField(max_length=20, default="", blank=True)
address = models.CharField(max_length=150, default="", blank=True)
city = models.CharField(max_length=20, default="", blank=True)
zip_code = models.CharField(max_length=20, default="", blank=True)
country = models.CharField( max_length=50, default="", blank=True, choices=settings.COUNTRIES_LIST)
about_me = models.TextField(max_length=225, default="", blank=True)
photo_url = models.TextField(null=True)
is_active = models.BooleanField(default=True) # can login
Blog model:
class Blog(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1, null=True, verbose_name=_('user'), related_name="%(class)s_blogs", on_delete=models.SET_NULL)
blog_id = models.CharField(max_length=150, null=False, default=get_id, unique=True, editable=False)
blog_title = models.CharField(max_length=150, null=False)
blog_description = models.TextField(max_length=300, null=True, blank=True)
created_at = models.DateTimeField(auto_now=False, auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True, auto_now_add=False)
blog = FroalaField()
dynamic_link = models.CharField(max_length=225, null=False, default="")
blog_type = models.CharField( max_length=50, null=False, choices=BLOGSTYPE_LIST)
blog_status = models.CharField(max_length=150, null=False, default="unapproved")
is_active = models.BooleanField(default=False)
is_featured = models.BooleanField(default=False)
My understanding is that I could use the related_name to get all blogs by a user.
>>> from users.models import CustomerUser
>>> user = CustomUser.objects.get(pk=1)
>>> user.blog_blogs.all()
<BlogQuerySet []>
As you must have seen, this turns to always return but an empty queryset, even though there are blog entries by that user.
So is it am not understanding here?
Thank you.

Django Nested Serializer does not find attribute

This are my models
class OWNER_STORE(models.Model):
_id = models.CharField(max_length=120, unique=True, default=uuid.uuid4)
MSE_STORE_TYPE = models.ForeignKey(STORE_TYPE)
MSE_OWNER_INFO = models.ForeignKey(OWNER_INFO, related_name='STORES')
STORE_NAME = models.CharField("STORE_NAME", max_length=250, null=True, blank=True)
STORE_ID = models.IntegerField("STORE_ID", null=True, blank=True)
STORE_ADDRESS_HOUSE = models.CharField("STORE_ADDRESS_HOUSE", max_length=250, null=True, blank=True)
STORE_ADDRESS_ROAD = models.CharField("STORE_ADDRESS_ROAD", max_length=250, null=True, blank=True)
STORE_ADDRESS_VILLAGE = models.CharField("STORE_ADDRESS_VILLAGE", max_length=250, null=True, blank=True)
STORE_ADDRESS_UNION = models.CharField("STORE_ADDRESS_UNION", max_length=250, null=True, blank=True)
STORE_ADDRESS_THANA = models.CharField("STORE_ADDRESS_THANA", max_length=250, null=True, blank=True)
STORE_ADDRESS_DISTRICT = models.CharField("STORE_ADDRESS_DISTRICT", max_length=250, null=True, blank=True)
STORE_ADDRESS_DIVISION = models.CharField("STORE_ADDRESS_DIVISION", max_length=250, null=True, blank=True)
STORE_EMPLOYEE_STATUS = models.BooleanField("EMPLOYEE_STATUS")
STORE_NUMBER_OF_EMPLOYEE = models.IntegerField("NUMBER_OF_EMPLOYEE", null=True, blank=True)
STORE_AVG_REVENUE = models.IntegerField("STORE_AVG_REVENUE", null=True, blank=True)
STORE_AVG_COGS = models.IntegerField("STORE_AVG_COGS", null=True, blank=True)
STORE_AVG_PROFIT = models.IntegerField("STORE_AVG_PROFIT", null=True, blank=True)
STORE_CUSTOMER_CREDIT_LIMIT = models.IntegerField("CUSTOMER_CREDIT_LIMIT", null=True, blank=True)
STORE_BUSINESS_TARGET = models.IntegerField("BUSINESS_TARGET", null=True, blank=True)
STORE_REORDER_THRESHOLD = models.IntegerField("REORDER_THRESHOLD", null=True, blank=True)
STORE_PASSWORD = models.TextField("STORE_PASSWORD", max_length=50, default=None, null=True, blank=True)
def __str__(self):
return str(self.id)
class MSE_EMPLOYEE(models.Model):
emp_id = models.IntegerField(null=True, blank=True)
store = models.ForeignKey(OWNER_STORE)
MSE_EMPLOYEE_ID = models.IntegerField(null=True, blank=True)
MSE_EMPLOYEE_NAME=models.CharField(max_length=250, null=True, blank=True)
MSE_EMPLOYEE_MOBILE_NUMBER = models.CharField(max_length=250, null=True, blank=True)
MSE_EMPLOYEE_JOINING_DATE = models.DateField(null=True, blank=True)
MSE_EMPLOYEE_SALARY = models.IntegerField(null=True, blank=True)
def __str__(self):
return str(self.MSE_EMPLOYEE_NAME)
For my serializers.py, I did this
class STORE_EMPLOYEE_SERIALIZER(serializers.ModelSerializer):
employees = EMPLOYEE_SERIALIZER(many=True)
class Meta:
model = OWNER_STORE
fields=("MSE_STORE_TYPE", "MSE_OWNER_INFO", "STORE_NAME", "STORE_ID", "STORE_ADDRESS_HOUSE", "STORE_ADDRESS_ROAD",
"STORE_ADDRESS_VILLAGE", "STORE_ADDRESS_UNION", "STORE_ADDRESS_THANA", "STORE_ADDRESS_DISTRICT",
"STORE_ADDRESS_DIVISION", "STORE_EMPLOYEE_STATUS", "STORE_NUMBER_OF_EMPLOYEE",
"STORE_AVG_REVENUE", "STORE_AVG_COGS", "STORE_AVG_PROFIT", "STORE_CUSTOMER_CREDIT_LIMIT",
"STORE_BUSINESS_TARGET", "STORE_REORDER_THRESHOLD", "STORE_PASSWORD", "employees")
I was hoping that with a get request, I would get all the employees under store but I keep getting this error.
AttributeError: 'OWNER_STORE' object has no attribute 'employees'
and on the last line of terminal,
AttributeError: Got AttributeError when attempting to get a value for field employees on serializer STORE_EMPLOYEE_SERIALIZER.
The serializer field might be named incorrectly and not match any attribute or key on the OWNER_STORE instance.
Original exception text was: 'OWNER_STORE' object has no attribute 'employees'.
You need use MSE_EMPLOYEE_set instead employees.
Or add related_name to the store = models.ForeignKey(OWNER_STORE)
store = models.ForeignKey(OWNER_STORE, related_name='employees')