I have a model:
class myLimit(models.Model):
limit = models.PositiveSmallIntegerField(help_text="The upper limit of the number of points that can be used.")
TYPES_OF_LIMITS = [('perday','Limit Per Day'),('lifetime', 'Lifetime Limit'),('peruser', 'Per User'),]
limit_type = models.CharField(choices=TYPES_OF_LIMITS, max_length=20, default='lifetime')
...
I want to know how to disable (or make it read-only) the "peruser" ("Per User") choice/option. The current myLimit
acts as a base model for an extended model which sets the default of limit_type
to "peruser" and makes the entire thing read-only my using admin model's exclude = ('limit_type',)
.
I set the default in the save()
method of the extended model just before calling the super method. The main question remains: How to make a few choices read-only? I have read tutorials on making the entire field read-only, hiding it, and others but haven't figured out a way to make "only some choices" read-only.
You can define a custom ModelForm
for your model where you can overwrite your field to change the available choices
class mylimitForm(forms.ModelForm):
class Meta:
fields = ('limit', 'limit_type', ...)
limit_type = forms.ChoiceField(choices=CHOICES_EXCLUDING_READONLY_ONES)
Then if you want to use this form in the admin you just have to set it in your ModelAdmin
class mylimitAdmin(admin.ModelAdmin):
form = mylimitForm