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=())
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 three tables,result, course and study, they are like follows
class Study(models.Model):
id_study = models.IntegerField(primary_key=True)
study_name = models.CharField(max_length=45)
description = models.CharField(max_length=45, blank=True, null=True)
language = models.CharField(max_length=45)
number_of_years = models.CharField(max_length=45)
class Course(models.Model):
id_course = models.AutoField(primary_key=True)
course_name = models.CharField(max_length=45, blank=True, null=True)
description = models.CharField(max_length=45, blank=True, null=True)
credits = models.CharField(max_length=45, blank=True, null=True)
teacher_id_teacher = models.ForeignKey('Teacher', models.DO_NOTHING, db_column='teacher_id_teacher')
study_id_study = models.ForeignKey('Study', models.DO_NOTHING, db_column='study_id_study')
class Exam(models.Model):
id_exam = models.AutoField(primary_key=True)
room = models.CharField(max_length=45, blank=True, null=True)
resit = models.CharField(max_length=45, blank=True, null=True)
date = models.DateField(blank=True, null=True)
time = models.TimeField(blank=True, null=True)
course_id_course = models.ForeignKey(Course, models.DO_NOTHING, db_column='course_id_course')
class Result(models.Model):
id_result = models.AutoField(primary_key=True)
grade = models.IntegerField(blank=True, null=True)
exam_id_exam = models.ForeignKey(Exam, models.DO_NOTHING, db_column='exam_id_exam')
date = models.DateField(blank=True, null=True)
student_id_student = models.ForeignKey('Student', models.DO_NOTHING, db_column='student_id_student')
passed = models.CharField(max_length=45, blank=True, null=True)
I want to get a result object depending on the study_id I give
Result depends on Exam, Exam depends on Course and Course depends on study
Thanks in advance
You can follow relationships in queries by using the double underscore notation
Result.objects.filter(
exam_id_exam__course_id_course__study_id_study__id=study_id
)
For what it's worth: you have an odd naming convention for your foreign keys, usually a foreign key to a model named Exam would be named exam, you are already setting db_column for each foreign key so you don't need to name them this way. If you changed their names to be the model name lowercase it would look like this which is probably more readable
Result.objects.filter(exam__course__study__id=study_id)
I am currently working on django. I created 4 classes in models.py, one of them is ReactionMeta class. This class has 62 columns, which defined as below:
class Reactionsmeta(models.Model):
id = models.ForeignKey('Reactions', db_column='id', primary_key=True, max_length=255, on_delete=models.CASCADE)
name = models.CharField(max_length=255, blank=True, null=True)
metabolite1 = models.ForeignKey('Metabolites', db_column='metabolite1', blank=True, null=True,
on_delete=models.CASCADE)
stoichiometry1 = models.CharField(max_length=255, blank=True, null=True)
metabolite2 = models.ForeignKey('Metabolites', db_column='metabolite2', blank=True, null=True,
on_delete=models.CASCADE, related_name='+')
stoichiometry2 = models.CharField(max_length=255, blank=True, null=True)
metabolite3 = models.ForeignKey('Metabolites', db_column='metabolite3', blank=True, null=True,
on_delete=models.CASCADE, related_name='+')
stoichiometry3 = models.CharField(max_length=255, blank=True, null=True)
metabolite4 = models.ForeignKey('Metabolites', db_column='metabolite4', blank=True, null=True,
on_delete=models.CASCADE, related_name='+')
#...
Some of the reactions, may have 6 metabolites, however, some of reactions may only have 2 metabolites and stoichiometries, which means there is no value of this reaction in metabolite3,4,5,6...columns and stoichiometry 3,4,5,6...columns.
In that case, how can I only display the Charfield with data while undisplay those Charfield with no value in django-admin?
So I think there is model design issue here. For my case i would have done this as follows
class ReactionMeta(models.Model):
reaction = models.ForeignKey(Reaction, ..)
name = models.CharField(..)
data = models.ForeignKey(Data, ..)
Contructing the Data class to hold the Stoichiometry and Metabolite
class Data(models.Model):
stoichiometry = models.CharField(..)
metabolite = models.ForeignKey(..)
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
I have the following models.py
class Institution(models.Model):
name = models.CharField(_('Name'), max_length=150, db_index=True)
slug = models.SlugField(_('Domain name'), unique=True)
class Company(Institution):
type = models.PositiveSmallIntegerField()
class HC(Institution):
type = models.PositiveSmallIntegerField()
bed_count = models.CharField(max_length=5, blank=True, null=True)
I have the models that are generated from Institutions and I want to follow the Institutions from the Profile model.
class Profile(models.Model):
user = models.OneToOneField(User)
about = models.TextField(_('About'), blank=True, null=True)
following_profile = models.ManyToManyField('self', blank=True, null=True)
following_institution = models.ManyToManyField(Institution, blank=True, null=True)
following_tag = models.ManyToManyField(Tag, blank=True, null=True)
I want to make M2M relations to all models that inherits Institutions. Is there a way to do this with Generic Relations?
Sounds like you're ready for polymorphism:
https://django-polymorphic.readthedocs.org
Otherwise you'll have to add them individually, so your Institution model would look something like this:
class Institution(models.Model):
name = models.CharField(_('Name'), max_length=150, db_index=True)
slug = models.SlugField(_('Domain name'), unique=True)
class Meta:
abstract = True
and the manytomany just basic like this:
following_company = models.ManyToManyField(Company, blank=True, null=True)
following_hc = models.ManyToManyField(Institution, blank=True, null=True)