Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » not able to recognize non-default python files in app + project directory [RSS Feed]

#1 June 10, 2010 19:35:11

rahul j.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

not able to recognize non-default python files in app + project directory


Hi Django,

In my app directory, I splitted my models and views.py into multiple
files since they start becoming very long but django is not able to
recognize those files .
It only recognizes models.py, views.py, tests.py (default ones).

So In my other files If i do this

from <project_name>.<app name>.models import <class name>

I get this error

ImportError: cannot import name <class name>

If I do the same on views.py, it just works fine.

Even on Eclipse I can see Django default files with different symbol
and python files with different symbols.

How to solve this problem ?

--RJ

--
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

#2 June 11, 2010 00:11:20

rahul j.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

not able to recognize non-default python files in app + project directory


HI Dan,

Thanks for your response but that will not solve my problem.

I am not splitting models. I am splitting actions defined in the
models across multiple files.

So suppose

one of my model actions is calling which in turn calls the constructor
or simple functions defined in normal python files

for ex:

def action (self, request, queryset):

Test1(<params>) //calls default constructor of the class
Test2(<params>)

Now each of these are separate class files names as Test1.py Test2.py.

In each of these files i want to do this

from <project_name>.<app name>.models import <class name>

But I get this error

ImportError: cannot import name <class name>

I tired various other variotions like just

from models import <class name>

from <app name>.models import <class name>

or without from

but none of them are working.

Also, in order to call this Test1(<params>) I thought if I import
Test1 in models then it will just work but again not

--RJ



On Thu, Jun 10, 2010 at 1:57 PM, Dan Harris <dih0...@gmail.com> wrote:
> When you split things out into multiple files or directories you have
> to remember to import them into the models.py file or the __init__.py
> file (if using a directory).
>
> So if i had some models in a new file "extraModels.py" for example
>
> # In extraModels.py
> class ExtraModel(models.Model):
>    pass
>
> Then in the regular "models.py"
>
> # In models.py
> from extraModels import ExtraModel
>
> class RegularModels(model.Model):
>   pass
>
> The key is that you ahve to import any models in your split files into
> your normal models.py file. This is because Django looks for models in
> the models.py file only. If you make a models directory, you have to
> import all models you want Django to recognize into your __init__.py
> file within the models directory.
>
> Hopefully this makes sense, if not I can try to be more clear.
>
> Dan Harris
> dih0...@gmail.com
>
>
> On Jun 10, 4:46 pm, rahul jain <jainwolver...@gmail.com> wrote:
>> anyone on this ??
>>
>> --RJ
>>
>>
>>
>> On Thu, Jun 10, 2010 at 11:34 AM, rahul jain <jainwolver...@gmail.com> wrote:
>> > Hi Django,
>>
>> > In my app directory, I splitted my models and views.py into multiple
>> > files since they start becoming very long but django is not able to
>> > recognize those files .
>> > It only recognizes models.py, views.py, tests.py (default ones).
>>
>> > So In my other files If i do this
>>
>> > from <project_name>.<app name>.models import <class name>
>>
>> > I get this error
>>
>> > ImportError: cannot import name <class name>
>>
>> > If I do the same on views.py, it just works fine.
>>
>> > Even on Eclipse I can see Django default files with different symbol
>> > and python files with different symbols.
>>
>> > How to solve this problem ?
>>
>> > --RJ
>
> --
> 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 at
>http://groups.google.com/group/django-users?hl=en.
>
>

--
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 June 11, 2010 00:27:14

rahul j.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

not able to recognize non-default python files in app + project directory


anyone on this ??

--RJ

On Thu, Jun 10, 2010 at 11:34 AM, rahul jain <jainwolver...@gmail.com> wrote:
> Hi Django,
>
> In my app directory, I splitted my models and views.py into multiple
> files since they start becoming very long but django is not able to
> recognize those files .
> It only recognizes models.py, views.py, tests.py (default ones).
>
> So In my other files If i do this
>
> from <project_name>.<app name>.models import <class name>
>
> I get this error
>
> ImportError: cannot import name <class name>
>
> If I do the same on views.py, it just works fine.
>
> Even on Eclipse I can see Django default files with different symbol
> and python files with different symbols.
>
> How to solve this problem ?
>
> --RJ
>

--
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

#4 June 11, 2010 00:44:56

Dan H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

not able to recognize non-default python files in app + project directory


When you split things out into multiple files or directories you have
to remember to import them into the models.py file or the __init__.py
file (if using a directory).

So if i had some models in a new file "extraModels.py" for example

# In extraModels.py
class ExtraModel(models.Model):
pass

Then in the regular "models.py"

# In models.py
from extraModels import ExtraModel

class RegularModels(model.Model):
pass

