How to add a custom field in django model form? - python

I'm trying to add a readonly field in a form.
The model Folder is registered in admin site. The FolderAdminForm defines the custom field statistics. There isn't statistcs field in the Folder model, I just want to put some readonly data on the form. This data is defined in the template.
But I get a error whenever the user doesn't have edit permission. If the user only have the view permission,this error is raised:
AttributeError: Unable to lookup 'statistics' on Folder or FolderAdmin
Here is my code:
class CustomWidget(forms.Textarea):
template_name = 'widget.html'
class FolderAdminForm(forms.ModelForm):
class Meta:
model = Folder
fields = ('field1', 'field2', 'field3',)
statistics = forms.Field(
widget=CustomWidget,
label='Estatísticas',
help_text='Estatísticas da pasta',
)

According to the last part of this answer, you could try to override the forms __init__() method and assign the fields initial attribute.
This could look like:
def __init__(self, *args, **kwargs):
# only change attributes if an instance is passed
instance = kwargs.get('instance')
if instance:
self.base_fields['statistics'].initial = 'something'
super().__init__(*args, **kwargs)
Does this work for you?

The error only occurred whenever I tried to open a folder instance without edit permission (i.e. with read only permission). Therefore, django consider the statistics field as a read only field and then search for a label for this field. Django look for this label in Model, ModelAdmin. Since none of them has the 'statistics' attribute, the error is raised.
So, this worked for me:
class FolderAdminForm(forms.ModelForm):
class Meta:
model = Folder
fields = ('field1', 'field2', 'field3',)
labels = {'statistics': 'Estatísticas'}
statistics = forms.Field(
widget=CustomWidget,
label='Estatísticas',
help_text='Estatísticas da pasta',
)
Therefore, whenever django looks for a label for statistics, it finds this label and the error is not raised. I don't know why it doesn't recognize the label passed as a Field parameter.

Related

Access a field's custom class in Django's ModelForm

