Django SimpleHistoryAdmin How to Change Custom Column Name - django

In Django SimpleHistoryAdmin List View are 5 default columns (OBJECT, DATE/TIME, COMMENT, CHANGE_BY, CHANGE_REASON). I may add another column adding it to the history_list_display but name of this additional column is displayed as it's name in code, for example first_name. Is there an easy way to display it as First Name?

the column will show the verbose_name defined in the field definition on the Model.
If you do not have access to the Model but to the ModelAdmin, you can try to do something like:
class SimpleHistoryAdmin(...):
...
def _myfirst_name(self, instance):
return instance.first_name
_myfirst_name.short_description = "DESIRED FIELD LABEL"
You can user gettext and it's variants to provide translations for your custom label if needed.
Hope this helps.

Related

Django ForeignKey, hide from user select options in dropdown menu

I have standard Django models with ForeignKey.
Django docs:
"ForeignKey is represented by django.forms.ModelChoiceField, which is a ChoiceField whose choices are a model QuerySet."
and
"If the model field has choices set, then the form field’s widget will be set to Select, with choices coming from the model field’s choices."
Now I have dropdown menu with choices.
I don't want dropdown menu where user can see options. I want CharField(textfield or similar) where user type, but still
that must be one of the options from the database for that field. He must type a valid entry.
I tried:
class TransakcijeForm(forms.ModelForm):
model = models.Transakcije
fields = .....
labels = .....
widgets ={'subscriber':forms.TextInput()}
but I receive the message:
"Select a valid choice. That choice is not one of the available choices."
(entry is correct and it works with dropdown menu)
This is my first question here and I'm sorry if I miss the form.
The reason you are getting that error is because your form is still treating the subscriber field as a ModelChoiceField because you are only overriding what widget is rendered to html. You need to change the actual field type of your field. You can define your form like this:
from django.core.exceptions import ValidationError
class TransakcijeForm(forms.ModelForm):
subscriber = forms.CharField()
class Meta:
model = models.Transakcije
fields = ....
labels = ....
def clean_subscriber(self):
subscriber_id = self.cleaned_data['subscriber']
try:
# adjust this line to appropriately get the model object that you need
subscriber = SubscriberModel.objects.get(id=subscriber_id)
return subscriber
except:
raise ValidationError('Subscriber does not exist')
The line subscriber = forms.CharField() will change the form to treat the field as a CharField rather than a ModelChoiceField. Doing this will cause the form to return the subscriber field value as a string, so you will need to get the appropriate model object based on the value of the field. That is what the clean_subscriber(self) function is for. It needs to be named like clean_<field name>(). That function will take the string that is returned by the form, try and find the correct model object and return it if an object is found. If it finds no matching objects it will raise a ValidationError so the form doesn't submit with a bad value.

How to test CheckboxSelectMultiple in save

I have a form (ModelForm) in Django, where I am adding a field for users in the init method as so:
self.fields["users"] = forms.ModelMultipleChoiceField(
queryset=users, widget=forms.CheckboxSelectMultiple, required=False,label="Add Designer(s)"
)
In the save method how I can iterate over the queryset for this field, however, I do not know how I can test if the particular model has been selected/checked. Help, please.
EDIT:
Let's say that you have a form where you want to be able to add users to a certain project, I set the users field as above (also usedMultipleChoiceField) but my real question is how do you determine the state of those checkboxes (which users should be added)?
Managed to fix it using MultipleChoiceField instead of ModelMultipleChoiceField. Then populated the choices with existing event IDs and passed it to the template.
In forms:
choices = forms.MultipleChoiceField(widget = forms.CheckboxSelectMultiple())
In views:
form.fields['choices'].choices = [(x.eventID, "Event ID: " + x.eventID) for x in unapproved]
Had to change some of the logic for finding and editing Event objects too.
The Django documentation states that a ModelMultipleChoiceField normalizes to a QuerySet of model instances. That means in your example, it will only return the users that have been checked. If none have been checked, it will return an empty QuerySet.
If you are overriding your ModelForm save method, you could include something like this:
selected_users = self.cleaned_data.get('users')
for user in selected_users:
project_users.add(user)

