My group and I are attempting to rewrite our website. We are keeping an Oracle database back-end, and rewriting the front-end with Python Django.
No problem connecting to Oracle, or accessing the datatables. Now I'm trying to add an Oracle view to the models.py, and I can't get it to work.
I googled this question with many results, so I know it is possible and apparently common, but I can't get it to work.
All I've done so far is add the class to models.py. We are using PhCharm as our IDE.
When I try to access the data using the Python Console, I can import the class, but when I try to load the QuerySet I get the error "Unable to get repr for " in the result window.
Does anyone see something I'm missing from my model?
Here is the model:
class LawBillRoleActionInfoView(LawBaseClass):
combo_id = models.AutoField(primary_key=True)
rltp_role_cd = models.CharField(max_length=2, blank=True)
bill_dft_no = models.CharField(max_length=6, blank=True)
session_id = models.IntegerField(blank=True, null=True)
ebrl_appl_seq = models.IntegerField(blank=True, null=True)
enty_id_seq = models.IntegerField(blank=True, null=True)
lst_nm = models.CharField(max_length=100, blank=True)
fst_nm = models.CharField(max_length=40, blank=True)
mi = models.CharField(max_length=1, blank=True)
entity = models.CharField(max_length=145, blank=True, null=True)
prtydist = models.CharField(max_length=11, blank=True, null=True)
blac_appl_seq = models.IntegerField(blank=None, null=True)
role_descr = models.CharField(max_length=80, blank=True)
shr_ttl = models.CharField(max_length=80, blank=True)
lmt_ind = models.CharField(max_length=1, blank=True)
bltp_bill_typ_cd = models.CharField(max_length=2, blank=True)
bill_no = models.IntegerField(blank=True, null=True)
actn_descr = models.CharField(max_length=80, blank=True)
actn_dt = models.DateField(blank=True, null=True)
sess_law_chpt_no = models.IntegerField(blank=True, null=True)
class Meta:
managed = False
db_table = 'law_bill_role_action_info_vw'
unique_together = (('bill_dft_no', 'rltp_role_cd', 'session_id'),)
app_label = 'laws'
Here are the commands I am running from the Python Console:
from laws.models import LawBillRoleActionInfoView
bill_qs = LawBillRoleActionInfoView.objects.all()
Here is the LawBaseClass:
Base Class
class LawBaseClass(models.Model):
"""
Base class
"""
created_by = models.CharField(max_length=30, blank=True, null=True)
dtm_created = models.DateField(blank=True, null=True)
mod_by = models.CharField(max_length=30, blank=True, null=True)
dtm_mod = models.DateField(blank=True, null=True)
class Meta:
managed = False
abstract = True
Related
I am getting an error in admin console while trying to open a model . How to fix it
Model.py
class TbSysEmailconfig(models.Model):
id = models.ForeignKey('self', models.DO_NOTHING, db_column='Id', primary_key=True) # Field name made lowercase.
emailtemplateid = models.ForeignKey(TbMasEmailtemplate, models.DO_NOTHING, db_column='EmailTemplateId')
emailfromname = models.CharField(db_column='EmailFromName', max_length=100, blank=True, null=True)
emailfrom = models.CharField(db_column='EmailFrom', max_length=100)
credential = models.CharField(db_column='Credential', max_length=100)
password = models.CharField(db_column='Password', max_length=100)
post = models.IntegerField(db_column='Post', blank=True, null=True)
host = models.CharField(db_column='Host', max_length=100)
priority = models.CharField(db_column='Priority', max_length=100, blank=True, null=True)
maximumday = models.IntegerField(db_column='MaximumDay', blank=True, null=True)
maximumminute = models.IntegerField(db_column='MaximumMinute', blank=True, null=True)
systemid = models.IntegerField(db_column='SystemId')
partfiletemplate = models.CharField(db_column='PartFileTemplate', max_length=500, blank=True, null=True)
comment = models.CharField(db_column='Comment', max_length=1000, blank=True, null=True)
ismaildefault = models.SmallIntegerField(db_column='IsMailDefault')
maildefault = models.CharField(db_column='MailDefault', max_length=4000, blank=True, null=True)
createddate = models.DateTimeField(db_column='CreatedDate')
createdbyuserid = models.IntegerField(db_column='CreatedByUserId')
updateddate = models.DateTimeField(db_column='UpdatedDate')
updatedbyuserid = models.IntegerField(db_column='UpdatedByUserId')
delflag = models.SmallIntegerField(db_column='DelFlag')
class Meta:
db_table = 'TB_SYS_EmailConfig'
admin.py
from django.contrib import admin
from .model import TbSysEmailconfig
#Modifire
admin.site.register(TbSysEmailconfig)
When I select some item
it show this error
Remove the id field from your model, as you have referenced it as a foreign key to the model itself. The id field is automatically generated if no field in the model is explicitly chosen.
I have the following 2 models that I want to use in a form set. I'm not sure what I've got wrong
models.py
class AppTradingPartnerTrp(models.Model):
id_trp = models.AutoField(primary_key=True)
tpid_trp = models.CharField(max_length=50, blank=True, null=True)
name_trp = models.CharField(max_length=50)
description_trp = models.CharField(max_length=100, blank=True, null=True)
idtrn_trp = models.ForeignKey('AppTransmissionTrn', models.DO_NOTHING, db_column='idtrn_trp', blank=True, null=True)
class AppCustomerTpRel(models.Model):
id_rel = models.AutoField(primary_key=True)
idcst_rel = models.ForeignKey(AppCustomerCst, models.DO_NOTHING, db_column='idcst_rel')
idtrp_rel = models.ForeignKey(AppTradingPartnerTrp, models.DO_NOTHING, db_column='id_trp')
cust_vendor_rel = models.CharField(max_length=50, blank=True, null=True)
sender_id_rel = models.CharField(max_length=50, blank=True, null=True)
old_vendor_rel = models.CharField(max_length=50, blank=True, null=True)
vendor_name_rel = models.CharField(max_length=50, blank=True, null=True)
category_rel = models.CharField(max_length=50, blank=True, null=True)
And here is where I'm trying to create the formset:
forms.py
CstVendorNoFormSet = inlineformset_factory(AppCustomerTpRel, AppTradingPartnerTrp, exclude=())
However when I do runserver I'm getting:
ValueError: 'AppTradingPartnerTrp' has no ForeignKey to 'AppCustomerTpRel'.
It looks like you've got your ForeignKey relationships the wrong way round.
Side note: I noticed that in your 'AppTradingPartnerTrp' model, for the idtrn_trp FK field, you have got 'AppTransmissionTrn' as a string, I think it should be AppTransmissionTrn without the apostrophes.
You have reversed the model order
inlineformset_factory(parent_model, model, ... )
Parent model is AppTradingPartnerTrp as it has multiple AppCustomerTpRel (foreign key) so
CstVendorNoFormSet = inlineformset_factory(AppTradingPartnerTrp, AppCustomerTpRel, exclude=())
I'm only a newbie in Python and Django.
I'm working with a MySQL database, I have created the model with the command: python manage.py inspectdb > /app/models/model.py. And it works wonderfully.
model.py
from __future__ import unicode_literals
from django.db import models
class Account(models.Model):
id = models.BigAutoField(primary_key=True)
login = models.CharField(unique=True, max_length=30)
lang = models.CharField(max_length=4)
forum_name = models.CharField(max_length=80)
hwid = models.CharField(max_length=255, blank=True, null=True)
use_allowed_hwids = models.IntegerField(blank=True, null=True)
password = models.CharField(max_length=45)
social_id = models.CharField(max_length=13)
email = models.CharField(max_length=64)
create_time = models.DateTimeField(blank=True, null=True)
status = models.CharField(max_length=8)
reason = models.CharField(max_length=255, blank=True, null=True)
securitycode = models.CharField(max_length=192, blank=True, null=True)
newsletter = models.IntegerField(blank=True, null=True)
empire = models.IntegerField()
name_checked = models.IntegerField()
availdt = models.DateTimeField(db_column='availDt', blank=True, null=True) # Field name made lowercase.
mileage = models.IntegerField()
cash = models.IntegerField()
gold_expire = models.DateTimeField()
silver_expire = models.DateTimeField()
safebox_expire = models.DateTimeField()
autoloot_expire = models.DateTimeField()
fish_mind_expire = models.DateTimeField()
marriage_fast_expire = models.DateTimeField()
money_drop_rate_expire = models.DateTimeField()
shop_double_up_expire = models.DateTimeField()
total_cash = models.IntegerField()
total_mileage = models.IntegerField()
ip = models.CharField(max_length=255, blank=True, null=True)
last_pw_change = models.DateTimeField(blank=True, null=True)
web_admin = models.IntegerField()
web_ip = models.CharField(max_length=15)
web_aktiviert = models.CharField(max_length=32)
real_name = models.CharField(max_length=32, blank=True, null=True)
question1 = models.CharField(max_length=64, blank=True, null=True)
answer1 = models.CharField(max_length=64, blank=True, null=True)
last_vote = models.DateTimeField(blank=True, null=True)
lostpw_token = models.CharField(max_length=64, blank=True, null=True)
last_play = models.DateTimeField(blank=True, null=True)
remember_token = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'account'
All works fine, but I can't see any "objects" attribute in my intellisense, and if i write it i can see only "Accounts.objects"... And no more
screenshot
I'm using Python 36
These are the relevant classes of my app. I want basically understand if the a certain user (form AuthUser) is linked to a business (from BusinessInformation) by looking at UserBusinessInformation. Thanks
class AuthUser(models.Model):
password = models.CharField(max_length=128)
last_login = models.DateTimeField(blank=True, null=True)
is_superuser = models.IntegerField()
username = models.CharField(unique=True, max_length=150)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.CharField(max_length=254)
is_staff = models.IntegerField()
is_active = models.IntegerField()
date_joined = models.DateTimeField()
class Meta:
managed = False
db_table = 'auth_user'
class BusinessInformation(models.Model):
business_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
lat = models.CharField(max_length=255)
lng = models.CharField(max_length=255)
formatted_address = models.CharField(max_length=255, blank=True, null=True)
locality = models.CharField(max_length=255, blank=True, null=True)
country = models.CharField(max_length=255, blank=True, null=True)
administrative_area_level_5 = models.CharField(max_length=255, blank=True, null=True)
administrative_area_level_4 = models.CharField(max_length=255, blank=True, null=True)
administrative_area_level_3 = models.CharField(max_length=255, blank=True, null=True)
administrative_area_level_2 = models.CharField(max_length=255, blank=True, null=True)
administrative_area_level_1 = models.CharField(max_length=255, blank=True, null=True)
postal_code = models.CharField(max_length=45, blank=True, null=True)
route = models.CharField(max_length=255, blank=True, null=True)
street_number = models.CharField(max_length=45, blank=True, null=True)
phone = models.CharField(max_length=255, blank=True, null=True)
phone2 = models.CharField(max_length=255, blank=True, null=True)
phone3 = models.CharField(max_length=255, blank=True, null=True)
email = models.CharField(max_length=255, blank=True, null=True)
email2 = models.CharField(max_length=255, blank=True, null=True)
email3 = models.CharField(max_length=255, blank=True, null=True)
website = models.CharField(max_length=255, blank=True, null=True)
facebook = models.CharField(max_length=255, blank=True, null=True)
class Meta:
managed = False
db_table = 'business_information'
class UserBusinessInformation(models.Model):
user = models.ForeignKey(AuthUser, models.DO_NOTHING)
business = models.ForeignKey(BusinessInformation, models.DO_NOTHING)
class Meta:
managed = False
db_table = 'user_business_information'
When I try to access to UserBusinessInformation in my views, I do not manage neither using _set.
def school(request, schoolname):
school_searched = BusinessInformation.objects.get(name=schoolname)
user_linked = school_searched.userbusinessinformation_set.all()
I miss the many to many field:
class BusinessInformation(models.Model):
business_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
users = models.ManyToManyField(AuthUser,
through='UserBusinessInformation')
...
Then, in your view:
def school(request, schoolname):
school_searched = BusinessInformation.objects.get(name=schoolname)
user_linked = school_searched.users.all()
Quoting Extra fields on many-to-many relationships django docs:
For these situations, Django allows you to specify the model that will be used to govern the many-to-many relationship. You can then put extra fields on the intermediate model. The intermediate model is associated with the ManyToManyField using the through argument to point to the model that will act as an intermediary.
Let me finish with a little advice, it is true, 'These are the relevant classes of my app', but you can illustrate this sample with just few fields. Learn about How to create a Minimal, Complete, and Verifiable example
[if you want access a field that’s a ForeignKey, you’ll get the related model object just like]
from django.db import models
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()`enter code here`
def __str__(self):
return self.name
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
def __str__(self):
return '%s %s' % (self.first_name, self.last_name)
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
def __str__(self):
return self.title
[you can access like that.... ]
b = Book.objects.get(id=50)
b.publisher
b.publisher.website
I am sure this question has been asked before but I have not found anything regarding admin console.
The database relationship is as follows: Host can have multiple students but a student has only one host (one-to-many or many-to-one depends on how we look at the problem). Below is my model setup:
from django.db import models
# Create your models here.
class Student(models.Model):
host = models.ForeignKey("Host", blank=True, null=True)
fullname = models.CharField(max_length=255, blank=True, null=True)
major = models.CharField(max_length=255, blank=True, null=True)
email = models.CharField(max_length=255, blank=True, null=True)
phone = models.CharField(max_length=255, blank=True, null=True)
country = models.CharField(max_length=255, blank=True, null=True)
interests = models.CharField(max_length=255, blank=True, null=True)
date = models.CharField(max_length=255, blank=True, null=True)
attendance = models.IntegerField(blank=True, null=True)
university = models.CharField(max_length=255, blank=True, null=True)
class Meta:
managed = True
db_table = 'student'
class Host(models.Model):
host_id = models.AutoField(primary_key=True)
fullname = models.CharField(max_length=255, blank=True, null=True)
email = models.CharField(max_length=255, blank=True, null=True)
phone = models.CharField(max_length=255, blank=True, null=True)
address = models.CharField(max_length=255, blank=True, null=True)
max_guests = models.IntegerField(default=5, blank=True, null=True)
preference = models.CharField(max_length=255, blank=True, null=True)
class Meta:
managed = True
db_table = 'person'
I can access host of a student by using .host (e.g. Student.objects.first().host) and also from a dropdown in django admin console. I can also access a list of students of a particular host like this:
list_of_students_of_first_host = Student.objects.filter(host_id=Host.objects.first().host_id)
But I cannot see from admin console list of students of a particular host. How to accomplish this if it is even possible. Any help would be appreciated.
a django admin inline will let you display all the Students associated with a Host in the Host's admin page
https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#django.contrib.admin.TabularInline