-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ManyToMany field is not updated correctly #202
Comments
Using Django 1.11.7 an approach, albeit not great one, for doing this is as follows; import algoliasearch_django
class PackageAdmin(admin.ModelAdmin):
class Meta:
model = Package
def response_add(self, request, obj: Package, post_url_continue=None):
# Ensure we update Algolia's index once all many-to-many fields have been updated.
algoliasearch_django.save_record(obj)
return super().response_add(request=request, obj=obj, post_url_continue=post_url_continue)
def response_change(self, request, obj: Package):
# Ensure we update Algolia's index once all many-to-many fields have been updated.
algoliasearch_django.save_record(obj)
return super().response_change(request=request, obj=obj) Note that you should also set You probably also want to pay special attention to Django's change log or have some good tests that ensure when you upgrade Django this continues to work. |
Thanks @alexhayes for your message ! I'll add our use case if it can help someone else. We have a One-To-Many relation and I want to have everything in a single object. We have this class Benefits linked to another class named Vendors. class Benefits(models.Model):
id_vendor = models.ForeignKey(
'Vendors', models.DO_NOTHING, related_name='benefits', db_column='id_vendor') When we update a Benefit object in its admin, we can update the associated Vendors object with this: def response_add(self, request, obj: Benefits, post_url_continue=None):
# Ensure we update Algolia's index once all many-to-many fields have been updated.
algoliasearch_django.save_record(obj.id_vendor)
return super().response_add(request=request, obj=obj, post_url_continue=post_url_continue)
def response_change(self, request, obj: Benefits):
# Ensure we update Algolia's index once all many-to-many fields have been updated.
algoliasearch_django.save_record(obj.id_vendor)
return super().response_change(request=request, obj=obj) |
It worked like a charm for me. Only thing I changed is the The Maybe this update should be planned in a milestone or next release? |
Hi there, sincere apologies for the slow reply. That's a nice solution to this issue, with the same concern as @alexhayes that as it relies on behavior from a I added this to the |
@alexhayes Can you elaborate on why this is necessary? Wouldn't that prevent the index being updated for normal object saves? |
@soulshake sorry, I can't remember why I suggested setting those values like that and I'm no longer working at that company. I've said when "registering the model" which makes me wonder if it should then be changed 🤔 |
From a customer's e-mail:
Context
Problem
Potential solution
The text was updated successfully, but these errors were encountered: