Replace "this field is required" message in django admin - django

is there a way to replace the default error message in django admin. I'm using custom widget so I have the form and I was wondering is there something like:
field_1 = forms.Charfield(widget=X, error_messge='y')
I already tried to add claen_field_1 method but it looks that it is not called when the field is empty. Any ideas will be appreciated

yes there is and actually this is forms functionality and not admin functionality ;), you can use this anywhere.
field_1 = forms.Charfield(widget=X, error_messages={'required': 'y'})
for more information see the django docs

Related

add user friendly json editor to django admin

I have a django app, using also rest_framework, and a model Product with field of type JSONField. so data is stored as JSON in Postgres, Now I want to provide the admin with a nice user friendly way on how he can change the json field (names/keys and values). is there an extension for that or is there a faster way on how to do that.
here is the column definition in the database.
my_column = JSONField(default={"editorial1": "text 1", "editorial_2": "text2", "editorial_3": "text"})
BOTH KEYS AND VALUES SHOULD BE EDITABLE BY THE ADMIN
The admin should not know anything about JSON, and should not enter/edit any json format field
You can use prettyjson's PrettyJSONWidget:
class ProductModelForm(forms.ModelForm):
class Meta:
fields = (
...
'my_column',
)
widgets = {
'my_column': PrettyJSONWidget(),
}
I ended up using the django-admin-json-editor. Not the best thing in the world, but it does the trick
https://github.com/abogushov/django-admin-json-editor
You can try https://github.com/jrief/django-entangled
In comparison to the editors mentioned above, it doesn't replace the widget used to render the JSON, but allows to override the ModelForm which otherwise is generated by Django's ModelAdmin.

Django Admin Page: Help Text for Model Methods?

I have a model method in Django that I am displaying on an admin page just like I would a model field. With a field, I can just add a help_text argument to it to give a description of what the field is and what the user should put into it. However, with a model method, help_text does not work. Adding the attribute short_description changes the way the method name is displayed, which is sort of okay, but I'm looking for a way to add a few sentences of description beneath the method value that is displayed. Is there any way to do this natively, or would I have to resort to overriding admin templates or something? (Which I do not think is worth it for something this minor).
You can do this using JS.
Replace ID-OF-THE-FIELD with the actual id of the desired field.
(function($) {
var myField = $('#ID-OF-THE-FIELD');
// find the id of the desired field by doing
// Right-Click > Inspect element
var help = $('<p class="help">A very long help text</p>');
help.insertAfter(myField);
})(django.jQuery);
Put this code into a JS file and supply this file using class Media of your ModelAdmin class.

limit choices for foreign field based on other field

Is it possible to do really dynamic form in AdminModel? I have following models:
class MyModel(models.Model):
firstfield=models.ForeignKey(First)
secondField= models.ForeignKey(Second, blank=True,null=True)
#some other fields
class Second(models.Model):
firstfield=models.ForeignKey(First)
#other fields
As you can see Second is optional. But I want it to limit according to current selection in First? It would require some page refreshing or some ajax work but I simply don't know how to even pass First value. Maybe I should add it to request and then use something similar to:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey ?
You can do it through ajax request. If you don't know how it works see the below links.
How to implement two dropdowns dependent on each other using Django and jQuery
Dynamic select fields with JQuery and django

How do you change a model name when it shows up in Django admin?

I have a model name called StoreEntry. Django admin changes it to look like 'Store Entrys'. This is weird. If anything it should be Store entries. Any idea what's going on here and how to change it?
In the model's Meta class, set the verbose_name_plural. Docs.

AppEngine/Django: editing db.Key in the Admin app

Or, to be precise, how do I properly present a form to edit a db.ListProperty of db.Keys on a model admin page, with app-engine-patch for Django?
I have a Category like this:
class Category(db.Model):
title = db.CategoryProperty(required=True)
and a Post with this:
categories = db.ListProperty(db.Key)
Currently in the Django admin page the field is shown as a textbox containing the Python list object string, which is wrong and breaks saving:
[datastore_types.Key.from_path(u'blog_category', 3L, _app_id_namespace=u'xyz')]
So I've had to 'exclude' it in my ModelAdmin class. I've thought of writing a ModelForm that manually to wire up the Category db.Keys and present them as a Django multiselect widget, but I suspect there are easier ways to do it...
Having read through the App-engine-patch docs more thoroughly, seems that using ragendja.dbutils.KeyListProperty answers this problem, with the old Django multiselection list too. :)
New code:
categories = KeyListProperty(Category)