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

63 views
Skip to first unread message

shaw...@gmail.com

unread,
Mar 23, 2018, 9:56:30 PM3/23/18
to Django users

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?

Cictani

unread,
Mar 24, 2018, 6:24:48 AM3/24/18
to Django users

Hi,

I think you have a misconception in your model. What you need is a many to many relation so it does not matter how many metabolites each Reaction has. I am not so fimilar with Chemistry. So each stoichiometry belongs to exactly one metabolite?

if yes you could create an additional Model call it for example StoichiometryMetabolite which has three fields:

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 = 6

Than you have to add it to your inlines in your Reactionsmeta admin

 inlines = [StoichiometryMetaboliteInline

Everytime you have numbered columns in your model you should consider refactor it since it is not good database design.





Cictani

unread,
Mar 24, 2018, 6:27:37 AM3/24/18
to Django users

Sorry it is not a many to many relation anymore but a one to many (from the new models perspective)

Cictani

unread,
Mar 24, 2018, 6:35:07 AM3/24/18
to Django users

Grammarly messed up my post quite badly...

You should set the extra attribute to 0 (so only if there is data admin will display an entry. If you set it to 6 it will display 6 empty entries but that's what you wanted to prevent)

In your admin.py you have to create an InlineAdmin for StoichiometryMetabolite:

class StoichiometryMetaboliteInline(admin.StackedInline):
model = StoichiometryMetabolite
extra = 0

also the ending ] was missing

 inlines = [StoichiometryMetaboliteInline]





shaw...@gmail.com

unread,
Mar 24, 2018, 7:02:40 AM3/24/18
to Django users
Dear Cictani:

Thank you for your great kindness!

However, if I create the additional model which maybe called stoichiometry metabolite, then I should I arrange the database t accommodate this class?

To make my question better understood, I posted my database of Reactionmeta as below:



在 2018年3月24日星期六 UTC+1上午11:35:07,Cictani写道:

Cictani

unread,
Mar 24, 2018, 8:16:21 AM3/24/18
to Django users
After adding the new model and doing the migration you have to write a small script which will convert the data. You have to go through all the Reactionsmeta objects with

for reaction in Reactionsmeta.objects.all():

Then within the for loop you have to create another for loop which will go through all metabolite columns from 1 to 6:

for i in range(1, 7):

Then you would have to check if the column is not None:

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:

If current_metabolite is not none you can add a new StoichiometryMetabolite object:

StoichiometryMetabolite.objects.create(metabolite=current_metabolite, stoichiometry=current_stoichiomentry, reaction=reaction)


That' basically it. You should probably also write a test which makes sure that all data has successfully converted. If everything worked fine and all views, templates etc have been updated you can remove the old fields from the model.





shaw...@gmail.com

unread,
Mar 25, 2018, 6:55:30 PM3/25/18
to Django users
Dear Cictani:

Thank you for your suggestion. But how I write the script? I mean do I need to write another py file in my project? And still, how do I deal with the database of stoichiometrymeta class? Since there is no table corresponding to this class in my database, so I think I can't run the project successfully. 
Thank you for your patience to help me out and I am sorry to trouble you again. I am really new to Django so I have to admit that I have a lot to learn and sometimes I may ask some stupid questions....if you can suggest some tutorials or materials to learn that would be also great!
Thank you again for your help!!!! I am really appreciate that.

在 2018年3月24日星期六 UTC+1下午1:16:21,Cictani写道:

Cictani

unread,
Mar 26, 2018, 12:29:05 AM3/26/18
to Django users

Hi,

yes you can create a new python file in your app folder in order to run it you can enter django shell:

python manage.py shell

Let's say your app is called 'chem' and your file is called 'helpers.py' in there you have defined a function called: 'convert_reactions_meta'.

Then you can run ist by

from chem.helpers import convert_reactions_meta
convert_reactions_meta
()

Django will automatically create all missing tables if you run

python manage.py makemigrations
python manage
.py migrate

You first have to do the database migration than write the script which converts the data.


shaw...@gmail.com

unread,
Mar 27, 2018, 9:32:41 AM3/27/18
to Django users
Dear Cictani:

Thank you for your detailed information.

Just to be clarified, I need to mention that in one reaction, one stoichiometry belongs to only one metabolites. But one metabolite might involves in multiple reactions.
For example: a + 2b +c -> c + 3d(the number before each letter is called stoichiometry)
and also, a + b -> d + e + f
also, a + c + d -> h + j + k

so, as you see, a participates in three reactions, and c not only functions as reactant(which is, on the left side of equation), but also functions as production(right side)

So, after I clarified this situation, is your method, which to create another class, still working?

Sorry to bother you again and thank you for your patience!

Best regards,
Shawn

在 2018年3月26日星期一 UTC+2上午6:29:05,Cictani写道:
Reply all
Reply to author
Forward
0 new messages