The key is that you ahve to import any models in your split files into
your normal models.py file. This is because Django looks for models in
the models.py file only. If you make a models directory, you have to
import all models you want Django to recognize into your __init__.py
file within the models directory.

Hopefully this makes sense, if not I can try to be more clear.

Dan Harris
dih0...@gmail.com


On Jun 10, 4:46 pm, rahul jain <jainwolver...@gmail.com> wrote:
> anyone on this ??
>
> --RJ
>
>
>
> On Thu, Jun 10, 2010 at 11:34 AM, rahul jain <jainwolver...@gmail.com> wrote:
> > Hi Django,
>
> > In my app directory, I splitted my models and views.py into multiple
> > files since they start becoming very long but django is not able to
> > recognize those files .
> > It only recognizes models.py, views.py, tests.py (default ones).
>
> > So In my other files If i do this
>
> > from <project_name>.<app name>.models import <class name>
>
> > I get this error
>
> > ImportError: cannot import name <class name>
>
> > If I do the same on views.py, it just works fine.
>
> > Even on Eclipse I can see Django default files with different symbol
> > and python files with different symbols.
>
> > How to solve this problem ?
>
> > --RJ

--
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 June 11, 2010 11:38:50

Scott G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

not able to recognize non-default python files in app + project directory


Did you put Test1.py and Test2.py into a "models" directory where
models.py would normally be, and add an __initial__.py file to it?

On Jun 10, 5:18 pm, rahul jain <jainwolver...@gmail.com> wrote:
> HI Dan,
>
> Thanks for your response but that will not solve my problem.
>
> I am not splitting models. I am splitting actions defined in the
> models  across multiple files.
>
> So suppose
>
> one of my model actions is calling which in turn calls the constructor
> or simple functions defined in normal python files
>
> for ex:
>
> def action (self, request, queryset):
>
>     Test1(<params>)     //calls default constructor of the class
>     Test2(<params>)
>
> Now each of these are separate class files names as Test1.py Test2.py.
>
> In each of these files i want to do this
>
> from <project_name>.<app name>.models import <class name>
>
> But I get this error
>
> ImportError: cannot import name <class name>
>
> I tired various other variotions like just
>
> from models import <class name>
>
> from <app name>.models import <class name>
>
> or without from
>
> but none of them are working.
>
> Also, in order to call this Test1(<params>) I thought if  I import
> Test1 in models then it will just work but again not
>
> --RJ
>
>
>
> On Thu, Jun 10, 2010 at 1:57 PM, Dan Harris <dih0...@gmail.com> wrote:
> > When you split things out into multiple files or directories you have
> > to remember to import them into the models.py file or the __init__.py
> > file (if using a directory).
>
> > So if i had some models in a new file "extraModels.py" for example
>
> > # In extraModels.py
> > class ExtraModel(models.Model):
> >    pass
>
> > Then in the regular "models.py"
>
> > # In models.py
> > from extraModels import ExtraModel
>
> > class RegularModels(model.Model):
> >   pass
>
> > The key is that you ahve to import any models in your split files into
> > your normal models.py file. This is because Django looks for models in
> > the models.py file only. If you make a models directory, you have to
> > import all models you want Django to recognize into your __init__.py
> > file within the models directory.
>
> > Hopefully this makes sense, if not I can try to be more clear.
>
> > Dan Harris
> > dih0...@gmail.com
>
> > On Jun 10, 4:46 pm, rahul jain <jainwolver...@gmail.com> wrote:
> >> anyone on this ??
>
> >> --RJ
>
> >> On Thu, Jun 10, 2010 at 11:34 AM, rahul jain <jainwolver...@gmail.com>
> >> wrote:
> >> > Hi Django,
>
> >> > In my app directory, I splitted my models and views.py into multiple
> >> > files since they start becoming very long but django is not able to
> >> > recognize those files .
> >> > It only recognizes models.py, views.py, tests.py (default ones).
>
> >> > So In my other files If i do this
>
> >> > from <project_name>.<app name>.models import <class name>
>
> >> > I get this error
>
> >> > ImportError: cannot import name <class name>
>
> >> > If I do the same on views.py, it just works fine.
>
> >> > Even on Eclipse I can see Django default files with different symbol
> >> > and python files with different symbols.
>
> >> > How to solve this problem ?
>
> >> > --RJ
>
> > --
> > 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.

--
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

#6 June 11, 2010 13:23:30

bruno d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

not able to recognize non-default python files in app + project directory


On 11 juin, 12:38, Scott Gould <zinck...@gmail.com> wrote:
> Did you put Test1.py and Test2.py into a "models" directory where
> models.py would normally be, and add an __initial__.py file to it?
>

s/__initial__/__init__/

--
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

#7 June 11, 2010 13:26:48

