django inline admin model and generic relations
I've got an inline admin model with a generic foreignkey and I want to validate a set of it's properties in the models clean() method. If I add a new model neither content_type nor object_id is set in the clean() method, but if I try to change an existing model of course I can access the content_type property.
Is there any work-around to get the content_type when I first add a new model?
Any hint or url is appreciated Thanks && have a nice day! :)
Bast开发者_JAVA百科i
If I understand you, something like the following should work:
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
def clean(self):
content_type = self.cleaned_data.get('content_type')
object_id = self.cleaned_data.get('object_id')
if content_type and object_id:
obj = content_type.get_object_for_this_type(pk=object_id)
# Check whatever on the object here, and if there's a problem:
# raise forms.ValidationError('Something is not right')
return self.cleaned_data
精彩评论