Add a field to the ModelAdmin Form - python

I want to give users the possibility to create multiple events at once. Therefore I would like to add a field to the admin-add-page where a number of repetitions can be specified. Then I want to override the save function and create multiple events (based on the input). I started writing some code but the admin add page does not update at all. I will show you the code below:
In admins.py:
class EventAdmin(admin.ModelAdmin):
form = EventForm
admin.site.register(Event, EventAdmin)
In forms.py
from django import forms
from django.db import models
from calendar_app.models import Event
class EventForm(forms.ModelForm):
name = models.CharField(max_length=100) # just for testing purpose
class Meta:
model = Event
def __init__(self, *args, **kwargs):
super(EventForm, self).__init__(*args, **kwargs)
if not kwargs.has_key('instance'):
self.fields['name'] = forms.CharField(label='Name')
self.base_fields['name'] = forms.CharField(label='Name')
def save(self, commit=True):
model = super(EventForm, self).save(commit=False)
# Save all the fields...
if commit:
model.save()
return model
But the "name" field is not showing up when I add an event. Any ideas? Thanks!

I used models.CharField instead of forms.CharField. See comments.

Related

Cannot modify a model field on save()

I'm a beginner with Django, and first time askig :)
I'm following a simple tutorial on generating a slug for a string (let's say a slug for a blog post generated from its title).
Perhaps I'm following an outdated guide, perhaps I'm missing a basic thing, I have no idea.
Django 2.0
Python 3.6
I am trying to do a very simple task of slugifying a simple string, so:
User enters a string in a simple form
When hitting 'save', the title goes through slugify and creates the
slug
Save.
models.py
from django.db import models
class Testmodel(models.Model):
title = models.CharField(max_length=220)
slug = models.SlugField(unique=True, null=True)
def __str__(self):
return self.title
views.py
from django.views.generic.edit import CreateView
class TestCreate(CreateView):
model = Testmodel
fields = '__all__'
forms.py
from django.forms import ModelForm
from .models import Testmodel
class TestCreateForm(ModelForm):
class Meta:
model = Testmodel
fields = '__all__'
Up until here everythig works, if I enter the slug manualy. In order to do it automaticaly, I have tried:
Overriding the save() method withing my ModelForm class.
Overriding the form_valid() method within the CreateView
Overriding the save() method within the model itself.
Tried to connect a pre_save signal to the model.
In all of these 4 tries, I had the same results:
When generating the form with the slug field, I couldn't do anything because it was required.
When generating the form without the slug field, nothing happens when I hit save.
The only way I have found to dodge this issue is to set the slug field to blank = True as well. I am not sure how secure it is, though?
Thank you!
Welcome to StackOverflow. You've written a wonderfuly constructed question (Cheers!)
When generating the form with the slug field, I couldn't do anything because it was required.
Okay so first we exlcude the slug because we want it to be autogenerated.
You can do this by
class TestCreateForm(ModelForm):
class Meta:
model = Testmodel
exclude = ['slug']
Now you'll get a form without the slug field.
When generating the form without the slug field, nothing happens when I hit save.
Now we override the save() function of the model itself since slug is a part of the model.
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super().save(*args, **kwargs)
But this will generate the slug everytime the model is saved.
We can go a step further and make sure the slug is set only if the model is 'created' and not every time it is 'updated'
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.title)
super().save(*args, **kwargs)

How to execute code on save in Django User model?

I would like to run some code specifically when the is_active field is changed for a Django User, similar to how the save method works for other models:
class Foo(models.Model):
...
def save(self, *args, **kwargs):
if self.pk is not None:
orig = Foo.objects.get(pk=self.pk)
if orig.is_active != self.is_active:
# code goes here
Can this be done through another model that is in one to one relation with the User model? Something like:
class Bar(models.Model):
owner = models.OneToOneField(User, on_save=?)
...
I guess I could duplicate the is_active field on the related model and then set the is_active field on the User when saving the related model. But this seems like a bit of a messy solution.
You're looking for this Signal
from django.db.models.signals import pre_save
from django.contrib.auth.models import User
def do_your_thing(sender, instance, **kwargs):
# Do something
print(instance)
pre_save.connect(do_your_thing, sender=User)

