Add custom html between two model fields in Django admins change_form

2024/9/23 16:28:18

Let's say I've two models:

class Book(models.Model):name = models.CharField(max_length=50)library = models.ForeignKeyField('Library')class Library(models.Model):name = models.CharField(max_length=50)   address = models.CharField(max_length=50)  tel = models.CharField(max_length=50)  

Is there a nice way to add some html(a readonly input field) between name and address in the Library change_form template?. I'm doing it overriding admin/includes/fieldset.html but it's getting messy and I can't find a way to display the html exactly where I want to. For example, if I want to add html displaying the amount of books that the library has below the name field I woul do this:

{% for field in line %}...{% if field.field.name == 'name' %}{{ field.field }}<div class="form-row total_books"><div><label for="total_books">Total books:</label><input type="text" maxlength="10" name="totbooks" id="totbooks" readonly="readonly"></div></div>{% else %}{{ field.field }}{% endif %}...
{% endfor %}

New solution:

I've found a nicer way I think, but I don't know why am I getting this error: "PresupuestoAdmin.readonly_fields1, 'name' is not a callable or an attribute of PresupuestoAdmin' or found in the model 'Presupuesto' ". It seems that the 'name' field is not added to the form used by the admin.

class FoooAdminForm(forms.ModelForm): name = models.CharField(max_length=100) class Meta: model = Foo class FooAdmin(admin.ModelAdmin): form = FooAdminForm fieldsets = ( (None, { 'fields': ('id', 'name', 'date')}), ) readonly_fields = ('id', 'name') admin.site.register(Foo, FooAdmin) 
Answer

models.py:

class Library(models.Model):name = models.CharField(max_length=50)   address = models.CharField(max_length=50)  tel = models.CharField(max_length=50)def book_count(self):return self.book_set.count()

admin.py:

class LibraryAdmin(admin.ModelAdmin): fieldsets = ( (None, { 'fields': ('name', 'book_count', 'address', 'tel' )}), ) readonly_fields = ('book_count',) admin.site.register(Library, LibraryAdmin)
https://en.xdnf.cn/q/71804.html

Related Q&A

Plotly: How to add a horizontal scrollbar to a plotly express figure?

Im beginning to learn more about plotly and pandas and have a multivariate time series I wish to plot and interact with using plotly.express features. I also want my plot to a horizontal scrollbar so t…

How to run script in Pyspark and drop into IPython shell when done?

I want to run a spark script and drop into an IPython shell to interactively examine data. Running both:$ IPYTHON=1 pyspark --master local[2] myscript.pyand$ IPYTHON=1 spark-submit --master local[2] my…

Finding Min/Max Date with List Comprehension in Python

So I have this list:snapshots = [2014-04-05,2014-04-06,2014-04-07,2014-04-08,2014-04-09]I would like to find the earliest date using a list comprehension.Heres what I have now, earliest_date = snapshot…

plotting single 3D point on top of plot_surface in python matplotlib

I have some code to plot 3D surfaces in Python using matplotlib:import math import numpy as np import matplotlib.pyplot as plt from pylab import meshgrid,cm,imshow,contour,clabel,colorbar,axis from mpl…

python group/user management packages

I was looking for python user/group management package.(Creation of user group and adding/removing members to that group) I found flask_dashed. https://github.com/jeanphix/Flask-Dashed/ It more or less…

Resize NumPy array to smaller size without copy

When I shrink a numpy array using the resize method (i.e. the array gets smaller due to the resize), is it guaranteed that no copy is made?Example:a = np.arange(10) # array([0, 1, 2, 3, 4, …

TensorFlow FileWriter not writing to file

I am training a simple TensorFlow model. The training aspect works fine, but no logs are being written to /tmp/tensorflow_logs and Im not sure why. Could anyone provide some insight? Thank you# import…

python time.strftime %z is always zero instead of timezone offset

>>> import time >>> t=1440935442 >>> time.strftime("%Y/%m/%d-%H:%M:%S %z",time.gmtime(t)) 2015/08/30-11:50:42 +0000 >>> time.strftime("%Y/%m/%d-%H:%M:…

Python: Nested for loops or next statement

Im a rookie hobbyist and I nest for loops when I write python, like so:dict = {key1: {subkey/value1: value2} ... keyn: {subkeyn/valuen: valuen+1}}for key in dict:for subkey/value in key:do it to itIm a…

How to install cython an Anaconda 64 bits with Windows 10?

Its all in the title, does someone have a step by step method to install cython and run it on Anaconda 64 bits on Windows 10? I search for hours and there are a lot of tutorials... For things that I w…