bruno d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

not able to recognize non-default python files in app + project directory


On 10 juin, 23:18, rahul jain <jainwolver...@gmail.com> wrote:
> HI Dan,
>
> Thanks for your response but that will not solve my problem.
>
> I am not splitting models. I am splitting actions

I assume you mean "functions" ?

> defined in the
> models  across multiple files.

(snip)

>
> But I get this error
>
> ImportError: cannot import name <class name>
>http://docs.python.org/tutorial/modules.htmlHTH

--
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

#8 June 11, 2010 14:53:50

rahul j.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

not able to recognize non-default python files in app + project directory


Yes they are in the same directory. How to use __initial__.py ?

On Fri, Jun 11, 2010 at 3:38 AM, Scott Gould <zinck...@gmail.com> wrote:
> Did you put Test1.py and Test2.py into a "models" directory where
> models.py would normally be, and add an __initial__.py file to it?
>
> On Jun 10, 5:18 pm, rahul jain <jainwolver...@gmail.com> wrote:
>> HI Dan,
>>
>> Thanks for your response but that will not solve my problem.
>>
>> I am not splitting models. I am splitting actions defined in the
>> models  across multiple files.
>>
>> So suppose
>>
>> one of my model actions is calling which in turn calls the constructor
>> or simple functions defined in normal python files
>>
>> for ex:
>>
>> def action (self, request, queryset):
>>
>>     Test1(<params>)     //calls default constructor of the class
>>     Test2(<params>)
>>
>> Now each of these are separate class files names as Test1.py Test2.py.
>>
>> In each of these files i want to do this
>>
>> from <project_name>.<app name>.models import <class name>
>>
>> But I get this error
>>
>> ImportError: cannot import name <class name>
>>
>> I tired various other variotions like just
>>
>> from models import <class name>
>>
>> from <app name>.models import <class name>
>>
>> or without from
>>
>> but none of them are working.
>>
>> Also, in order to call this Test1(<params>) I thought if  I import
>> Test1 in models then it will just work but again not
>>
>> --RJ
>>
>>
>>
>> On Thu, Jun 10, 2010 at 1:57 PM, Dan Harris <dih0...@gmail.com> wrote:
>> > When you split things out into multiple files or directories you have
>> > to remember to import them into the models.py file or the __init__.py
>> > file (if using a directory).
>>
>> > So if i had some models in a new file "extraModels.py" for example
>>
>> > # In extraModels.py
>> > class ExtraModel(models.Model):
>> >    pass
>>
>> > Then in the regular "models.py"
>>
>> > # In models.py
>> > from extraModels import ExtraModel
>>
>> > class RegularModels(model.Model):
>> >   pass
>>
>> > The key is that you ahve to import any models in your split files into
>> > your normal models.py file. This is because Django looks for models in
>> > the models.py file only. If you make a models directory, you have to
>> > import all models you want Django to recognize into your __init__.py
>> > file within the models directory.
>>
>> > Hopefully this makes sense, if not I can try to be more clear.
>>
>> > Dan Harris
>> > dih0...@gmail.com
>>
>> > On Jun 10, 4:46 pm, rahul jain <jainwolver...@gmail.com> wrote:
>> >> anyone on this ??
>>
>> >> --RJ
>>
>> >> On Thu, Jun 10, 2010 at 11:34 AM, rahul jain <jainwolver...@gmail.com>
>> >> wrote:
>> >> > Hi Django,
>>
>> >> > In my app directory, I splitted my models and views.py into multiple
>> >> > files since they start becoming very long but django is not able to
>> >> > recognize those files .
>> >> > It only recognizes models.py, views.py, tests.py (default ones).
>>
>> >> > So In my other files If i do this
>>
>> >> > from <project_name>.<app name>.models import <class name>
>>
>> >> > I get this error
>>
>> >> > ImportError: cannot import name <class name>
>>
>> >> > If I do the same on views.py, it just works fine.
>>
>> >> > Even on Eclipse I can see Django default files with different symbol
>> >> > and python files with different symbols.
>>
>> >> > How to solve this problem ?
>>
>> >> > --RJ
>>
>> > --
>> > 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.
>
> --
> 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 at
>http://groups.google.com/group/django-users?hl=en.
>
>

--
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

#9 June 11, 2010 15:02:50

Tom E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

not able to recognize non-default python files in app + project directory


On Fri, Jun 11, 2010 at 2:53 PM, rahul jain <jainwolver...@gmail.com> wrote:
> Yes they are in the same directory. How to use __initial__.py ?
>

It's called __init__.py. It must exist in any python module
(directory), and need not contain anything.

You were linked to this earlier in the thread, I suggest you read it!http://docs.python.org/tutorial/modules.htmlHTH

--
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

