How do I customize the spyglass query in an inline form for a raw_id foreignkey?
I tried overriding formfield_for_foreignkey but that did nothing and I think it's because it's for dropdown foreignkeys rather than raw_id. I also tried a custom widget but that doesn't seem to work on inline.
So after a lot of digging around, this is what I came up with.
from django.admin import widgets
class ItemSubRecipeRawIdWidget(widgets.ForeignKeyRawIdWidget):
def url_parameters(self):
res = super(ItemSubRecipeRawIdWidget, self).url_parameters()
# DO YOUR CUSTOM FILTERING HERE!
res['active'] = True # here I filter on recipe.active==True
return res
class ItemSubRecipeInline(admin.TabularInline):
def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
field = super(ItemSubRecipeInline, self).formfield_for_foreignkey(db_field, request, **kwargs)
if db_field.name == 'recipe':
field.widget = ItemSubRecipeRawIdWidget(rel=ItemSubRecipe._meta.get_field('recipe').rel, admin_site=site)
return field
So the spyglass thing is a ForeignKeyRawIdWidget and you need to override the default with a custom one. The url_parameters function on the widget is what is passed to build the query that populates the list of usable object foreignkeys.
Is enought to do following:
class ItemSubRecipeInline(admin.TabularInline):
def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
field = super(ItemSubRecipeInline, self).formfield_for_foreignkey(db_field, request, **kwargs)
if db_field.name == 'recipe':
field.widget.rel.limit_choices_to = {'your_field_to_filter': True}
return field
Related
I am using Django 2.2
I am creating a form dynamically, by reading a JSON definition file; the configuration file specifies the types of widgets, permitted values etc.
I have come a bit unstuck with the Select widget however, because I prepend a '--' to the list of permitted values in the list (so that I will know when a user has not selected an item).
This is the code snippet where the Select widget is created:
class myForm(forms.Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# ...
elif widget_type_id == WIDGET_TYPE_DROPDOWN:
CHOICES.insert(0,('-', INVALID_SELECTION))
form_field = CharField(label=the_label, widget=Select(choices=CHOICES),required=is_required)
My problem is that when the is_valid() method is invoked on my form, any rendered Select widgets are are accepted as valid regardless of the selection.
I want to implement code that has this logic (pseudocode below):
def is_valid(self):
for field_name, field in self.fields.items():
if isinstance(field.type, Select):
if field.required and field.selected_value == INVALID_SELECTION:
return False
return super().is_valid()
What would be the correct way to implement this functionality? For instance, how would I even get the selected values for the field (in the form code)?
Validate a form data: docs
by creating a custom Field with validation
a specific field by using fieldname_clean()
by overriding clean()
by using validators
? What would be the correct way to implement this functionality?
I've deviated a bit from the pseudocode in what you've asked
Validators docs
There are already many buitin validators. For this purpose, create a custom validator which checks the value in the field and raise ValidationError.
Create validator.py in your app
# validator.py
from django.core.exceptions import ValidationError
def validate_select_option(value):
if value == '-':
raise ValidationError('Invalid selection')
In forms.py import validate_select_option and add validator to the field
# forms.py
from .validator import validate_select_option
# other parts of code
form_field = CharField(label=the_label, widget=Select(choices=CHOICES), required=is_required, validator=[validate_select_option])
This validator validate_select_option can now be used not only to the field which has choices but also to any other field to validate. Anyhow, that's not its intend. So can be used to any fields with choices :)
Since you said that you are creating the form dynamically and I'm assuming that you can add additional options to Field using JSON definition file. For fieldname_clean() and clean() you will need to add these methods in your Form class. Custom field can be created and imported into. But, I think simple validators can do this easily.
? how would I even get the selected values for the field (in the form code)
If Form class method clean(self, *args, **kwargs) and fieldname_clean(self, *args, **kwargs) are used : you can access the form data by self.cleaned_data dictionary. cleaned_data is created only after is_valid().
While overriding is_valid , in-order to access the form data, we need to call the parent validation first and then work on it.
def is_valid(self, *args, **kwargs):
# cannot access self.cleaned_data since it is not created yet
valid = super(myForm, self).is_valid()
# can access self.cleaned_data since it has been created when parent is_valid() has been called
for fieldname, field in self.fields.items():
if isinstance(field, forms.CharField): # the type of widget is not considered.
if fieldname in self.cleaned_data and field.required and self.cleaned_data[fieldname] == '-':
valid = False
return valid
It's kind of hidden because you're using a CharField with a Select widget but if you look at the ChoiceField documentation it says that the empty value should be an empty string.
Assuming the form field is required=True, you should just be able to change your empty value tuple to ('', INVALID_SELECTION).
CHOICES having already values. I insert ('INVALID_SELECTION', 'INVALID_SELECTION') this. And check if the value of form_field field is INVALID_SELECTION then add error in same field. Else form is submited.
views.py
class DynamicFormView(View):
template_name = 'test.html'
def get(self, request, *args, **kwargs):
form = myForm( request.POST or None)
context = {
'form' : form,
}
return render(request, self.template_name, context)
def post(self, request,id = None, *args, **kwargs):
context = {}
form = myForm(request.POST or None,)
if form.is_valid():
if request.POST['form_field'] == 'INVALID_SELECTION':
form.add_error("form_field",_("This field is required."))
else:
form.save()
context = {
'form' : form,
}
return render(request, self.template_name, context)
forms.py
class myForm(forms.Form):
CHOICES = [
('FR', 'Freshman'),
('SO', 'Sophomore'),
('JR', 'Junior'),
('SR', 'Senior'),
('GR', 'Graduate'),
]
CHOICES.insert(0,('INVALID_SELECTION', 'INVALID_SELECTION'))
form_field = forms.ChoiceField(label='the_label',choices=CHOICES,widget=forms.Select(attrs={'class':' form-control'}),required = False)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def save(self):
# form is success
pass
class Meta:
model = Student
urls.py
path('dyanmic-form/create/', views.DynamicFormView.as_view(), name='dyanamic_form_create'),
Sorry my bad english language.
I am attempting to limit the option to a foreign key in the admin app for a specific user (The field that i am trying to limit is called school) . This is what my code looks like - Unfortunately there are two problems (mentioned below) when I attempt to edit a student (by clicking on their name).
1.The default value for school is --
2.When I select the right school from the drop down and attempt to save I get the error on school field saying
Select a valid choice. That choice is not one of the available
choices.
This is what it looks like
class modelStudentAdmin(admin.ModelAdmin):
def get_queryset(self, request):
qs = super(modelStudentAdmin, self).get_queryset(request)
if request.user.is_superuser:
return qs
else:
schoolInstance = modelSchool.objects.get(user=request.user)
qs = modelStudent.objects.filter(school=schoolInstance)
return qs
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if request.user.is_superuser:
return super(modelStudentAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
#Not superuser only staff
if db_field.name == 'school':
t = modelSchool.objects.filter(user=request.user).values_list("school_name",flat=True)
kwargs['queryset'] = t
return super(modelStudentAdmin,self).formfield_for_foreignkey(db_field, request, **kwargs)
Now if I remove the method
def formfield_for_foreignkey(self, db_field, request, **kwargs):
everything works but then I cannot restrict the foreign key. Any suggestions on what I might be doing wrong ?
Try replacing
t = modelSchool.objects.filter(user=request.user).values_list("school_name",flat=True)
with this
modelSchool.objects.filter(user=request.user)
you dont need to value_list your query set.
I've just created a forms.models.BaseInlineFormSet to override the default formset for a TabularInline model. I need to evaluate the user's group in formset validation (clean) because some groups must write a number inside a range (0,20).
I'm using django admin to autogenerate the interface.
I've tried getting the request and the user from the kwargs in the init method, but I couldn't get the reference.
This is what I have now:
class OrderInlineFormset(forms.models.BaseInlineFormSet):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super(OrderInlineFormset, self).__init__(*args, **kwargs)
def clean(self):
# get forms that actually have valid data
count = 0
for form in self.forms:
try:
if form.cleaned_data:
count += 1
if self.user.groups.filter(name='Seller').count() == 1:
if form.cleaned_data['discount'] > 20:
raise forms.ValidationError('Not authorized to specify a discount greater than 20%')
except AttributeError:
# annoyingly, if a subform is invalid Django explicity raises
# an AttributeError for cleaned_data
pass
if count < 1:
raise forms.ValidationError('You need to specify at least one item')
class OrderItemInline(admin.TabularInline):
model = OrderItem
formset = OrderInlineFormset
Then I use it as inlines = [OrderItemInline,] in my ModelAdmin.
Unfortunatly self.user is always None so I cannot compare the user group and the filter is not applied. I need to filter it because other groups should be able to specify any discount percent.
How can I do? If you also need the ModelAdmin code I'll publish it (I just avoided to copy the whole code to avoid confusions).
Well, I recognise my code there in your question, so I guess I'd better try and answer it. But I would say first of all that that snippet is really only for validating a minimum number of forms within the formset. Your use case is different - you want to check something within each form. That should be done with validation at the level of the form, not the formset.
That said, the trouble is not actually with the code you've posted, but with the fact that that's only part of it. Obviously, if you want to get the user from the kwargs when the form or formset is initialized, you need to ensure that the user is actually passed into that initialization - which it isn't, by default.
Unfortunately, Django's admin doesn't really give you a proper hook to intercept the initialization itself. But you can cheat by overriding the get_form function and using functools.partial to wrap the form class with the request argument (this code is reasonably untested, but should work):
from functools import partial
class OrderForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super(OrderForm, self).__init__(*args, **kwargs)
def clean(self)
if self.user.groups.filter(name='Seller').count() == 1:
if self.cleaned_data['discount'] > 20:
raise forms.ValidationError('Not authorized to specify a discount greater than 20%')
return self.cleaned_data
class MyAdmin(admin.ModelAdmin):
form = OrderForm
def get_form(self, request, obj=None, **kwargs):
form_class = super(MyAdmin, self).get_form(request, obj, **kwargs)
return functools.partial(form_class, user=request.user)
Here's another option without using partials. First override the get_formset method in your TabularInline class.
Assign request.user or what ever extra varaibles you need to be available in the formset as in example below:
class OrderItemInline(admin.TabularInline):
model = OrderItem
formset = OrderInlineFormset
def get_formset(self, request, obj=None, **kwargs):
formset = super(OrderProductsInline, self).get_formset(request, obj, **kwargs)
formset.user = request.user
return formset
Now the user is available in the formset as self.user
class OrderInlineFormset(forms.models.BaseInlineFormSet):
def clean(self):
print(self.user) # is available here
I need to change the widget used in the admin, based on the value of the db_field. Here's where I'm trying to step in:
def formfield_for_dbfield(self,db_field,**kwargs):
field = super(MyAdmin, self).formfield_for_dbfield(db_field, **kwargs)
if db_field.name == "my_custom_name":
# how can I check here the value of the object?
I've been trying various combinations in the shell for the past 10 minutes, to no result.
Ok, so here's how I finally did it:
class MyAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
self.object_instance = obj
return super(MyAdmin,self).get_form(request,obj,**kwargs)
After that, everything was easy.
I'm trying to limit the choices of a FK field found in a generic inline, depending on what the inline is attached to.
For example I have Article, with a Generic relation Publishing, edited inline with the Article.
I'd like for the PublishingInline to 'know', somehow, that it is currently being edited inline to an Article, and limit the available PublishingTypes to content_type Article.
This is the start that I've made:
class PublishingInlineForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
try:
data = kwargs.pop("data", {})
if kwargs["instance"]:
publishing_type_kwargs = {
'content_type': kwargs["instance"].content_type, }
data["publishing_type"] = PublishingType.objects.filter(**publishing_type_kwargs)
kwargs["data"] = data
except KeyError:
pass
super(PublishingInlineForm, self).__init__(*args, **kwargs)
class PublishingInline(generic.GenericStackedInline):
form = PublishingInlineForm
model = get_model('publishing', 'publishing')
extra = 0
If i understand you correctly formfield_for_foreignkey on your GenericInlineModelAdmin is your friend.
Something along these lines should do it:
def formfield_for_foreignkey(self, db_field, request, **kwargs):
print self.parent_model # should give you the model the inline is attached to
if db_field.name == "publishing_type":
kwargs["queryset"] = ... # here you can filter the selectable publishing types based on your parent model
return super(PublishingInline, self).formfield_for_foreignkey(db_field, request, **kwargs)