Missing attribute on User

1 view
Skip to first unread message

Hanne Moa

unread,
Nov 22, 2009, 12:59:20 PM11/22/09
to django...@googlegroups.com
I have models M, N, which both have a foreignkey to User. When in the
shell/runserver, User has attributes for both M and N, but in a
batchfile ($ DJANGO_SETTINGS_MODULE=settings python batch.py) only M
is reachable from User while trying to get N leads to

AttributeError: 'User' object has no attribute 'N_set'

Wherever and how do I start debugging something like this?


HM

Russell Keith-Magee

unread,
Nov 22, 2009, 6:46:17 PM11/22/09
to django...@googlegroups.com
If model X has a foreign key pointing to model Y, model Y won't get
the X_set attribute until model X is actually imported. This means
that if X isn't imported, attributes will appear to be missing from Y.

When you run under runserver, the INSTALLED_APPS setting is
effectively an implied import of all the models listed. However,
provided you have the right tables in your database, you can sometimes
successfully use an app (i.e., import it and use the models) without
the app being listed in INSTALLED_APPS.

As a result, the behaviour you are seeing could be caused if:
* You aren't importing the N model in your standalone script (either
due to an import error of some kind, or you simply aren't doing the
import), or
* The code that runserver is executing imports model N, but the app
that defines N isn't listed in INSTALLED APPS

So - this will require some forensic work on your part, but I suspect
you need to look very closely at imports and import failures.

Yours,
Russ Magee %-)

Hanne Moa

unread,
Nov 23, 2009, 3:34:48 AM11/23/09
to django...@googlegroups.com
On Mon, Nov 23, 2009 at 00:46, Russell Keith-Magee
<freakb...@gmail.com> wrote:
> On Mon, Nov 23, 2009 at 1:59 AM, Hanne Moa <hann...@gmail.com> wrote:
>> I have models M, N, which both have a foreignkey to User. When in the
>> shell/runserver, User has attributes for both M and N, but in a
>> batchfile ($ DJANGO_SETTINGS_MODULE=settings python batch.py) only M
>> is reachable from User while trying to get N leads to
>>
>> AttributeError: 'User' object has no attribute 'N_set'
>>
>> Wherever and how do I start debugging something like this?
>
> If model X has a foreign key pointing to model Y, model Y won't get
> the X_set attribute until model X is actually imported. This means
> that if X isn't imported, attributes will appear to be missing from Y.
>
> When you run under runserver, the INSTALLED_APPS setting is
> effectively an implied import of all the models listed.

So, settings isn't actually run properly (and thus the apps in
INSTALLED_APPS aren't all imported) when doing

$ DJANGO_SETTINGS_MODULE=settings python script.py

?

So how is one supposed to do such things without going trough manage.py then?


HM

Daniel Roseman

unread,
Nov 23, 2009, 4:44:28 AM11/23/09
to Django users
On Nov 23, 8:34 am, Hanne Moa <hanne....@gmail.com> wrote:
> On Mon, Nov 23, 2009 at 00:46, Russell Keith-Magee
>
>
>
>
>
> <freakboy3...@gmail.com> wrote:
> > On Mon, Nov 23, 2009 at 1:59 AM, Hanne Moa <hanne....@gmail.com> wrote:
> >> I have models M, N, which both have a foreignkey to User. When in the
> >> shell/runserver, User has attributes for both M and N, but in a
> >> batchfile ($ DJANGO_SETTINGS_MODULE=settings python batch.py) only M
> >> is reachable from User while trying to get N leads to
>
> >> AttributeError: 'User' object has no attribute 'N_set'
>
> >> Wherever and how do I start debugging something like this?
>
> > If model X has a foreign key pointing to model Y, model Y won't get
> > the X_set attribute until model X is actually imported. This means
> > that if X isn't imported, attributes will appear to be missing from Y.
>
> > When you run under runserver, the INSTALLED_APPS setting is
> > effectively an implied import of all the models listed.
>
> So, settings isn't actually run properly (and thus the apps in
> INSTALLED_APPS aren't all imported)  when doing
>
> $ DJANGO_SETTINGS_MODULE=settings python script.py
>
> ?
>
> So how is one supposed to do such things without going trough manage.py then?
>
> HM

*Do* go through manage.py. Write your script as a custom manage.py
command, which will take care of all this:
http://docs.djangoproject.com/en/dev/howto/custom-management-commands/
--
DR.
Reply all
Reply to author
Forward
0 new messages