#10 June 11, 2010 15:04:58

rahul j.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

not able to recognize non-default python files in app + project directory


@bruno: yes functions.

So in some other file I want to use models methods like

<Model>.objects. all

For that I have to import the <Model> . But I am not able to.

Import works fine on views.py

Also, can you test this for me

Create views1.py (not views.py)

import your model class

and in one of the functions do
<Model>.objects. all

and then run python manage.py runserver. I think it will break.

Let me know what i am missing or will __initial__.py will solve this
problem ?.





On Fri, Jun 11, 2010 at 6:53 AM, rahul jain <jainwolver...@gmail.com> wrote:
> Yes they are in the same directory. How to use __initial__.py ?
>
> On Fri, Jun 11, 2010 at 3:38 AM, Scott Gould <zinck...@gmail.com> wrote:
>> Did you put Test1.py and Test2.py into a "models" directory where
>> models.py would normally be, and add an __initial__.py file to it?
>>
>> On Jun 10, 5:18 pm, rahul jain <jainwolver...@gmail.com> wrote:
>>> HI Dan,
>>>
>>> Thanks for your response but that will not solve my problem.
>>>
>>> I am not splitting models. I am splitting actions defined in the
>>> models  across multiple files.
>>>
>>> So suppose
>>>
>>> one of my model actions is calling which in turn calls the constructor
>>> or simple functions defined in normal python files
>>>
>>> for ex:
>>>
>>> def action (self, request, queryset):
>>>
>>>     Test1(<params>)     //calls default constructor of the class
>>>     Test2(<params>)
>>>
>>> Now each of these are separate class files names as Test1.py Test2.py.
>>>
>>> In each of these files i want to do this
>>>
>>> from <project_name>.<app name>.models import <class name>
>>>
>>> But I get this error
>>>
>>> ImportError: cannot import name <class name>
>>>
>>> I tired various other variotions like just
>>>
>>> from models import <class name>
>>>
>>> from <app name>.models import <class name>
>>>
>>> or without from
>>>
>>> but none of them are working.
>>>
>>> Also, in order to call this Test1(<params>) I thought if  I import
>>> Test1 in models then it will just work but again not
>>>
>>> --RJ
>>>
>>>
>>>
>>> On Thu, Jun 10, 2010 at 1:57 PM, Dan Harris <dih0...@gmail.com> wrote:
>>> > When you split things out into multiple files or directories you have
>>> > to remember to import them into the models.py file or the __init__.py
>>> > file (if using a directory).
>>>
>>> > So if i had some models in a new file "extraModels.py" for example
>>>
>>> > # In extraModels.py
>>> > class ExtraModel(models.Model):
>>> >    pass
>>>
>>> > Then in the regular "models.py"
>>>
>>> > # In models.py
>>> > from extraModels import ExtraModel
>>>
>>> > class RegularModels(model.Model):
>>> >   pass
>>>
>>> > The key is that you ahve to import any models in your split files into
>>> > your normal models.py file. This is because Django looks for models in
>>> > the models.py file only. If you make a models directory, you have to
>>> > import all models you want Django to recognize into your __init__.py
>>> > file within the models directory.
>>>
>>> > Hopefully this makes sense, if not I can try to be more clear.
>>>
>>> > Dan Harris
>>> > dih0...@gmail.com
>>>
>>> > On Jun 10, 4:46 pm, rahul jain <jainwolver...@gmail.com> wrote:
>>> >> anyone on this ??
>>>
>>> >> --RJ
>>>
>>> >> On Thu, Jun 10, 2010 at 11:34 AM, rahul jain <jainwolver...@gmail.com>
>>> >> wrote:
>>> >> > Hi Django,
>>>
>>> >> > In my app directory, I splitted my models and views.py into multiple
>>> >> > files since they start becoming very long but django is not able to
>>> >> > recognize those files .
>>> >> > It only recognizes models.py, views.py, tests.py (default ones).
>>>
>>> >> > So In my other files If i do this
>>>
>>> >> > from <project_name>.<app name>.models import <class name>
>>>
>>> >> > I get this error
>>>
>>> >> > ImportError: cannot import name <class name>
>>>
>>> >> > If I do the same on views.py, it just works fine.
>>>
>>> >> > Even on Eclipse I can see Django default files with different symbol
>>> >> > and python files with different symbols.
>>>
>>> >> > How to solve this problem ?
>>>
>>> >> > --RJ
>>>
>>> > --
>>> > 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.
>>
>> --
>> 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 at
>>http://groups.google.com/group/django-users?hl=en.
>>
>>
>

--
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

  • Root
  • » Django
  • » not able to recognize non-default python files in app + project directory [RSS Feed]

Board footer

Moderator control

Enjoy the 20th of October
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