Django Admin - Detect changes in inline fields - django

I am pretty new to python. I have Models named Project and ProjectTopic.
class Project(models.Model):
name = models.CharField(max_length=250)
description = models.TextField()
class ProjectTopic(models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE)
title = models.CharField(max_length=250)
In the admin.py, I have added the ProjectTopic as an inline field. The admin can add as many topics that he likes.
Now the requirement is I need to send a notification if there is any change in the project that includes the ProjectTopic section. I have added
def save_model(self, request, obj, form, change):
if 'description' in form.changed_data:
send_notification()
super().save_model(request, obj, form, change)
But don't know how to track the changes in the inline ProjectTopic section.

I have been looking into getting a proper solution to this as well. In case you are not strictly looking to access the changed inline fields from within the Project admin's save_model() method, I can suggest three solutions. Even though it might not be the best solution, you can get your job done.
Method 1
You can use the save_related() method as follows
def save_related(self, request, form, formsets, change):
if form.changed_data:
# Call function to notify change in Project fields
send_project_notification()
for form_set in formsets:
if form_set.has_changed():
# Call function to notify change in ProjectTopic fields
send_project_topic_notification()
super().save_related(request, form, formsets, change)
Method 2
It would require you to use the Field Tracker using which you can track changes in model fields. Add the project_topic_tracker to your model and use a post_save signal to track the changes as follows.
models.py
class ProjectTopic(models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE)
title = models.CharField(max_length=250)
project_topic_tracker = FieldTracker()
signals.py
#receiver(post_save, sender=ProjectTopic)
def notify_project_topic_change(sender, **kwargs):
if 'instance' in kwargs:
instance = kwargs['instance']
if(
hasattr(instance, 'project_topic_tracker') and
instance.project_topic_tracker.changed()
):
changed_list = list(instance.project_topic_tracker.changed().keys())
if changed_list:
# Call your notification function
send_project_topic_notification()
Method 3
You can use a form ProjectTopicForm from whose clean() method you can access the changed data.
forms.py
class ProjectTopicForm(forms.ModelForm):
class Meta:
model = ProjectTopic
fields = '__all__'
def clean(self):
if self.changed_data:
send_project_topic_notification()
I have a similar issue here for which I have been looking for work arounds

Related

What's the best way to perform actions before a Model.save() using Django?

I'm using Django-Rest-Framework(ViewSet approach) on my project interacting with a React app. So, I'm not using Django admin nor Django forms.
My project's structure is:
View
Serializer
Model
What I need to do is to perform actions before models method calls:
Insert the request.user on a Model field.
Start a printer process after a Model.save()
.....
I have read a lot about django-way to do on Django.docs and there, the things seems to be showed for a Django-Admin like project, which is not my case.
By other hand, by reading the Stack's answers about in other topics, the way to do seems to be like: "It will work, but, It's not the right way to do that".
According to Django's documentation, the best way to perform that supposed to be by using a new file, called admin.py, where I would to register actions binding to a Model which could support save, delete, etc., but, it's not clear if this approach is to do that or only for provide a Django-Admin way to perform an action.
# app/models.py
from django.db import models
from django.contrib.auth.models import User
class Post(models.Model):
user = models.ForeignKey(User)
content = models.TextField()
class Comment(models.Model):
post = models.ForeignKey(Post)
user = models.ForeignKey(User)
content = models.TextField()
# app/admin.py
from app.models import Post, Comment
from django.contrib import admin
class CommentInline(admin.TabularInline):
model = Comment
fields = ('content',)
class PostAdmin(admin.ModelAdmin):
fields= ('content',)
inlines = [CommentInline]
def save_model(self, request, obj, form, change):
obj.user = request.user
obj.save()
def save_formset(self, request, form, formset, change):
if formset.model == Comment:
instances = formset.save(commit=False)
for instance in instances:
instance.user = request.user
instance.save()
else:
formset.save()
admin.site.register(Post, PostAdmin)
According to the answers I have heard, the best way would use something like that on Models:
class MyModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
return super(MyModelForm, self).__init__(*args, **kwargs)
def save(self, *args, **kwargs):
kwargs['commit']=False
obj = super(MyModelForm, self).save(*args, **kwargs)
if self.request:
obj.user = self.request.user
obj.save()
return obj
What I want to know is:
What's the best way to to perform that actions, on which files, what's the best structure.
to insert a request.user on a Model field you can use the perform_create() method on your view class. for more information visit https://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/#associating-snippets-with-users which is exactly what u want!
I'm not sure what you mean by start a printer process, but you usually can override save() method on your model class for doing a process after saving a model instace.
https://www.django-rest-framework.org/api-guide/serializers/#overriding-save-directly
The best way I found to insert the request.user on the model, as a "created_by" field, was by inserting a hidden field on the model serializer with a default data, just like these:
my_field = serializers.HiddenField(default=serializers.CurrentUserDefault())
The CurrentUserDefault() is a function wich returns the user request onto serializer.
https://www.django-rest-framework.org/api-guide/validators/#advanced-field-defaults
For actions performing after/before a save/delete, what I chose to use Django Signals,wich works as a dispatcher of actions, a little like react's redux.
https://docs.djangoproject.com/en/2.2/topics/signals/
Thank you everybody for the helpful answers.

