Display a Foreign Key Dropdown in Django Form as Text - django

Is there a way to show a foreign key field in a Django form as an read only text box?
forms.py
class NewModelForm(forms.ModelForm):
class Meta:
model = Model
fields = ['fk_field', 'other_field']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['fk_field'].widget.attrs['readonly'] = True #the dropdown is still active when this is set
#self.fields['fk_field'] = forms.CharField(widget=forms.TextInput()) ##when I turn this on, I get an error that I am assigning to the wrong instance.

You can override any of your ModelForm's fields, even those that come from the model, by just setting it as class attribute like you would for a normal Form field:
class NewModelForm(ModelForm):
fk_field = forms.CharField(required=False, disabled=True)
class Meta:
model = MyModel
fields = ['fk_field', 'other_field']
The disabled option on a field sets the input to disabled.
Note that you should not trust the data submitted to contain the correct initial value for fk_field. Anyone can still submit a different fk_field if they know how to use curl or Postman, even if the <input> is disabled. So if just ignore whatever value is submitted and set it to the correct value in your view.

Related

Django modelform field how to make disabled and prevent tampering?

There is this disabled attribute. But i am not able to apply it to the modelform fields. I am not sure how to. I can add it to forms.Form easily. But since I am using widgets I just dont know where to insert it.
https://docs.djangoproject.com/en/2.0/ref/forms/fields/#disabled
class TestForm(forms.ModelForm):
class Meta:
model = Test
fields = ['date']
widgets = {'date': forms.TextInput(attrs={'readonly': 'readonly'})}
I was facing a situation when I wanted to disable some fields when creating . And some fields disabled when editing.
My Env: Python 3, Django 2.1
My Form:
class AddInvoiceForm(forms.ModelForm):
disabled_fields = ['inv_type', 'report', 'subsidiary']
class Meta:
model = models.Invoice
fields = ('inv_type', 'report', 'subsidiary', 'rate_card', 'reviewed')
def __init__(self, *args, **kwargs):
super(AddInvoiceForm, self).__init__(*args, **kwargs)
instance = getattr(self, 'instance', None)
if instance and instance.pk:
for field in self.disabled_fields:
self.fields[field].disabled = True
else:
self.fields['reviewed'].disabled = True
Try something like this, assuming that your date field is forms.DateField and that you want to use TextInput widget:
class TestForm(forms.ModelForm):
date = forms.DateField(widget=forms.TextInput, disabled=True)
class Meta:
model = Test
fields = ['date']
This will override the default field definition which is created from your Test model definition.
The disabled boolean argument, when set to True, disables a form field using the disabled HTML attribute so that it won’t be editable by users. Even if a user tampers with the field’s value submitted to the server, it will be ignored in favor of the value from the form’s initial data.
Read about readonly vs disabled HTML input attributes.
The key note to take out from the above SO post is:
A readonly element is just not editable, but gets sent when the according form submits. a disabled element isn't editable and isn't sent on submit.
From above quote, setting disabled=True is enough, so you dont need to set readonly attribute on your widget.
class TestForm(forms.ModelForm):
date = forms.CharField(disabled=True)
class Meta:
model = Test
fields = ['date']
widgets = {
'date': forms.TextInput(attrs={'readonly': 'readonly'}),
}

How hold data after submit admin site form

Here is the screenshot of my project with model.py.
when i click the "save and add another button" then the values of "Lev name:" "Sem name:" "SType:" changed to its default value.
what should i do?
so that the given value will not change to default values.
If you want the defaults in the form to be the values from the last entry, you need to customize your form in the ModelAdmin. Probably the easiest would be to use your own form to change the initial value. Something like:
class CourseAdmin(admin.ModelAdmin):
form = CourseForm
class CourseForm(forms.ModelForm):
class Meta:
model = Course
fields = '__all__'
def __init__(self, **kwargs):
try:
last_course = Course.objects.last()
initial = {'sem_name': last_course.sem_name, 'sType': last_course.sType}
except Course.DoesNotExist:
initial = None
super(CourseForm, self).__init__(initial=initial, **kwargs)

