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, like 2a + b + c -> 3d + e + f
however, some of reactions may only have 2 metabolites and stoichiometries, like a -> 2c
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?
stoichiometry = models.CharField(max_length=255, blank=True, null=True)
metabolite = models.ForeignKey('Metabolites', on_delete=models.CASCADE)
reaction = models.ForeignKey('Reactionsmeta', on_delete=models.CASCADE
In your admin.py you have to create an InlineAdmin for StoichiometryMetabolite:
class StoichiometryMetaboliteInline(admin.StackedInline):model = StoichiometryMetabolite
extra = 0
also the ending ] was missinginlines = [StoichiometryMetaboliteInline]
for reaction in Reactionsmeta.objects.all():
for i in range(1, 7):
current_metabolite = getattr(reaction, 'metabolite' + i, None)
current_stoichiometry = getattr(reaction, 'stoichimetry' + i, None)
if current_metabolite is not None and current_stoichiomentry is not None:
StoichiometryMetabolite.objects.create(metabolite=current_metabolite, stoichiometry=current_stoichiomentry, reaction=reaction)
python manage.py shell
from chem.helpers import convert_reactions_meta
convert_reactions_meta()
python manage.py makemigrations
python manage.py migrate