I'm working on a ModelForm in Django that uses a Model, which has a custom CharField.
When handling form errors I want to show the user valid examples (I thought this shouldn't be part of the raised ValidationError). However, when I try to access the fields using myform.fields by getting the invalid fields and the corresponding error messages using myform.errors.items() type(myform.fields["myfield"]) returns <class 'django.forms.fields.CharField'> instead of my custom MyField.
Therefore myform.fields["myfield"].test() raises AttributeError: 'CharField' object has no attribute 'test'.
How do I get the correct MyField instance from form.fields?
class MyField(models.CharField):
pass
class MyModel(models.Model):
myfield = MyField(max_length=20)
class MyForm(forms.ModelForm):
class Meta:
exclude = tuple()
model = MyModel
myform = MyForm()
print(type(myform.fields["myfield"]))
A model field is different than a form field. A model field focusses on storing the data in the database, a form field.
You can define an extra form field, for example:
# app_name/forms/fields.py
from django.forms import CharField
class MyField(Field):
# …
pass
Now we can specify this form field as the default field for the MyField model field:
app_name/fields.py
class MyField(models.CharField):
# …
def formfield(self, **kwargs):
return super().formfield(**{
'form_class': app_name.forms.fields.MyField,
**kwargs,
})
If you thus now use your MyField model field (the second code fragment), it will use the MyField form field (the first code fragment).

Where should i do the django validations for objects and fields?

I'm creating a django application which uses both the Django Rest Framework and the plain django-views as entrypoint for users.
I want to do validation both independant fields of my models, and on objects on a whole. For example:
Field: is the entered licence-plate a correct one based on a regex function. No relation to other fields.
Object: Is the entered zipcode valid for the given country. Relates to zipcode and country in the model.
For the DRF-API i use ModelSerializers which automatically call all the validators i have placed in my Model, for example:
class MyModel(models.Model):
licence_plate = CharField(max_length=20, validators=[LicencePlateValidator])
Since the validator is given in the model, the API POSTS (because i use a ModelSerializer), as well as the objects created in the django admin backend are validated.
But when i want to introduce object level validation i need to do that in the serializer's validate()-method, which means objects are only validated in the API.
I'll have to override the model's save method too, to validate the objects created in the Django admin page.
Question: This seems a bit messy to me, is there a single point where i can put the object-level validators so that they are run at the API and in the admin-page, like i did with the field-level validation (I only have to put them in my model-declaration and everything is handled)
For model-level validation, there is the Model.clean method.
It is called if you are using ModelForm (which is used by default in admin), so this solves django views and admin parts.
On the other hand, DRF does not call models' clean automatically, so you will have to do it yourself in Serializer.validate (as the doc suggests). You can do it via a serializer mixin:
class ValidateModelMixin(object)
def validate(self, attrs):
attrs = super().validate(attrs)
obj = self.Meta.model(**attrs)
obj.clean()
return attrs
class SomeModelSerializer(ValidateModelMixin, serializers.ModelSerializer):
#...
class Meta:
model = SomeModel
or write a validator:
class DelegateToModelValidator(object):
def set_context(self, serializer):
self.model = serializer.Meta.model
def __call__(self, attrs):
obj = self.model(**attrs)
obj.clean()
class SomeModelSerializer(serializers.ModelSerializer):
#...
class Meta:
model = SomeModel
validators = (
DelegateToModelValidator(),
)
Caveats:
an extra instantiation of your models just to call clean
you will still have to add the mixin/validator to your serializers
You can create a separate function validate_zipcode_with_country(zipcode, country) which will take 2 arguments zipcode and country.
Then, we will call this method in the serializer's validate() and in our model's clean().
from django.core.exceptions import ValidationError
def validate_zipcode_with_country(zipcode, country):
# check zipcode is valid for the given country
if not valid_zipcode:
raise ValidationError("Zipcode is not valid for this country.")
Then in your serializers.py, you need to call this function in your validate() function.
class MySerializer(serializers.ModelSerializer):
def validate(self, attrs):
zipcode = attrs.get('zipcode')
country = attrs.get('country')
validate_zipcode_with_country(zipcode, country) # call the function
...
Similarly, you need to override the model's clean() and call this function.
class MyModel(models.Model):
def clean(self):
validate_zipcode_with_country(self.zipcode, self.country) # call this function
...

ModelForm fields order inaffected? [duplicate]

This question already has answers here:
How can I order fields in Django ModelForm?
(2 answers)
Closed 9 years ago.
trying to alter order of fields in admin ModelForm.
Bellow is my attempt, however order is kept unchanged. Added fields oi_number and vat_number are rendered at the end besides they are not at the end in self.fields SortedDict dictionary.
class ContactAdminForm(forms.ModelForm):
oi_number = fields_for_model(OrganizationExtra)['oi_number']
vat_number = fields_for_model(OrganizationExtra)['vat_number']
  # fields = ('organization', 'oi_number', 'vat_number')
# ^^^ this won't affect fields order either
class Meta:
model = Organization
def __init__(self, *args, **kwargs):
super(ContactAdminForm, self).__init__(*args, **kwargs)
try:
org_ix = self.fields.keyOrder.index('organization')
self.fields.keyOrder.insert(org_ix+1, self.fields.keyOrder[-2])
self.fields.keyOrder.insert(org_ix+2, self.fields.keyOrder[-1])
del self.fields.keyOrder[-2:]
except ValueError:
pass
Does get the order of fields resolved before __init__ method is called ? How can I change their order ?
Update:
The above ModelForm is used as a form in admin model which defines its own fields, so if I put all fields definition in above form, I'll get FieldError exception about unknown field name:
class ContactAdminForm(forms.ModelForm):
...
class Meta:
model = Organization
fields = ('organization', 'oi_number', 'vat_number')
class ContactOptionsEx(ContactOptions):
form = ContactAdminForm
admin.site.register(Contact, ContactOptionsEx)
# at attempt to render the form:
# FieldError at /admin/contact/contact/3/
# Unknown field(s) (organization) specified for Organization
However the field named organization does exist and is available in ContactAdminForm.__init__ method.
The error
Unknown field(s) (organization) specified for Organization
does not refer to a field on your form, but to a field on the model (Organization).
I think the problem here is that you are trying to add fields from a different Model (OrganizationExtra) to the ModelForm for Organization. There is always a one-to-one relation between a ModelForm and a Model. If you want to edit a related instance in the admin, you can use inlines:
class OrganizationExtraInline(admin.StackedInline):
model = OrganizationExtra
class ContactOptionsEx(ContactOptions):
inlines = ContactOptions.inlines + [OrganizationExtraInline]
# ...
If you want to limit the inline to one instance, use a OneToOneField or max_num = 1

Django Admin TabularInline Complaining About Missing Field

I have the following model and TabularInline subclasses:
class SomeModel(models.Model):
name = models.CharField(max_length=50)
class SomeModelInline(admin.TabularInline):
model = SomeModel
class SomeOtherModelAdmin(admin.ModelAdmin):
inlines = [SomeModelInline]
Without explicitly specifying the TabularInline's fields, Django's admin shows the fields "id" and "name". However, when I try and do:
class SomeModelInline(admin.TabularInline):
model = SomeModel
fields ['id','name']
Django throws the ImproperlyConfigured exception:
'SomeModelInline.fields' refers to field 'id' that is missing from the form.
What's going on here? Why can't I explicitly specify the id, even though Django's clearly capable of accessing it?
Ids are non-editable, by default inline shows the editable fields, but you can show the non-editable fields as well
From django docs
fields can contain values defined in ModelAdmin.readonly_fields to be
displayed as read-only.
So first add 'id' to readonly_fields, then add it to fields

Django: Faking a field in the admin interface?

I have a model, Foo. It has several database properties, and several properties that are calculated based on a combination of factors. I would like to present these calculated properties to the user as if they were database properties. (The backing factors would be changed to reflect user input.) Is there a way to do this with the Django admin interface?
I would suggest you subclass a modelform for Foo (FooAdminForm) to add your own fields not backed by the database. Your custom validation can reside in the clean_* methods of ModelForm.
Inside the save_model method of FooAdmin you get the request, an instance of Foo and the form data, so you could do all processing of the data before/after saving the instance.
Here is an example for a model with a custom form registered with django admin:
from django import forms
from django.db import models
from django.contrib import admin
class Foo(models.Model):
name = models.CharField(max_length=30)
class FooAdminForm(forms.ModelForm):
# custom field not backed by database
calculated = forms.IntegerField()
class Meta:
model = Foo
class FooAdmin(admin.ModelAdmin):
# use the custom form instead of a generic modelform
form = FooAdminForm
# your own processing
def save_model(self, request, obj, form, change):
# for example:
obj.name = 'Foo #%d' % form.cleaned_data['calculated']
obj.save()
admin.site.register(Foo, FooAdmin)
Providing initial values for custom fields based on instance data
(I'm not sure if this is the best solution, but it should work.)
When a modelform for a existing model instance in the database is constructed, it gets passed this instance. So in FooAdminForm's __init__ one can change the fields attributes based on instance data.
def __init__(self, *args, **kwargs):
super(FooAdminForm, self).__init__(*args, **kwargs)
# only change attributes if an instance is passed
instance = kwargs.get('instance')
if instance:
self.fields['calculated'].initial = (instance.bar == 42)
It's easy enough to get arbitrary data to show up in change list or make a field show up in the form: list_display arbitrarily takes either actual model properties, or methods defined on the model or the modeladmin, and you can subclass forms.ModelForm to add any field type you'd like to the change form.
What's far more difficult/impossible is combining the two, i.e. having an arbitrary piece of data on the change list that you can edit in-place by specifying list_editable. Django seems to only accept a true model property that corresponds to a database field. (even using #property on the method in the model definition is not enough).
Has anyone found a way to edit a field not actually present on the model right from the change list page?
In the edit form, put the property name into readonly_fields (1.2 upwards only).
In the changelist, put it into list_display.
You can use the #property decorator in your model (Python >= 2.4):
class Product(models.Model):
#property
def ranking(self):
return 1
"ranking" can then be used in list_display:
class ProductAdmin(admin.ModelAdmin):
list_display = ('ranking', 'asin', 'title')

Categories

Resources