{"id":72,"date":"2011-08-31T20:40:23","date_gmt":"2011-08-31T20:40:23","guid":{"rendered":"http:\/\/blogs.law.harvard.edu\/rprasad\/?p=72"},"modified":"2011-09-09T18:19:37","modified_gmt":"2011-09-09T18:19:37","slug":"django-admin-resizing-form-fields-for-tabularinline","status":"publish","type":"post","link":"https:\/\/archive.blogs.harvard.edu\/rprasad\/2011\/08\/31\/django-admin-resizing-form-fields-for-tabularinline\/","title":{"rendered":"Django Admin: Resizing Form Fields (for TabularInline)"},"content":{"rendered":"<p>Recently, one &#8220;small&#8221;* project in particular required the resizing of fields for displaying <a href=\"https:\/\/docs.djangoproject.com\/en\/dev\/ref\/contrib\/admin\/#inlinemodeladmin-objects\">Inline Models in the admin<\/a>.<\/p>\n<p>For example, one inline model, &#8220;ElectroporationConditionsRecord&#8221; needed to display 11 fields.<\/p>\n<p><a href=\"http:\/\/blogs.law.harvard.edu\/rprasad\/files\/2011\/08\/tabular_inline1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-large wp-image-79\" src=\"http:\/\/blogs.law.harvard.edu\/rprasad\/files\/2011\/08\/tabular_inline1-1024x127.png\" alt=\"\" width=\"1024\" height=\"127\" srcset=\"https:\/\/archive.blogs.harvard.edu\/rprasad\/files\/2011\/08\/tabular_inline1-1024x127.png 1024w, https:\/\/archive.blogs.harvard.edu\/rprasad\/files\/2011\/08\/tabular_inline1-300x37.png 300w, https:\/\/archive.blogs.harvard.edu\/rprasad\/files\/2011\/08\/tabular_inline1.png 1256w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><br \/>\n(click image to see it full sized)<\/p>\n<p>Squeezing the 11 fields into a readable row meant updating the &#8220;size&#8221; attribute of each input box.\u00a0 A &#8220;default sized&#8221; input box in the admin does not include the &#8220;size&#8221; attribute, as in &#8216;size=&#8221;7&#8243;&#8216; below:<\/p>\n<div>\n<div>\n<div>\n<div style=\"padding-left: 30px\">&lt;input type=&#8221;text&#8221;<strong> size=&#8221;7<\/strong>&#8221; value=&#8221;0&#8243; name=&#8221;id_days_of_drug_selection&#8221; id=&#8221;id_days_of_drug_selection&#8221;&gt;<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>One way to add the &#8220;size&#8221; attribute, or any other attribute, is by making a form and connecting it to the admin.\u00a0 <strong>For this example, three files are involved in making this happen:<\/strong><\/p>\n<pre>my_appp\/models.py      # definition of model ElectroporationConditionsRecord\r\n       \/forms.py\r\n       \/admin.py<\/pre>\n<p><strong>(1) models.py<\/strong><\/p>\n<p><strong><\/strong> Here is a snippet of the ElectroporationConditionsRecord model, from the <strong>models.py<\/strong> file:<\/p>\n<pre>class ElectroporationConditions(models.Model):\r\n\r\n    example_target_model = models.ForeignKey(ExampleTargetModel)  # example FK\r\n    construct_name = models.CharField(max_length=40)\r\n    drug_selection = models.CharField(max_length=40)\r\n    days_of_drug_selection= models.IntegerField(default=0)\r\n(etc..)<\/pre>\n<p>(2)<strong> forms.py<\/strong><\/p>\n<p>The inline model for the ElectroporationConditionsRecord in the screenshot above uses the following form, located in the <strong>forms.py<\/strong> file:<\/p>\n<pre>from django import forms\r\n\r\nclass ElectroporationConditionsForm(forms.ModelForm):\r\n    '''ElectroporationConditionsForm.  Used to size the text input boxes'''\r\n\r\n    class Meta:\r\n        widgets = { '<strong>construct<\/strong>_<strong>name<\/strong>': forms.<strong>TextInput<\/strong>(attrs={'size': 20})\r\n                    , '<strong>drug_selection<\/strong>': forms.<strong>TextInput<\/strong>(attrs={'size': 20})\r\n                    , 'days_of_drug_selection': forms.TextInput(attrs={'size': 7})\r\n                    , 'drug_concentration': forms.TextInput(attrs={'size': 7}) \r\n\r\n                    , 'dna_quantity': forms.TextInput(attrs={'size': 7})\r\n                    , 'dna_concentration': forms.TextInput(attrs={'size': 7})\r\n                    , 'linearized_by': forms.TextInput(attrs={'size': 7}) \r\n\r\n                    , 'passage': forms.TextInput(attrs={'size': 7})\r\n                    , 'peak_voltage': forms.TextInput(attrs={'size': 7})\r\n                    , 'time_constant': forms.TextInput(attrs={'size': 7})\r\n                 }\r\n# examples of other form widgets: PasswordInput, HiddenInput, Select, DateInput, etc.<\/pre>\n<p>Note, each key in the widgets dictionary above (e.g. &#8216;<strong>construct_name<\/strong>&#8216;, <strong>drug_selection<\/strong>&#8216;, etc.) is the name of a field in the model ElectroporationConditionsRecord.<\/p>\n<p>You could also substitute the &#8220;size&#8221; attribute for a css class, as in:<\/p>\n<pre>              , 'time_constant': forms.TextInput(attrs={'<strong>class<\/strong>': '<strong>input_sm_number<\/strong>'})<\/pre>\n<p>where the css might be something like:<\/p>\n<pre>             input.<strong>input_sm_number<\/strong> { width:6opx; }<\/pre>\n<p><strong>(3) admin.py<\/strong><\/p>\n<p>To connect the new ElectroporationConditionsForm to the model ElectroporationConditionsRecord, a change is made in the <strong>admin.py:<\/strong><\/p>\n<pre># For this TabularInline Admin model, use the form that sets the size attributes\r\n#\r\nclass <strong>ElectroporationConditionsAdminInline<\/strong>(admin.TabularInline):\r\n    model = ElectroporationConditionsRecord   # from models.py\r\n    <strong>form = ElectroporationConditionsForm<\/strong>      # from forms.py, sets the size attributes for the input boxes\r\n    extra=0\r\n\r\n# example of using the TabularInline Admin model defined above\r\n#\r\nclass ExampleTargetModel(admin.ModelAdmin):\r\n    # note: the ElectroporationConditionsRecord has an FK to this model, ExampleTargetModel\r\n    inlines = (  <strong>ElectroporationConditionsAdminInline<\/strong>,)<\/pre>\n<p>Basically, that&#8217;s it.<\/p>\n<p>In summary, to define the field width\u00a0 in the admin model:<\/p>\n<p style=\"padding-left: 30px\">(a) Make a form similar to the <strong>ElectroporationConditionsForm<\/strong>, as in step (2) above.<\/p>\n<p style=\"padding-left: 30px\">(b) In your<strong> admin.py<\/strong> file, connect the form (<strong>ElectroporationConditionsForm<\/strong>) to your model, similar to the <strong>ElectroporationConditionsAdminInline<\/strong> model shown in step (3) above.<\/p>\n<p>There are also other ways to do this, as seen on <a href=\"http:\/\/stackoverflow.com\/questions\/910169\/resize-fields-in-django-admin\">StackOverflow.com<\/a>.<\/p>\n<p>* The project is still small\/low maintenance meaning it&#8217;s just models and the Django admin. It has 39 database tables, not including the &#8220;built-in&#8221; django tables.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, one &#8220;small&#8221;* project in particular required the resizing of fields for displaying Inline Models in the admin. For example, one inline model, &#8220;ElectroporationConditionsRecord&#8221; needed to display 11 fields. (click image to see it full sized) Squeezing the 11 fields into a readable row meant updating the &#8220;size&#8221; attribute of each input box.\u00a0 A &#8220;default &hellip; <a href=\"https:\/\/archive.blogs.harvard.edu\/rprasad\/2011\/08\/31\/django-admin-resizing-form-fields-for-tabularinline\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Django Admin: Resizing Form Fields (for TabularInline)<\/span><\/a><\/p>\n","protected":false},"author":3875,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1257,50627],"tags":[],"class_list":["post-72","post","type-post","status-publish","format-standard","hentry","category-admin","category-django"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4JC3p-1a","_links":{"self":[{"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/posts\/72","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/users\/3875"}],"replies":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/comments?post=72"}],"version-history":[{"count":34,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/posts\/72\/revisions"}],"predecessor-version":[{"id":94,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/posts\/72\/revisions\/94"}],"wp:attachment":[{"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/media?parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/categories?post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/tags?post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}