Make models belong to users in django

I am using django.contrib.auth and I am wondering how I can make an instance of a model "belong to" a particular user. Do I need to define a foreign key in the model that points to the user? I want users to keep their CRUD to themselves.
# The objects created from this model should belong
# to the user who created them and should not be
# viewable by other users of the website.
from django.contrib.auth.models import User
class Classroom(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=30)
def __unicode__ (self):
return self.name
Thanks.
This is the general approach I take:
Creating models that belong to the user
On the model, have a foreign key to the user.
Define a custom ModelForm that requires a user to be passed to the constructor (__init__()). Also, override the form's save() method so that the user passed to the constructor is added to the model instance, before it's saved.
Define class based views based on CreateView etc. that override the get_form_kwargs method to pass the form the user stored in the request.
Here's the code:
# models.py
from django.db import models
from django.contrib.auth.models import User
class MyModel(models.Model):
"A model that belongs to a user."
user = models.ForeignKey(User)
# forms.py
from django import forms
from .models import MyModel
class MyModelForm(forms.ModelForm):
"""A form for creating/updating MyModels that accepts
the user as a kwarg.
"""
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super(MyModelForm, self).__init__(*args, **kwargs)
def save(self, *args, **kwargs):
self.instance.user = self.user
return super(MyModelForm, self).save(*args, **kwargs)
class Meta:
model = MyModel
# Don't show the user on the form
exclude = ('user',)
# views.py
from django.views.generic import CreateView
from .models import MyModel
from .forms import MyModelForm
class MyModelCreateView(CreateView):
"View for creating a MyModel belonging to the current user."
model = MyModel
form_class = MyModelForm
def get_form_kwargs(self, *args, **kwargs):
form_kwargs = super(MyModelCreateView,
self).get_form_kwargs(*args, **kwargs)
# Pass the current user to the form constructor
form_kwargs['user'] = self.request.user
return form_kwargs
Listing models that belong to the user
This is more straightforward - you extend ListView and filter the queryset by the current user. (Read more about ListView.)
Editing models that belong to the user
Use a similar approach to MyModelCreateView, but extending an UpdateView instead. The important thing here is to check that the user has permission to edit:
# views.py
from django.views.generic import UpdateView
from django.core.exceptions import PermissionDenied
...
class MyModelUpdateView(UpdateView):
"View for the current user editing a MyModel."""
model = MyModel
form_class = MyModelForm
def get_object(self, *args, **kwargs):
object = super(MyModelUpdateView, self).get_object(*args, **kwargs)
# Raise a permission denied if the current user doesn't
# 'own' the MyModel they're trying to edit.
if object.user != self.request.user:
raise PermissionDenied
def get_form_kwargs(self, *args, **kwargs):
form_kwargs = super(MyModelCreateView,
self).get_form_kwargs(*args, **kwargs)
# Pass the current user to the form constructor
form_kwargs['user'] = self.request.user
return form_kwargs
If you would like to have multiple objects belong to the user, a foreign key field to the User object will work. Note, you can still use a foreign key, and have one instance by passing
the unique attribute to the field definition.
If one object (and only one) and belong to a user, use a one to one field. A one to one field can be accessed from either side of the models
You can use user.classroom or classroom.user, these bindings can be changed with the related_name attribute of one to one field definitions.

Add custom form fields that are not part of the model (Django)

