Thursday, September 9, 2010

Re: Overridden Model __init__ being called twice

The first request is the actual request and its stack trace shows the work being kicked off by the request handler.  The second request stack trace is actually coming from a the use of the inlines functionality in the OrderOpportunityAdmin class.

I know I could do some stack inspection using the inspect module, but in the past using inspect has gotten me into trouble so I try to avoid it.  Anyone have any thoughts on how I can have my __init__ tell the difference between the two times it is loaded besides using inspect?  Also, why doesn't the formset formation use the already instantiated model object?

Brian

On Thu, Sep 9, 2010 at 3:42 PM, akaariai <akaariai@gmail.com> wrote:
On 9 syys, 20:06, bmbouter <bmbou...@gmail.com> wrote:
> I have a model defined named OrderOpportunity where I have overridden
> __init__(self).  I have included the relevant parts of the Model below
> as an example.  What I want to understand is why the overridden
> __init__ is being called twice for the creation of a single
> OrderOpportunity DB record.
>
> In models.py:
> class OrderOpportunity(models.Model):
>     def __init__(self):
>         models.Model.__init__(self)
>         print 'foo'
>
> In admin.py:
> class OrderOpportunityAdmin(admin.ModelAdmin):
>     inlines = (ProductForSaleAdmin,)
>
> I enter the admin interface and go to add an OrderOpportunity.  Prior
> to saving the OrderOpportunity instance, I see the following output
> from python manage.py runserver:
>
> Development server is running athttp://127.0.0.1:8080/
> Quit the server with CONTROL-C.
> foo
> foo
> [09/Sep/2010 09:33:50] "GET /admin/bulk_buying_club/orderopportunity/
> add/ HTTP/1.1" 200 9164
> [09/Sep/2010 09:33:50] "GET /admin/jsi18n/ HTTP/1.1" 200 1761
>
> I see foo printed twice which seems strange to me.  Can someone
> provide some insight into two questions I have.  1)  Why is __init__
> being called twice?  2)  Why is jsi18n is also being called when I
> only load the page once?

I can answer the jsi18n part: This is to do with loading translations
for the javascript used in Admin. That is, this is loaded because
there is <script type="text/javascript" src="/admin/jsi18n/..." /> in
the loaded page somewhere.

Why the model is initialized twice? Try: import pdb; pdb.set_trace()
in the __init__ and you will see why that happens.

 - Anssi

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




--
Brian Bouterse
ITng Services

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

No comments:

Post a Comment