How to undisplay the field with empty value in django admin? - python

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(..)

Related

how do i add multiple models in one-to-many field in Django Model?

I want to pass gamingpc and pcComponent on OrderItem item field so I can add one of them in item field
class OrderItem(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
ordered = models.BooleanField(default=False)
item = models.ForeignKey(
gamingpc, on_delete=models.CASCADE, blank=True, null=True)
product = models.ForeignKey(
PcComponent, on_delete=models.CASCADE, blank=True, null=True)
you must use content-type for this situation .
class OrderItem(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
content_object = GenericForeignKey()
object_id = models.CharField(max_length=100, null=True, blank=True)
content_type = models.ForeignKey(
ContentType, on_delete=models.CASCADE, null=True, blank=True,
limit_choices_to=(models.Q(model__in=['PcComponent', 'gamingpc']))

Creating an Inline Formset - Foreign Key Issue

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=())

how can I get data from a table that is liked to a table that is liked to another one it self?

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)

how to make inner join without relation in django orm?

This is my first model
class DataPribadiSiswa(models.Model):
SiswaID = models.AutoField(primary_key=True)
WaliKelasID = models.CharField(max_length=11, blank=True, null=True)
SiswaNIS = models.CharField(max_length=7, null=True, blank=True, unique=True)
and this is my second model
class Transaksi(models.Model):
tanggal = models.DateField(null=True, blank=True)
keterangan = models.CharField(max_length=100, null=True, blank=True)
nominal = models.IntegerField(null=True, blank=True)
nis = models.CharField(max_length=7, null=True, blank=True)
how to make inner join query from that model without relation from those models where DataPribadiSiswa.Siswanis same with Transaksi.nis.

Model Many to Many relations with Abstract Class

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)

Categories

Resources