Django - Show BooleanField in a formset as one group of radio buttons

I have the following models:
class Profile(models.Model):
verified = models.BooleanField(default=False)
def primary_phone(self):
return self.phone_set.get(primary=True)
class Phone(models.Model):
profile = models.ForeignKey(Profile)
type = models.CharField(choices=PHONE_TYPES, max_length=16)
number = models.CharField(max_length=32)
primary = models.BooleanField(default=False)
def save(self, force_insert=False, force_update=False, using=None):
if self.primary:
# clear the primary attribute of other phones of the related profile
self.profile.phone_set.update(primary=False)
self.save(force_insert, force_update, using)
I'm using Phone in a ModelForm as a formset. What I'm trying to do is to show Phone.primary as a radio button beside each instance of Phone. If I make primary as a RadioSelect widget:
class PhoneForm(ModelForm):
primary = forms.BooleanField(widget=forms.RadioSelect( choices=((0, 'False'), (1, 'True')) ))
class Meta:
from accounts.models import Phone
model = Phone
fields = ('primary', 'type', 'number', )
It will show two radio buttons, and they will be grouped together next to each instance. Instead, I'm looking for a way to show only one radio button next to each instance (which should set primary=True for that instance), and have all the set of radio buttons grouped together so that only one of them can be chosen.
I'm also looking for a clean way of doing this, I can do most of the above manually - in my head - but I'm interested to see if there is a better way to do it, a django-style way.
Anyone got an idea?
Ok you've got two dilemma's here. First is you need to group all radio selects from different formsets by giving them the same HTML name attribute. I did that with the add_prefix override below.
Then you have to make sure that the 'primary' field in your post data returns something meaningful, from which you can determine which phone was selected (there should only be one 'name' value in POST data b/c you can only select one radio button from within a group). By assigning the proper prefix value (this needs to be done under _init_ so you can access the self instance), you'll be able to associate the 'primary' value with the rest of its form data (through a custom save method).
I tested a formset with the following and it spat out the right html. So give this a try:
class PhoneForm(ModelForm):
def __init__ (self, *args, **kwargs)
super(PerstransForm, self).__init__(*args, **kwargs)
self.fields['primary'] = forms.BooleanField( widget = forms.RadioSelect(choices=((self.prefix, 'This is my Primary Phone'),))
#enter your fields except primary as you had before.
def add_prefix(self, field):
if field == 'primary': return field
else: return self.prefix and ('%s-%s' % (self.prefix, field)) or field

Django: Custom "add only" inline

I want an inline form to only show its fields contents, and not let users to edit or remove entries, only add them. That means that the values would be similar when using the readonly_fields option, and the "Add another ..." link at the bottom would make a form appear, letting users add more entries.
The can_delete option it's useful here, but the readonly_fields lock both add and change possibilities. I imagine that building a new inline template would do. In that case, how would I just show the field values for each entry and then put a form at the bottom?
Edit: what I got until now:
# models.py
class AbstractModel(models.Model):
user = models.ForeignKey(User, editable = False)
... some more fields ...
class Meta:
abstract = True
class ParentModel(AbstractModel):
... fields ...
class ChildModel(AbstractModel):
parent = models.ForeignKey(ParentModel, ... options ...)
... fields ...
# admin.py
class ChildModelInline(admin.TabularInline):
model = ChildModel
form = ChildModelForm
can_delete = False
class ParentModelAdmin(admin.ModelAdmin):
... options ...
inlines = (ChildModelInline,)
# forms.py
class ChildModelForm(models.ModelForm):
user = forms.CharField(required = False)
... some more fields and stuff needed ...
def __init__(self, *args, **kwargs):
super(ChildModelForm, self).__init__(*args, **kwargs)
try: user = User.objects.get(id = self.instance.user_id)
except: return None
self.fields['user'].initial = user.first_name
self.fields['user'].widget.attrs['readonly'] = 'readonly'
In this example I'm doing like I wanted the user field as readonly.
In the last line, If I change the widget attribute to ['disabled'] = True, it works fine, but I need a text entry, not a disabled form field. I'm also aware that I'll need to override the save_model() and save_formsets() for this to work properly.
I would use extra=1 to get that last working form.
Then loop through all the forms except the last one in your view and change, like this, every field: In a Django form, how do I make a field readonly (or disabled) so that it cannot be edited?
You don't have to do it in the __init__, you can access those attributes after the entire formset is created of course.

Django "Enter a list of values" form error when rendering a ManyToManyField as a Textarea

I'm trying to learn Django and I've ran into some confusing points. I'm currently having trouble creating a movie using a form. The idea of the form is to give the user any field he'd like to fill out. Any field that the user fills out will be updated in its respective sql table (empty fields will be ignored). But, the form keeps giving me the error "Enter a list of values" when I submit the form. To address this, I thought stuffing the data from the form into a list and then returning that list would solve this.
The first idea was to override the clean() in my ModelForm. However, because the form fails the is_valid() check in my views, the cleaned_data variable in clean() doesn't contain anything. Next, I tried to override the to_python(). However, to_python() doesn't seem to be called.
If I put __metaclass__ = models.SubfieldBase in the respective model, I receive the runtime error
"TypeError: Error when calling the
metaclass bases
metaclass conflict: the metaclass of a derived class must be a
(non-strict) subclass of the
metaclasses of all its bases"
My approach doesn't seem to work. I'm not sure how to get around the 'Enter a list of values" error! Any advice?
Here is the relevant code (updated):
models.py
""" Idea:
A movie consists of many equipments, actors, and lighting techniques. It also has a rank for the particular movie, as well as a title.
A Theater consists of many movies.
A nation consists of many theaters.
"""
from django.db import models
from django.contrib.auth.models import User
class EquipmentModel(models.Model):
equip = models.CharField(max_length=20)
# user = models.ForeignKey(User)
class ActorModel(models.Model):
actor = models.CharField(max_length=20)
# user = models.ForeignKey(User)
class LightModel(models.Model):
light = models.CharField(max_length=20)
# user = models.ForeignKey(User)
class MovieModel(models.Model):
# __metaclass__ = models.SubfieldBase
rank = models.DecimalField(max_digits=5000, decimal_places=3)
title = models.CharField(max_length=20)
equipments = models.ManyToManyField(EquipmentModel, blank=True, null=True)
actors = models.ManyToManyField(ActorModel, blank=True, null=True)
lights = models.ManyToManyField(LightModel, blank=True, null=True)
class TheaterModel(models.Model):
movies = models.ForeignKey(MovieModel)
class NationModel(models.Model):
theaters = models.ForeignKey(TheaterModel)
=====================================
forms.py
"""
These Modelforms tie in the models from models.py
Users will be able to write to any of the fields in MovieModel when creating a movie.
Users may leave any field blank (empty fields should be ignored, ie: no updates to database).
"""
from django import forms
from models import MovieModel
from django.forms.widgets import Textarea
class MovieModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MovieModelForm, self).__init__(*args, **kwargs)
self.fields["actors"].widget = Textarea()
self.fields["equipments"].widget = Textarea()
self.fields["lights"].widget = Textarea()
def clean_actors(self):
data = self.cleaned_data.get('actors')
print 'cleaning actors'
return [data]
class Meta:
model = MovieModel
=============================================
views.py
""" This will display the form used to create a MovieModel """
from django.shortcuts import render_to_response
from django.template import RequestContext
from forms import MovieModelForm
def add_movie(request):
if request.method == "POST":
form = MovieModelForm(request.POST)
if form.is_valid():
new_moviemodel = form.save()
return HttpResponseRedirect('/data/')
else:
form = MovieModelForm()
return render_to_response('add_movie_form.html', {form:form,}, context_instance=RequestContext(request))
The probable problem is that the list of values provided in the text area can not be normalized into a list of Models.
See the ModelMultipleChoiceField documentation.
The field is expecting a list of valid IDs, but is probably receiving a list of text values, which django has no way of converting to the actual model instances. The to_python will be failing within the form field, not within the form itself. Therefore, the values never even reach the form.
Is there something wrong with using the built in ModelMultipleChoiceField? It will provide the easiest approach, but will require your users to scan a list of available actors (I'm using the actors field as the example here).
Before I show an example of how I'd attempt to do what you want, I must ask; how do you want to handle actors that have been entered that don't yet exist in your database? You can either create them if they exist, or you can fail. You need to make a decision on this.
# only showing the actor example, you can use something like this for other fields too
class MovieModelForm(forms.ModelForm):
actors_list = fields.CharField(required=False, widget=forms.Textarea())
class Meta:
model = MovieModel
exclude = ('actors',)
def clean_actors_list(self):
data = self.cleaned_data
actors_list = data.get('actors_list', None)
if actors_list is not None:
for actor_name in actors_list.split(','):
try:
actor = Actor.objects.get(actor=actor_name)
except Actor.DoesNotExist:
if FAIL_ON_NOT_EXIST: # decide if you want this behaviour or to create it
raise forms.ValidationError('Actor %s does not exist' % actor_name)
else: # create it if it doesnt exist
Actor(actor=actor_name).save()
return actors_list
def save(self, commit=True):
mminstance = super(MovieModelForm, self).save(commit=commit)
actors_list = self.cleaned_data.get('actors_list', None)
if actors_list is not None:
for actor_name in actors_list.split(","):
actor = Actor.objects.get(actor=actor_name)
mminstance.actors.add(actor)
mminstance.save()
return mminstance
The above is all untested code, but something approaching this should work if you really want to use a Textarea for a ModelMultipleChoiceField. If you do go down this route, and you discover errors in my code above, please either edit my answer, or provide a comment so I can. Good luck.
Edit:
The other option is to create a field that understands a comma separated list of values, but behaves in a similar way to ModelMultipleChoiceField. Looking at the source code for ModelMultipleChoiceField, it inhertis from ModelChoiceField, which DOES allow you to define which value on the model is used to normalize.
## removed code because it's no longer relevant. See Last Edit ##
Edit:
Wow, I really should have checked the django trac to see if this was already fixed. It is. See the following ticket for information. Essentially, they've done the same thing I have. They've made ModelMutipleChoiceField respect the to_field_name argument. This is only applicable for django 1.3!
The problem is, the regular ModelMultipleChoiceField will see the comma separated string, and fail because it isn't a List or Tuple. So, our job becomes a little more difficult, because we have to change the string to a list or tuple, before the regular clean method can run.
class ModelCommaSeparatedChoiceField(ModelMultipleChoiceField):
widget = Textarea
def clean(self, value):
if value is not None:
value = [item.strip() for item in value.split(",")] # remove padding
return super(ModelCommaSeparatedChoiceField, self).clean(value)
So, now your form should look like this:
class MovieModelForm(forms.ModelForm):
actors = ModelCommaSeparatedChoiceField(
required=False,
queryset=Actor.objects.filter(),
to_field_name='actor')
equipments = ModelCommaSeparatedChoiceField(
required=False,
queryset=Equipment.objects.filter(),
to_field_name='equip')
lights = ModelCommaSeparatedChoiceField(
required=False,
queryset=Light.objects.filter(),
to_field_name='light')
class Meta:
model = MovieModel
to_python AFAIK is a method for fields, not forms.
clean() occurs after individual field cleaning, so your ModelMultipleChoiceFields clean() methods are raising validation errors and thus cleaned_data does not contain anything.
You haven't provided examples for what kind of data is being input, but the answer lies in form field cleaning.
http://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-a-specific-field-attribute
You need to write validation specific to that field that either returns the correct data in the format your field is expecting, or raises a ValidationError so your view can re-render the form with error messages.
update: You're probably missing the ModelForm __init__ -- see if that fixes it.
class MovieModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MovieModelForm, self).__init__(*args, **kwargs)
self.fields["actors"].widget = Textarea()
def clean_actors(self):
data = self.cleaned_data.get('actors')
# validate incoming data. Convert the raw incoming string
# to a list of ids this field is expecting.
# if invalid, raise forms.ValidationError("Error MSG")
return data.split(',') # just an example if data was '1,3,4'