How can I add an additional description to a Django field which would be stored in the database?

I would like to provide context help for the input fields in my forms ("First name": "Your first name. Please enter all of them if you have several."). Instead of hard-coding them in source code, I would like to make those help texts editable through the admin interface. My idea is to somehow extend the field class (include a new attribute similar to verbose_name) and store that in the database (probably a three-column table 'Model, Field, Help' would be sufficient).
However, I don't know whether this is feasible or has been done before. Do you? Could you give me some to where to start if it has been not?
Every field in a form already contains help_text, though it should be declared as a parameter in the field, in the Form class.
E.g.,
class SomeForm(forms.Form):
some_field1 = forms.CharField(verbose_name="Some Field 1", max_length=100, help_text="Please the first field.")
some_field2 = forms.CharField(verbose_name="Some Field 2", max_length=100, help_text="Please the second field.")
Personally, I don't see the benefit of having it in the database rather than in the form tied to the field.
EDIT:
So you can override the help text. Let's say first imagine you had a dictionary for each form you want to override help_text in a form. Before rendering the Context, you could reprocess the form with the dictionary as such:
my_form = SomeForm()
for field_name, new_help_text in my_form_override_help_text_dict.items():
my_form.fields[field_name].help_text = new_help_text
and then add my_form to the context before rendering it.
Now where and how you want to store the help text is your choice; e.g., your solution of creating a ModelFieldHelp with three char fields (Model Name, Field Name, Help Text) would work, then you need something like
class ModelHelpField(models.Model):
model_name = CharField(max_length=50)
field_name = CharField(max_length=50)
new_help_text = CharField(max_length=50)
field_help_qs= ModelHelpField.objects.filter(model_name='SomeModel')
my_form_override_help_text_dict = dict([(mfh.field_name, mfh.new_help_text) for mfh in field_help_qs])
Now it may make sense to automate this process for all your models that you create forms for, by defining a function in the form or model that automatically creates these ModelHelpFields (if not defined) and updates itself with the current help text after being initialized ...

Django admin site: how to include conditional fields?

I wonder if is it possible to add some conditional fields in django.
Say I have a category model which has an ID, name and description fields.
What I would like is to add a many-to-many field in my Product model that links it with the Category ID model... and as a helping reference show what the name of that Category would be.
I know I could just link it to the category name, but my real scenario is a bit more complex and I would really need to display a second field based on the selection in another !
Many thanks!
In addition to Daniel's answer: If you just want to customize the representation of the objects in a ModelChoiceField (and not change it in general what you would do with the __unicode__ method): The field class has method label_from_instance, which returns by default the object's unicode value, but you can override it as you like:
class CategoryChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return "%s %s" % obj.pk, obj.name
That's not a conditional field. If I understand you correctly, all you really need is to customise the display of the related item, so that it shows the name field rather than the raw ID. Luckily, that is what Django will do by default if you define a __unicode__ method on the Category model, which returns the value you want to display instead of the ID.

How would you populate a field based on another field

From the admin panel I want to populate a slug field based on a certain text field
eg.
Title: My Awesome Page
would automaticaly populate
Slug: my_awesome_page
There used to be a prepoulate_from option for the SlugField up to 0.96. It became an admin option after that. See here for reference on that.
Alternatively, you could override the model's save method to calculate the value of the slug field on save().
This question may be helpful, too.
There is also an app called django-autoslug which provides a field type AutoSlugField. Using that, you could have:
class Something(models.Model):
title = models.CharField(max_lenght=200)
slug = AutoSlugField(populate_from='title')
...
This AutoSlugField has many nice features, such as generating a slug so that it is unique either globally of combined with some other field (maybe a category or the year part of a DateTimeField).
See http://pypi.python.org/pypi/django-autoslug for further details.