How to change the appearance of unselected Foreign Key fields in Django's ModelForm

When implementing a form that inherits ModelForm, when forms.RadioSelect is applied as a widget to the ForeignKey field, the item when it is not selected is" --------- "as shown in the figure below. It is displayed and I thought it was a bit strange in terms of UI, so this is the method when I changed it.

Default figure

デフォルト

Figure after change

変更後

code

models.py

# -*-coding: utf-8-*-                                                                                                                                           
from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=30)

    def __unicode__(self):
        return self.name

class Article(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey(Author)

forms.py

# -*-coding: utf-8-*-                                                                                                                                           
from django import forms
from django.forms import ModelForm
from .models import Article, Author

class ArticleForm(ModelForm):
    class Meta:
        model = Article
        fields = '__all__'

    author = forms.ModelChoiceField(
        queryset=Author.objects.all(),
        widget=forms.RadioSelect,
        empty_label='Not applicable'
    )

Description

It is OK if you pass an arbitrary character string to forms.ModelChoiceField with empty_label.

author = forms.ModelChoiceField(
    queryset=Author.objects.all(),
    widget=forms.RadioSelect,
    empty_label='Not applicable'
)

Recommended Posts

How to change the appearance of unselected Foreign Key fields in Django's ModelForm
How to change the color of just the button pressed in Tkinter
If you specify a foreign key in Django's model change, you need to specify on_delete.
How to get the number of digits in Python
How to find the optimal number of clusters in k-means
[python] How to check if the Key exists in the dictionary
How to set the html class attribute in Django's forms.py
How to quickly count the frequency of appearance of characters from a character string in Python?
How to handle multiple versions of CUDA in the same environment
How to determine the existence of a selenium element in Python
How to change the log level of Azure SDK for Python
How to implement Java code in the background of RedHat (LinuxONE)
How to know the internal structure of an object in Python
How to check the memory size of a variable in Python
How to judge that the cross key is input in Python3
Feel free to change the label of the legend in Seaborn in python
How to check the memory size of a dictionary in Python
[TensorFlow 2] How to check the contents of Tensor in graph mode
How to get the vertex coordinates of a feature in ArcPy
I summarized how to change the boot parameters of GRUB and GRUB2
Script to change the description of fasta
How to check the version of Django
How to compare if the contents of the objects in scipy.sparse.csr_matrix are the same
How to change editor color in PyCharm
How to view images in Django's Admin
How to get the "name" of a field whose value is limited by the choice attribute in Django's model
How to pass the execution result of a shell command in a list in Python
How to mention a user group in slack notification, how to check the id of the user group
How to uniquely identify the source of access in the Django Generic Class View
Change the Key of Object on S3 from normal date format to Hive format
How to count the number of elements in Django and output to a template
How to find the coefficient of the trendline that passes through the vertices in Python
How to make the font width of jupyter notebook put in pyenv equal width
How to get a list of files in the same directory with python
How to install the deep learning framework Tensorflow 1.0 in the Anaconda environment of Windows
How to calculate the volatility of a brand
How to find the area of the Voronoi diagram
How to keep track of work in Powershell
[Python] How to change the date format (display format)
Summary of how to import files in Python 3
Change the font size of the legend in df.plot
Summary of how to use MNIST in Python
Change the decimal point of logging from, to.
Make any key the primary key in Django's model
I want to customize the appearance of zabbix
How to display the modification date of a file in C language up to nanoseconds
How to store Python function in Value of dictionary (dict) and call function according to Key
How to identify the element with the smallest number of characters in a Python list?
How to limit the API to be published in the C language shared library of Linux
How to check in Python if one of the elements of a list is in another list
How to count the number of occurrences of each element in the list in Python with weight
[Implementation explanation] How to use the Japanese version of BERT in Google Colaboratory (PyTorch)
How to change the generated image of GAN to a high quality one to your liking
How to set up public key authentication in ssh
How to specify a schema in Django's database settings
How to retrieve the nth largest value in Python
How to know the port number of the xinetd service
How to get the variable name itself in python
How to run the Ansible module added in Ansible Tower
[Touch Designer] Change the appearance color of Button COMP
How to know the current directory in Python in Blender