Problem when trying to validate a field in a ModelAdmin which has inline forms

43 views
Skip to first unread message

Gabriel Reis

unread,
Jan 14, 2010, 6:45:50 PM1/14/10
to django...@googlegroups.com
Hi people,

I am having some problem to validate a data in my application. I will try to simply the model in here to help you guys to understand the problem.
I have 2 models:

class Clip(models.Model):
    is_approved = models.BooleanField(default=False)
    language = models.ForeignKey(Language)

class ClipDescription(models.Model):
    clip = models.ForeignKey(Clip)
    text = models.TextField()
    language = models.ForeignKey(Language)

I am editing via ModelAdmin. I defined a ClipModelAdmin class as above (simplified):

class ClipAdmin(admin.ModelAdmin):
    inlines = [
        ClipDescriptionInline
    ]


So, as you can see, both Clip and ClipDescription are edited in the same page (using the 'inlines' feature).

But the rule is: if the user trigger the 'Save' action, the attribute Clip.is_approved can be True only if there is a ClipDescription instance associated to the Clip instance, having the same language. For example, if I have a clip with id=1, language=english and is_approved=True, it can be saved only if there is a clip description with clip_id=1, language=english. If not, I want to show the error message 'Cannot approve this clip without having a description in English' in the form.

I have already read the official documentation and tried to work with validators, tried to define a ModelForm and its clean_is_approved method, among other workarounds. And I still couldn't make this work. The problem is at the clean_is_approved context I couldn't figure out how to get access to the form data that is being entered at that moment, to retrieve the Clip descriptions.

I don't if I was clear enough, I can give more details. Any ideas and suggestions will be very appreciated.

Thank you very much for your attention.

Kind regards,

Gabriel


Gabriel de Carvalho Nogueira Reis
Software Developer
+44 7907 823942

Marco Rogers

unread,
Jan 15, 2010, 11:45:33 AM1/15/10
to Django users
I've never had to do this but it sounds you want to work with
Form.clean() in stead of Form.clean_is_approved(). From the docs:

"The Form subclass’s clean() method. This method can perform any
validation that requires access to multiple fields from the form at
once."

So in your clean method you would grab the submitted inlines and check
them against the is_approved field on the Clip. I'm not a django
expert, but that's where I would start. Good luck.

:Marco

Gabriel Reis

unread,
Jan 15, 2010, 1:10:42 PM1/15/10
to django...@googlegroups.com
Hey Marcos,

In the clean() method of my ClipModelForm class I couldn't manage how to reach the ClipDescriptions that are coming from the inline ClipDescriptionInline. I think I can't validate there. I spent the whole day trying and I couldn't fix. Any help would be very handy!

Thanks for your atention.


Kind regards,

Gabriel

Gabriel de Carvalho Nogueira Reis
Software Developer
+44 7907 823942


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




mrts

unread,
Feb 4, 2010, 5:48:24 PM2/4/10
to Django users
See http://code.djangoproject.com/ticket/12780 .

I will upload a patch and provide an example,
but I can't yet say when.

Best,
Mart Sõmermaa

On Jan 15, 8:10 pm, Gabriel Reis <gabriel...@gmail.com> wrote:
> Hey Marcos,
>
> In the clean() method of my ClipModelForm class I couldn't manage how to
> reach the ClipDescriptions that are coming from the inline
> ClipDescriptionInline. I think I can't validate there. I spent the whole day
> trying and I couldn't fix. Any help would be very handy!
>
> Thanks for your atention.
>
> Kind regards,
>
> Gabriel
>
> Gabriel de Carvalho Nogueira Reis
> Software Developer
> +44 7907 823942
>

> > django-users...@googlegroups.com<django-users%2Bunsu...@googlegroups.com>

mrts

unread,
Feb 5, 2010, 7:11:23 PM2/5/10
to Django users
Patch has been uploaded to http://code.djangoproject.com/ticket/12780
.

After applying the patch you can override

def formsets_are_valid(self, formsets, form, form_is_valid,
instance,
request):

and do any complex validation you like. As it's rather arcane, I've
provided
an example based on your description at http://gist.github.com/296411
.

Hope that helps,

Reply all
Reply to author
Forward
0 new messages