Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 5, 2010 19:26:19

Daniel C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

doing validation in admin form


hi

I have this models:


models.py:

class Galeria(models.Model):
...
class Foto(models.Model):
...
galeria = models.ForeignKey(Galeria, null=False)


admin.py:

class FotoInline(admin.StackedInline):
model = Foto
class GaleriaAdmin(admin.ModelAdmin):
inlines =
admin.site.register(Galeria, GaleriaAdmin)



When a galeria is saved in the django administration, I want to perform
a validation that involves his fotos. ..

They are edited at the same time because I use inline in the admin.


But cannot do that in Galeria.save, nor GaleriaAdmin.save_model, nor in
a postsave event from galeria, because at the time it is called the
fotos have not been saved yet.

Where should I do it the validation?
Have to define a GaleriaForm and do something there?signature.ascDescription:OpenPGP digital signature

Attachments:
attachment signature.asc (554 bytes)

Offline

#2 Dec. 6, 2010 03:05:26

Wayne S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

doing validation in admin form


It's hard to say, because I don't know what kind of "validation" or business
logic regarding it you are trying to accomplish/employ.

However, if you are wanting to ensure you validate a foto during a gallery
save, why not call an overridden fotos.save() from your custom
galeria.save()?

On Sun, Dec 5, 2010 at 1:25 PM, Daniel Carvalho <idn...@gmail.com> wrote:

> hi
>
> I have this models:
>
>
> models.py:
>
> class Galeria(models.Model):
> ...
> class Foto(models.Model):
> ...
> galeria = models.ForeignKey(Galeria, null=False)
>
>
> admin.py:
>
> class FotoInline(admin.StackedInline):
> model = Foto
> class GaleriaAdmin(admin.ModelAdmin):
> inlines =
> admin.site.register(Galeria, GaleriaAdmin)
>
>
>
> When a galeria is saved in the django administration, I want to perform
> a validation that involves his fotos. ..
>
> They are edited at the same time because I use inline in the admin.
>
>
> But cannot do that in Galeria.save, nor GaleriaAdmin.save_model, nor in
> a postsave event from galeria, because at the time it is called the
> fotos have not been saved yet.
>
> Where should I do it the validation?
> Have to define a GaleriaForm and do something there?
>
>

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

Offline

#3 Dec. 6, 2010 11:19:08

Daniel C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

doing validation in admin form


On 12/06/2010 03:05 AM, Wayne Smith wrote:
> It's hard to say, because I don't know what kind of "validation" or business
> logic regarding it you are trying to accomplish/employ.
>


suppose I want a validation like this:

the number of fotos in a galeria should be limited to a number, which
depends on the user that created it (OR, depends on some values that are
in the galeria object)



> However, if you are wanting to ensure you validate a foto during a gallery
> save, why not call an overridden fotos.save() from your custom
> galeria.save()?
>signature.ascDescription:OpenPGP digital signature

Attachments:
attachment signature.asc (554 bytes)

Offline

#4 Dec. 6, 2010 16:18:21

Wayne S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

doing validation in admin form


Then yes, in the clean() method of your Galeria form, you could check the
number of fotos present and then raise a ValidationError if it is too many,
along with a custom message for the user.

Is that what you wish to do?

On Mon, Dec 6, 2010 at 5:18 AM, Daniel Carvalho <idn...@gmail.com> wrote:

> On 12/06/2010 03:05 AM, Wayne Smith wrote:
> > It's hard to say, because I don't know what kind of "validation" or
> business
> > logic regarding it you are trying to accomplish/employ.
> >
>
>
> suppose I want a validation like this:
>
> the number of fotos in a galeria should be limited to a number, which
> depends on the user that created it (OR, depends on some values that are
> in the galeria object)
>
>
>
> > However, if you are wanting to ensure you validate a foto during a
> gallery
> > save, why not call an overridden fotos.save() from your custom
> > galeria.save()?
> >
>
>

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

Offline

#5 Dec. 7, 2010 22:46:17

Daniel C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

doing validation in admin form


On 12/06/2010 04:18 PM, Wayne Smith wrote:
> Then yes, in the clean() method of your Galeria form, you could check the
> number of fotos present and then raise a ValidationError if it is too many,
> along with a custom message for the user.
>
> Is that what you wish to do?
>

hi
I think the "clean" method wont solve the problem because at the time it
is called, the inlines from galeria have not been saved yet...


This is what happen when I submit a form with a galeria and two
fotografias. the methods are called in this order:

1. Galeria.clean - don't know the fotografias yet!

2. GaleriaAdmin.save_model (before calling super.save_model)

3. Galeria.save ( before calling super.save)
4. galeria presave signal
5. galeria presave signal
6. galeria postsave signal
7. galeria postsave signal
8. Galeria.save ( after calling super.save)

9. GaleriaAdmin.save_model (after calling super.save_model)

10. Fotografia.save - first foto - only now the fotos are saved...
11. Fotografia.save - second foto


So it seems if someone wants to execute some code after all the form has
been processed, he has to do that somewhere else.

Anyway if I want to perform a validation there, it is to late.

Maybe the only solution would be to do that in the
GaleriaAdmin.save_model, and have to see the request parameters to know
how many new files have been uploaded...signature.ascDescription:OpenPGP digital signature

Attachments:
attachment signature.asc (554 bytes)

Offline

Board footer

Moderator control

Enjoy the 12th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support