Poorna Shashank
2018-11-15 07:36:42 UTC
This has been very old and I had to search for it for a long time. Posting
in case some one needs the answer to the same.
So, the way to do this would be to go via the ModelForm route where the
querysets for each fields for each instance can be set separately
from django import forms
class MyModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
instance = kwargs['instance']
self.fields['my_field'].queryset = MyFieldModel.objects.filter(...)
@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
list_display = ['foo_field', 'bar_field', 'my_field']
list_editable = ('my_field',)
def get_changelist_form(self, request, **kwargs):
return MyModelForm
in case some one needs the answer to the same.
So, the way to do this would be to go via the ModelForm route where the
querysets for each fields for each instance can be set separately
from django import forms
class MyModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
instance = kwargs['instance']
self.fields['my_field'].queryset = MyFieldModel.objects.filter(...)
@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
list_display = ['foo_field', 'bar_field', 'my_field']
list_editable = ('my_field',)
def get_changelist_form(self, request, **kwargs):
return MyModelForm
Hello,
I'm using list_editable of a ModelAdmin to allow users change a
choicefield right in the changelist view.
I've a list
*Field1 Field2 ChoiceField*
abc def foo
ghj klm bar
The idea is that ChoiceField is filled according to the *field1 *and *field2
*values, so the values filled in "foo" choicefield can be different than
in "bar" choicefield.
But to filter this field this way I need to know some field values of the
instance I'm pointing.
kwargs['queryset'] = MyNewFiltter
MyNewFilter is a filter based on fields values of the instance itself
But it seems that the formfield_for_foreignkey is not created for a
specific instance, so I've no access to instance fields, is there some way
to do what I'm thinking about in Admin? If you've any other suggestions to
do that please let me know.
Best Regards,
--
Daniel França,
about.me/danielfranca
I'm using list_editable of a ModelAdmin to allow users change a
choicefield right in the changelist view.
I've a list
*Field1 Field2 ChoiceField*
abc def foo
ghj klm bar
The idea is that ChoiceField is filled according to the *field1 *and *field2
*values, so the values filled in "foo" choicefield can be different than
in "bar" choicefield.
But to filter this field this way I need to know some field values of the
instance I'm pointing.
kwargs['queryset'] = MyNewFiltter
MyNewFilter is a filter based on fields values of the instance itself
But it seems that the formfield_for_foreignkey is not created for a
specific instance, so I've no access to instance fields, is there some way
to do what I'm thinking about in Admin? If you've any other suggestions to
do that please let me know.
Best Regards,
--
Daniel França,
about.me/danielfranca
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fffd08a5-1054-4ca1-8be0-e4ed2ef3e132%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fffd08a5-1054-4ca1-8be0-e4ed2ef3e132%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.