django save_model() error creating a new object with m2m fields

I have a model models.py:
class MyModelClass(models.Model):
name = models.CharField(max_length=255)
m2m_1 = models.ManyToManyField(A, blank=True, null=True)
m2m_2 = models.ManyToManyField(B, blank=True, null=True)
fk = models.ForeignKey('C')
int = models.IntegerField()
and an admin.py class:
class MyModelClassAdmin(admin.ModelAdmin):
list_display = ('name',)
#Get all fields selected in MyModelClass m2m_2
def get_selected_in_m2m_2(self, obj):
sel = obj.m2m_2.all() #This line is the one for i get an error. The error is described below.
return sel
def save_model(self, request, obj, form, change):
"""When creating a new object, set the creator field.
"""
m2m_2_selected = self.get_selected_in_m2m_2(obj)
print m2m_2_selected
print request.user
if not change:
obj.creator = request.user
obj.save()
The problem:
Everything works if i click "Save" button on already existing MyModelClass's object in my admin-page.
But if i try to create a new model object in admin-page and click the "Save" button(to save a new object, not to update existing one), i'll get an error:'MyModelClass' instance needs to have a primary key value before a many-to-many relationship can be used.
Sorry for my English.
Any help is appreciated.
Thanks.
def save_model(self, request, obj, form, change):
"""When creating a new object, set the creator field.
"""
if not change:
obj.creator = request.user
super(MyModelClassAdmin, self).save_model(request, obj, form, change)
m2m_2_selected = self.get_selected_in_m2m_2(obj)
print m2m_2_selected
print request.user
Very common problem. You haven't got object, so you can't set m2m relationship. Try to use super function (I'm sorry, can't remember proper usage of it, but you'll easly find it in docs) to create this object (well, process pure function before chages) and then modify it and save.

Add multiple records at once in django admin panel

I have following setup.
from django.db import models
from django.contrib.auth.models import User
class Event(models.Model):
name = models.CharField(max_length=64)
date = models.DateField()
ATTENDANCE_CHOICES = (
('A','Attending'),
('N','Absent'),
('L','Taken ill'),
)
class Attendance(models.Model):
student = models.ForeignKey(User)
event = models.ForeignKey(Event)
status = models.CharField(max_length=1, choices=ATTENDANCE_CHOICES)
In a nutshell: Students(User) attend or doesn't attend classes(Event), this is registered by Attendance.
Problem is adding those attendance records one at a time.
What I am looking for is a way to provide form for each class(each Event object) with list of all students and attendance status radio buttons or drop downs next to them.
Something like this:
http://i.imgur.com/jANIZ.png
I have looked at many discussions about multiple/bulk record insertion via django admin and am beginning to wonder is this even possible with django admin or do I have to create such form from scratch? Either way, what would be the best (most django-ish) approach?
"Is this even possible?" It's possible right out of the box.
Look into the django Admin app, Inlines, ModelForms, and the RadioSelect widget.
class MyForm(forms.ModelForm):
class Meta:
model = Attendance
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs
self.fields['status'].widget = forms.RadioSelect(choices=self.fields['status'].choices)
class AttendanceInline(admin.TabularInline):
model = Attendance
form = MyForm
class EventAdmin(admin.ModelAdmin):
inlines = [AttendanceInline]
def save_model(self, request, obj, form, change):
obj.save()
for user in User.objects.all():
obj.attendance_set.create(user=user, status='')
# you should consider a null field or a possible choice for "Undecided"
admin.site.register(Event, EventAdmin)

field added dynamically to a ModelForm at __init__ does not save

I'm using Django profiles and was inspired by James Bennett to create a dynamic form (http://www.b-list.org/weblog/2008/nov/09/dynamic-forms/ )
What I need is a company field that only shows up on my user profile form when the user_type is 'pro'.
Basically my model and form look like:
class UserProfile(models.Model):
user_type = models.CharField(...
company_name = models.CharField(...
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
exclude = ('company_name',)
And I add the company_name field in init like James Bennett showed:
def __init__(self, *args, **kwargs):
super(UserProfileForm, self).__init__(*args,**kwargs)
if (self.instance.pk is None) or (self.instance.user_type == 'pro'):
self.fields['company_name'] = forms.CharField(...
The problem is that, when I try to save() an instance of UserProfileForm, the field 'company_name' is not saved...
I have gone around this by calling the field explicitly in the save() method:
def save(self, commit=True):
upf = super(UserProfileForm, self).save(commit=False)
if 'company_name' in self.fields:
upf.company_name = self.cleaned_data['company_name']
if commit:
upf.save()
return upf
But I am not happy with this solution (what if there was more fields ? what with Django's beauty ? etc.). It kept me up at night trying to make the modelform aware of the new company_name field at init .
And that's the story of how I ended up on stackoverflow posting this...
I would remove this logic from form and move it to factory. If your logic is in factory, you can have two forms:
UserProfileForm
ProUserProfileForm
ProUserProfileForm inherits from UserProfileForm and changes only "exclude" constant.
You will have then following factory:
def user_profile_form_factory(*args, instance=None, **kwargs):
if (self.instance.pk is None) or (self.instance.user_type == 'pro'):
cls = ProUserProfileForm
else:
cls = UserProfileForm
return cls(*args, instance, **kwargs)
It seems I found a solution:
def AccountFormCreator(p_fields):
class AccountForm(forms.ModelForm):
class Meta:
model = User
fields = p_fields
widgets = {
'photo': ImageWidget()
}
return AccountForm
#...
AccountForm = AccountFormCreator( ('email', 'first_name', 'last_name', 'photo', 'region') )
if request.POST.get('acforms', False):
acform = AccountForm(request.POST, request.FILES, instance=request.u)
if acform.is_valid():
u = acform.save()
u.save()
ac_saved = True
else:
acform = AccountForm(instance = request.u)
When are you expecting the user_type property to be set? This seems like something that should be handled by javascript rather than trying to do funny things with the model form.
If you want the company_name field to appear on the client after they've designated themselves as a pro, then you can 'unhide' the field using javascript.
If instead, they've already been designated a pro user, then use another form that includes the company_name field. You can sub-class the original model form in the following manner.
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
exclude = ('company_name',)
class UserProfileProForm(UserProfileForm):
class Meta:
exclude = None # or maybe tuple() you should test it
Then in your view, you can decide which form to render:
def display_profile_view(request):
if user.get_profile().user_type == 'Pro':
display_form = UserProfileProForm()
else:
display_form = UserProfileForm()
return render_to_response('profile.html', {'form':display_form}, request_context=...)
This would be the preferred way to do it in my opinion. It doesn't rely on anything fancy. There is very little code duplication. It is clear, and expected.
Edit: (The below proposed solution does NOT work)
You could try changing the exclude of the meta class, and hope that it uses the instances version of exclude when trying to determine whether to include the field or not. Given an instance of a form:
def __init__(self, *args, **kwargs):
if self.instance.user_type == 'pro':
self._meta.exclude = None
Not sure if that will work or not. I believe that the _meta field is what is used after instantiation, but I haven't verified this. If it doesn't work, try reversing the situation.
def __init__(self, *args, **kwargs):
if self.instance.user_type != 'pro':
self._meta.exclude = ('company_name',)
And remove the exclude fields altogether in the model form declaration. The reason I mention this alternative, is because it looks like the meta class (python sense of Meta Class) will exclude the field even before the __init__ function is called. But if you declare the field to be excluded afterwards, it will exist but not be rendered.. maybe. I'm not 100% with my python Meta Class knowledge. Best of luck.
What about removing exclude = ('company_name',) from Meta class? I'd think that it is the reason why save() doesn't save company_name field

Working with extra fields in an Inline form - save_model, save_formset, can't make sense of the difference

Suppose I am in the usual situation where there're extra fields in the many2many relationship:
class Person(models.Model):
name = models.CharField(max_length=128)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
class Membership(models.Model):
person = models.ForeignKey(Person)
group = models.ForeignKey(Group)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
# other models which are unrelated to the ones above..
class Trip(models.Model):
placeVisited = models.ForeignKey(Place)
visitor = models.ForeignKey(Person)
pleasuretrip = models.Boolean()
class Place(models.Model):
name = models.CharField(max_length=128)
I want to add some extra fields in the Membership form that gets displayed through the Inline. These fields basically are a shortcut to the instantiation of another model (Trip). Trip can have its own admin views, but these shortcuts are needed because when my project partners are entering 'Membership' data in the system they happen to have also the 'Trip' information handy (and also because some of the info in Membership can just be copied over to Trip etc. etc.).
So all I want to have is two extra fields in the Membership Inline - placeVisited and pleasuretrip - which together with the Person instance will let me instantiate the Trip model in the background...
I found out I can easily add extra fields to the inline view by defining my own form. But once the data have been entered, how and when to reference to them in order to perform the save operations I need to do?
class MyForm(forms.ModelForm):
place = forms.ModelChoiceField(required=False, queryset=Place.objects.all(), label="place",)
pleasuretrip = forms.BooleanField(required=False, label="...")
class MembershipInline(admin.TabularInline):
model = Membership
form = MyForm
def save_model(self, request, obj, form, change):
place = form.place
pleasuretrip = form.pleasuretrip
person = form.person
....
# now I can create Trip instances with those data
....
obj.save()
class GroupAdmin(admin.ModelAdmin):
model = Group
....
inlines = (MembershipInline,)
This doesn't seem to work... I'm also a bit puzzled by the save_formset method... maybe is that the one I should be using? Many thanks in advance for the help!!!!
As syn points out in his answer, for TabularInline and StackedInline, you have to override the save_formset method inside the ModelAdmin that contains the inlines.
GroupAdmin(admin.ModelAdmin):
model = Group
....
inlines = (MembershipInline,)
def save_formset(self, request, form, formset, change):
instances = formset.save(commit=False)
for instance in instances:
if isinstance(instance, Member): #Check if it is the correct type of inline
if(not instance.author):
instance.author = request.user
else:
instance.modified_by = request.user
instance.save()
I just hit this same problem, and it seems the solution is that for both save_formset and save_model with inlines, they are not called. Instead you must implement it in the formset which is being called which is the parent e.g.
model = Group
....
inlines = (MembershipInline,)
def save_formset(self, request, form, formset, change):
instances = formset.save(commit=False)
for instance in instances:
# Here an instance is a MembershipInline formset NOT a group...
instance.someunsetfield = something
# I think you are creating new objects, so you could do it here.
instance.save()