I have a model registered on the admin site. One of its fields is a long string expression. I'd like to add custom form fields to the add/update pages of this model in the admin. Based on the values of these fields I will build the long string expression and save it in the relevant model field.
How can I do this?
I'm building a mathematical or string expression from symbols. The user chooses symbols (these are the custom fields that are not part of the model) and when they click save then I create a string expression representation from the list of symbols and store it in the DB. I don't want the symbols to be part of the model and DB, only the final expression.
Either in your admin.py or in a separate forms.py you can add a ModelForm class and then declare your extra fields inside that as you normally would. I've also given an example of how you might use these values in form.save():
from django import forms
from yourapp.models import YourModel
class YourModelForm(forms.ModelForm):
extra_field = forms.CharField()
def save(self, commit=True):
extra_field = self.cleaned_data.get('extra_field', None)
# ...do something with extra_field here...
return super(YourModelForm, self).save(commit=commit)
class Meta:
model = YourModel
To have the extra fields appearing in the admin just:
Edit your admin.py and set the form property to refer to the form you created above.
Include your new fields in your fields or fieldsets declaration.
Like this:
class YourModelAdmin(admin.ModelAdmin):
form = YourModelForm
fieldsets = (
(None, {
'fields': ('name', 'description', 'extra_field',),
}),
)
UPDATE:
In Django 1.8 you need to add fields = '__all__' to the metaclass of YourModelForm.
It it possible to do in the admin, but there is not a very straightforward way to it. Also, I would like to advice to keep most business logic in your models, so you won't be dependent on the Django Admin.
Maybe it would be easier (and maybe even better) if you have the two seperate fields on your model. Then add a method on your model that combines them.
For example:
class MyModel(models.model):
field1 = models.CharField(max_length=10)
field2 = models.CharField(max_length=10)
def combined_fields(self):
return '{} {}'.format(self.field1, self.field2)
Then in the admin you can add the combined_fields() as a readonly field:
class MyModelAdmin(models.ModelAdmin):
list_display = ('field1', 'field2', 'combined_fields')
readonly_fields = ('combined_fields',)
def combined_fields(self, obj):
return obj.combined_fields()
If you want to store the combined_fields in the database you could also save it when you save the model:
def save(self, *args, **kwargs):
self.field3 = self.combined_fields()
super(MyModel, self).save(*args, **kwargs)
Django 2.1.1
The primary answer got me halfway to answering my question. It did not help me save the result to a field in my actual model. In my case I wanted a textfield that a user could enter data into, then when a save occurred the data would be processed and the result put into a field in the model and saved. While the original answer showed how to get the value from the extra field, it did not show how to save it back to the model at least in Django 2.1.1
This takes the value from an unbound custom field, processes, and saves it into my real description field:
class WidgetForm(forms.ModelForm):
extra_field = forms.CharField(required=False)
def processData(self, input):
# example of error handling
if False:
raise forms.ValidationError('Processing failed!')
return input + " has been processed"
def save(self, commit=True):
extra_field = self.cleaned_data.get('extra_field', None)
# self.description = "my result" note that this does not work
# Get the form instance so I can write to its fields
instance = super(WidgetForm, self).save(commit=commit)
# this writes the processed data to the description field
instance.description = self.processData(extra_field)
if commit:
instance.save()
return instance
class Meta:
model = Widget
fields = "__all__"
You can always create new admin template, and do what you need in your admin_view (override the admin add URL to your admin_view):
url(r'^admin/mymodel/mymodel/add/$','admin_views.add_my_special_model')
If you absolutely only want to store the combined field on the model and not the two seperate fields, you could do something like this:
Create a custom form using the form attribute on your ModelAdmin. ModelAdmin.form
Parse the custom fields in the save_formset method on your ModelAdmin. ModelAdmin.save_model(request, obj, form, change)
I never done something like this so I'm not completely sure how it will work out.
The first (highest score) solution (https://stackoverflow.com/a/23337009/10843740) was accurate, but I have more.
If you declare fields by code, that solution works perfectly, but what if you want to build those dynamically?
In this case, creating fields in the __init__ function for the ModelForm won't work. You will need to pass a custom metaclass and override the declared_fields in the __new__ function!
Here is a sample:
class YourCustomMetaClass(forms.models.ModelFormMetaclass):
"""
For dynamically creating fields in ModelForm to be shown on the admin panel,
you must override the `declared_fields` property of the metaclass.
"""
def __new__(mcs, name, bases, attrs):
new_class = super(NamedTimingMetaClass, mcs).__new__(
mcs, name, bases, attrs)
# Adding fields dynamically.
new_class.declared_fields.update(...)
return new_class
# don't forget to pass the metaclass
class YourModelForm(forms.ModelForm, metaclass=YourCustomMetaClass):
"""
`metaclass=YourCustomMetaClass` is where the magic happens!
"""
# delcare static fields here
class Meta:
model = YourModel
fields = '__all__'
This is what I did to add the custom form field "extra_field" which is not the part of the model "MyModel" as shown below:
# "admin.py"
from django.contrib import admin
from django import forms
from .models import MyModel
class MyModelForm(forms.ModelForm):
extra_field = forms.CharField()
def save(self, commit=True):
extra_field = self.cleaned_data.get('extra_field', None)
# Do something with extra_field here
return super().save(commit=commit)
#admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
form = MyModelForm
You might get help from my answer at :
my response previous on multicheckchoice custom field
You can also extend multiple forms having different custom fields and then assigning them to your inlines class like stackedinline or tabularinline:
form =
This way you can avoid formset complication where you need to add multiple custom fields from multiple models.
so your modeladmin looks like:
inlines = [form1inline, form2inline,...]
In my previous response to the link here, you will find init and save methods.
init will load when you view the page and save will send it to database.
in these two methods you can do your logic to add strings and then save thereafter view it back in Django admin change_form or change_list depending where you want.
list_display will show your fields on change_list.
Let me know if it helps ...
....
class CohortDetailInline3(admin.StackedInline):
model = CohortDetails
form = DisabilityTypesForm
...
class CohortDetailInline2(admin.StackedInline):
model = CohortDetails
form = StudentRPLForm
...
...
#admin.register(Cohort)
class CohortAdmin(admin.ModelAdmin):
form = CityInlineForm
inlines = [uploadInline, cohortDetailInline1,
CohortDetailInline2, CohortDetailInline3]
list_select_related = True
list_display = ['rto_student_code', 'first_name', 'family_name',]
...

Django: form save exculde certain field

I do not understand the official document about exclude .
Set the exclude attribute of the ModelForm‘s inner Meta class to a list of fields to be excluded from the form.
For example:
class PartialAuthorForm(ModelForm):
class Meta:
model = Author
exclude = ['title']
Since the Author model has the 3 fields name, title and birth_date, this will result in the fields name and birth_date being present on the form.
My understanding is as follows: django form save method will save all form data.If one set exclude =('something',) , 'something' field will not show on frontend and wouldn't be save while calling form save method.
But when I do as the document saying, 'something' field still show.What's the matter?
I also want to add some fields to a form for validating which can show on frontend without saving.It is stange that I find nothing about this need.
**update**
my code :
class ProfileForm(Html5Mixin, forms.ModelForm):
password1 = forms.CharField(label=_("Password"),
widget=forms.PasswordInput(render_value=False))
password2 = forms.CharField(label=_("Password (again)"),
widget=forms.PasswordInput(render_value=False))
captcha_text = forms.CharField(label=_("captcha"),
widget=forms.TextInput())
captcha_detext = forms.CharField(
widget=forms.HiddenInput())
class Meta:
model = User
fields = ("email", "username")
exclude = ['captcha_text']
def __init__(self, *args, **kwargs):
super(ProfileForm, self).__init__(*args, **kwargs)
..........
def clean_username(self):
.....
def clean_password2(self):
....
def save(self, *args, **kwargs):
"""
Create the new user. If no username is supplied (may be hidden
via ``ACCOUNTS_PROFILE_FORM_EXCLUDE_FIELDS`` or
``ACCOUNTS_NO_USERNAME``), we generate a unique username, so
that if profile pages are enabled, we still have something to
use as the profile's slug.
"""
..............
def get_profile_fields_form(self):
return ProfileFieldsForm
if exclude only affect the model defined under class Meta , so exclude = ['captcha_text'] would not work?
exclude = ['title'] will exclude the field from the form, not from the model.
form.save() will try to save the model instance with the available for fields, but model might throw any error pertaining to the missing field.
To add extra fields in model form, do this:
class PartialAuthorForm (ModelForm):
extra_field = forms.IntegerField()
class Meta:
model = Author
def save(self, *args, **kwargs):
# do something with self.cleaned_data['extra_field']
super(PartialAuthorForm, self).save(*args, **kwargs)
But make sure there is no field called "PartialAuthorForm" in the model Author.
First, the reason why your title field is still displayed must be somewhere in your view. Be sure that you create your (unbound) form instance like this:
form = PartialAuthorForm()
and try this simple rendering method in the template
{{ form.as_p }}
Second, it should be no problem to add extra fields to a model form, see e.g. this post.

Categories