Re: [mezzanine-users] Re: Best way to add a custom field to forms app

508 views
Skip to first unread message

Stephen McDonald

unread,
Jul 2, 2012, 4:39:57 PM7/2/12
to mezzani...@googlegroups.com
I hadn't seen that captcha app before - very cool. The way it adds itself to the field types is probably the cleverest approach at the moment. There might be another way where you add the field (via the admin, as a DB record) as a hidden field, then do some tweaks in your form template to not display that field, and show a custom one instead. Neither of these approaches are great, but they should get you there until we have a proper API for adding new field types.

I can't seem to find the thread at the moment on github or this mailing list, but there was a brief discussion around refactoring the field types to have a registry that  fields could be added to, achieving what the captcha app mentioned does in a more structured way. So instead of all the constants that currently make up the field types, we'd have a register function that takes all the variables of a field type. It might even be a "Field" class that gets subclassed, and each time it's subclassed, the new class automatically gets added to the registry. But nothing has happened with this yet. If you'd like to work on something like that then go right ahead and we can merge it in. If not, I will at some point, but it's not a priority right now.

On Mon, Jul 2, 2012 at 11:43 PM, Markus Törnqvist <m...@nysv.org> wrote:
On Monday, 2 July 2012 12:42:42 UTC+3, freewill wrote:
 
I would like to add a custom field to available fields list of forms application.
 
What could be the best way to achieve it within my custom application without having to edit mezzanine/forms/fields.py?


I'm actually looking at the same thing. My idea would be to inspect mezzanine.forms and look
at one of the structures, why not NAMES. Figuring out the latest ID from there you should be able
to monkey-patch one extra into the module and work from there.

The downside of this is obviously that if you ever loaded two extra field thingies in the wrong order,
or remove one from the middle and restart, you're going to see pain.

I, too, would like to know if there's a better solution :>

Thanks!

--
mjt
 



--
Stephen McDonald
http://jupo.org

freewill

unread,
Jul 3, 2012, 3:04:06 AM7/3/12
to mezzani...@googlegroups.com
Hi Stephen,
 
a register function for adding custom fields seems the cleanest way. I understand that it's not a priority right now so if I'll get a little of time I'll try to work on it.
 
Thanks for your attention.
 
Regards,
Marco

freewill

unread,
Jul 3, 2012, 3:11:16 AM7/3/12
to mezzani...@googlegroups.com
Hi Markus,
 
thanks for your explanation and code.
 
Yours was also my approach and it works like a charm in development mode (manage.py runserver) however for some strange reason it doesn't work on production environment (apache, mod_wsgi). I did not yet debug but for now I can't think up what could be the cause of this different behaviour.
 
Regards,
Marco
 
 

On Monday, July 2, 2012 4:29:58 PM UTC+2, Markus Törnqvist wrote:
Hi!

I coded something up, you can look at this.

https://github.com/mjtorn/mezzanine-captcha

--
mjt

freewill

unread,
Jul 5, 2012, 2:44:50 AM7/5/12
to mezzani...@googlegroups.com
Hello again Markus,
 
I would like to ask you if you did try your code in a production enviroment with apache + mod_wsgi and if it really works for you.
 
I figured out that mezzanine.forms app loads before my custom app even if mezzanine.forms is listed after my custom app in INSTALLED_APPS. I even tried to put my custom app on top in INSTALLED_APPS but no luck.
 
Do you have any ideas about this problem?
 
Thanks in advance.

Markus Törnqvist

unread,
Jul 5, 2012, 2:57:18 AM7/5/12
to mezzani...@googlegroups.com
On Wed, Jul 04, 2012 at 11:44:50PM -0700, freewill wrote:
>Hello again Markus,

_o/

>I would like to ask you if you did try your code in a production enviroment
>with apache + mod_wsgi and if it really works for you.

Not that, but gunicorn+nginx.

>I figured out that mezzanine.forms app loads before my custom app even if
>mezzanine.forms is listed after my custom app in INSTALLED_APPS. I even
>tried to put my custom app on top in INSTALLED_APPS but no luck.

How did you figure?

Here's something from my settings.py in production:

INSTALLED_APPS = (
...
"django.contrib.sitemaps",
"django.contrib.staticfiles",
'captcha',
'mezzacaptcha',
"mezzanine.boot",
...
"mezzanine.forms",
...
)

--
mjt

freewill

unread,
Jul 5, 2012, 6:01:34 AM7/5/12
to mezzani...@googlegroups.com
I configured INSTALLED_APPS just like you: captcha and mezzacaptcha before any mezzanine app.
 
I figured out using django's logging (LOGGING setting) printing out modules loading time and mezzanine.forms.fields loads before mezzacaptcha.models. Loading's process and thread are exactly the same for every module so no asyncro logging problem here.
 
I'll do further investigations.
 
Thanks a lot for your considerations.
 
Regards,
Marco

HB

unread,
Jul 13, 2012, 2:37:54 AM7/13/12
to mezzani...@googlegroups.com
I'm having the same issue  - works in dev, but not in production using apache + mod_wsgi. 

Please let us know if you make any progress, Marco.

freewill

unread,
Jul 17, 2012, 10:50:13 AM7/17/12
to mezzani...@googlegroups.com

Hi,

I'm being too busy lately so no progresses, sorry.
It seems that mod_wsgi works slightly different from others wsgi servers so no news for now...

Regards,
Marco


Reply all
Reply to author
Forward
0 new messages