Skip to content

Instantly share code, notes, and snippets.

@mahdikhi
mahdikhi / django multiple formset add or remove
Last active Jun 14, 2020
make it possible dynamically add or remove multiple django formset instances
View django multiple formset add or remove
Hello everyone
I had tested solution for django dynamic formsets.
make it possible to add or remove formset instances.
# note: it doesn't matter that you're using djangos class based views or functional views
# note: I ignored the froms.py forms as it doesn't have effect on the procedure fo this solution.
imagine we have two models, model_A and model_B as follows:
class model_A(models.Model):
# some fields it has
@mahdikhi
mahdikhi / django filter choices for autocomplete_fields.txt
Last active Jun 14, 2020
django handel limit_choices_to not working when autocomplete_fields is used
View django filter choices for autocomplete_fields.txt
Hello everyone
To solve the problem, use "limit _choices _to" which does not work when using "autocomplete_fields".
A suggested solution is as follows:
imagine we have two models model_A and modle_B:
we are going to override the "get_search_results" of \
model-admin of model_A(because model_B has a foreign_key(or m2m) to it)
in my case I just want to limit choices to all model_A objects which \
currentlly dont have a model_B connected object(s)
or in case of updating an object of model_B limit to just the previous model_A object(s).
You can’t perform that action at this time.