Re: circular imports error

2021-06-11 Thread Adam Stein
Glad to be of help. Sometimes it just takes a fresh pair of eyes.

On Fri, 2021-06-11 at 17:33 -0500, frank dilorenzo wrote:
> I hate to sound corny but you sir are my hero!.  Thanks,  it all
> works now
>  
> frank-
> 
> 
> On Fri, Jun 11, 2021 at 2:43 PM Adam Stein  wrote:
> > Since you said 3 separate apps, I assume each of these classes are
> > in a separate file. However, you have the line:
> > 
> > from .models import Supplier
> > 
> > which would make it seem that Supplier is defined in the same file
> > as Shipment. Shipment should be in ".models" and if Supplier is
> > also defined in the same file, no need to import it. If it's
> > defined in a different file, then the import should reference
> > another place, not ".models".
> > 
> > On Fri, 2021-06-11 at 12:22 -0700, frank dilorenzo wrote:
> > > Hello,  I have 3 separate app as follow:
> > > from django.db import models
> > > 
> > > # Create your models here.
> > > 
> > > # Supplier can have many shipments
> > > class Supplier(models.Model):
> > > name = models.CharField(max_length=120, null=True)
> > > phone = models.CharField(max_length=15, null=True)
> > > email = models.CharField(max_length=120, null=True)
> > > created = models.DateTimeField(auto_now_add=True)
> > > updated = models.DateTimeField(auto_now=True)
> > > 
> > > def __str__(self):
> > > return self.name
> > > -
> > > from django.db import models
> > > from django.utils import timezone
> > > 
> > > # Create your models here.
> > > 
> > > # A specie can belong to many shipments
> > > class Specie(models.Model):
> > > name = models.CharField(max_length=120, null=True)
> > > image = models.ImageField(upload_to="species",
> > > default="no_picture.png")
> > > created = models.DateTimeField(default=timezone.now)
> > > 
> > > def __str__(self):
> > > return self.name
> > > 
> > > from django.db import models
> > > from .models import Supplier
> > > 
> > > # from .models import Supplier, Specie
> > > 
> > > # Create your models here.
> > > 
> > > # A shipment can have many species
> > > class Shipment(models.Model):
> > > 
> > > supplier = models.ManyToManyField(Suppliers)
> > > specie = models.ManyToManyField(Species)
> > > 
> > > name = models.CharField(
> > > max_length=120, null=True, help_text="enter name from Supplier
> > > above..."
> > > )
> > > slabel = models.CharField(max_length=10)
> > > received = models.PositiveIntegerField(default=0)
> > > bad = models.PositiveIntegerField(default=0)
> > > non = models.PositiveIntegerField(default=0)
> > > doa = models.PositiveIntegerField(default=0)
> > > para = models.PositiveIntegerField(default=0)
> > > released = models.PositiveIntegerField(default=0)
> > > entered = models.BooleanField(default=False)
> > > created = models.DateTimeField(default=timezone.now)
> > > 
> > > def __str__(self):
> > > return f"{self.slabel}"
> > > 
> > > ===
> > > 
> > > when trying to migrate I get this error message:
> > > 
> > > ImportError: cannot import name 'Supplier' from partially
> > > initialized module 'shipments.models' (most likely due to a
> > > circular import)
> > > (/Users/frankd/django_projects/stlzoo/src/shipments/models.py)
> > > 
> > > I am stuck here and cannot seem to resolve this error.  I would
> > > truly appreciate any help on this.  Thanks.
> > > 
> > > Frank
> > > -- 
> > > You received this message because you are subscribed to the
> > > Google Groups "Django users" group.
> > > To unsubscribe from this group and stop receiving emails from it,
> > > send an email to django-users+unsubscr...@googlegroups.com.
> > > To view this discussion on the web visit
> > >
> >
> https://groups.google.com/d/msgid/django-users/c0aff4c4-651a-43ad-9ac9-42c6452a33f4n%40googlegroups.com
> > > .
> > 
> > 
> > -- 
> > Adam (a...@csh.rit.edu)
> > 

-- 
Adam (a...@csh.rit.edu)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/34fe18d800ae49957c4a6ababcc80d943bf95295.camel%40csh.rit.edu.


Re: circular imports error

2021-06-11 Thread frank dilorenzo
I hate to sound corny but you sir are my hero!.  Thanks,  it all works now

frank-


On Fri, Jun 11, 2021 at 2:43 PM Adam Stein  wrote:

> Since you said 3 separate apps, I assume each of these classes are in a
> separate file. However, you have the line:
>
> from .models import Supplier
>
> which would make it seem that Supplier is defined in the same file as
> Shipment. Shipment should be in ".models" and if Supplier is also defined
> in the same file, no need to import it. If it's defined in a different
> file, then the import should reference another place, not ".models".
>
> On Fri, 2021-06-11 at 12:22 -0700, frank dilorenzo wrote:
>
> Hello,  I have 3 separate app as follow:
> from django.db import models
>
> # Create your models here.
>
> # Supplier can have many shipments
> class Supplier(models.Model):
> name = models.CharField(max_length=120, null=True)
> phone = models.CharField(max_length=15, null=True)
> email = models.CharField(max_length=120, null=True)
> created = models.DateTimeField(auto_now_add=True)
> updated = models.DateTimeField(auto_now=True)
>
> def __str__(self):
> return self.name
> -
> from django.db import models
> from django.utils import timezone
>
> # Create your models here.
>
> # A specie can belong to many shipments
> class Specie(models.Model):
> name = models.CharField(max_length=120, null=True)
> image = models.ImageField(upload_to="species", default="no_picture.png")
> created = models.DateTimeField(default=timezone.now)
>
> def __str__(self):
> return self.name
> 
> from django.db import models
> from .models import Supplier
>
> # from .models import Supplier, Specie
>
> # Create your models here.
>
> # A shipment can have many species
> class Shipment(models.Model):
>
> supplier = models.ManyToManyField(Suppliers)
> specie = models.ManyToManyField(Species)
>
> name = models.CharField(
> max_length=120, null=True, help_text="enter name from Supplier above..."
> )
> slabel = models.CharField(max_length=10)
> received = models.PositiveIntegerField(default=0)
> bad = models.PositiveIntegerField(default=0)
> non = models.PositiveIntegerField(default=0)
> doa = models.PositiveIntegerField(default=0)
> para = models.PositiveIntegerField(default=0)
> released = models.PositiveIntegerField(default=0)
> entered = models.BooleanField(default=False)
> created = models.DateTimeField(default=timezone.now)
>
> def __str__(self):
> return f"{self.slabel}"
>
> ===
>
> when trying to migrate I get this error message:
>
> ImportError: cannot import name 'Supplier' from partially initialized
> module 'shipments.models' (most likely due to a circular import)
> (/Users/frankd/django_projects/stlzoo/src/shipments/models.py)
>
> I am stuck here and cannot seem to resolve this error.  I would truly
> appreciate any help on this.  Thanks.
>
> Frank
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/c0aff4c4-651a-43ad-9ac9-42c6452a33f4n%40googlegroups.com
> 
> .
>
>
> --
>
> Adam (a...@csh.rit.edu)
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/a5c49b5dedd58f28ba9d7039ed8977b626b53a1e.camel%40csh.rit.edu
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAJbiy1CTynKdbOoPYyT%3DbB02YsEw3baFsdBr5dhG4NokcU2mBA%40mail.gmail.com.


Re: circular imports error

2021-06-11 Thread Adam Stein
Since you said 3 separate apps, I assume each of these classes are in a
separate file. However, you have the line:

 from .models import Supplier

which would make it seem that Supplier is defined in the same file as
Shipment. Shipment should be in ".models" and if Supplier is also
defined in the same file, no need to import it. If it's defined in a
different file, then the import should reference another place, not
".models".

On Fri, 2021-06-11 at 12:22 -0700, frank dilorenzo wrote:
> Hello,  I have 3 separate app as follow:
> from django.db import models
> 
> # Create your models here.
> 
> # Supplier can have many shipments
> class Supplier(models.Model):
> name = models.CharField(max_length=120, null=True)
> phone = models.CharField(max_length=15, null=True)
> email = models.CharField(max_length=120, null=True)
> created = models.DateTimeField(auto_now_add=True)
> updated = models.DateTimeField(auto_now=True)
> 
> def __str__(self):
> return self.name
> -
> from django.db import models
> from django.utils import timezone
> 
> # Create your models here.
> 
> # A specie can belong to many shipments
> class Specie(models.Model):
> name = models.CharField(max_length=120, null=True)
> image = models.ImageField(upload_to="species",
> default="no_picture.png")
> created = models.DateTimeField(default=timezone.now)
> 
> def __str__(self):
> return self.name
> 
> from django.db import models
> from .models import Supplier
> 
> # from .models import Supplier, Specie
> 
> # Create your models here.
> 
> # A shipment can have many species
> class Shipment(models.Model):
> 
> supplier = models.ManyToManyField(Suppliers)
> specie = models.ManyToManyField(Species)
> 
> name = models.CharField(
> max_length=120, null=True, help_text="enter name from Supplier
> above..."
> )
> slabel = models.CharField(max_length=10)
> received = models.PositiveIntegerField(default=0)
> bad = models.PositiveIntegerField(default=0)
> non = models.PositiveIntegerField(default=0)
> doa = models.PositiveIntegerField(default=0)
> para = models.PositiveIntegerField(default=0)
> released = models.PositiveIntegerField(default=0)
> entered = models.BooleanField(default=False)
> created = models.DateTimeField(default=timezone.now)
> 
> def __str__(self):
> return f"{self.slabel}"
> 
> ===
> 
> when trying to migrate I get this error message:
> 
> ImportError: cannot import name 'Supplier' from partially initialized
> module 'shipments.models' (most likely due to a circular import)
> (/Users/frankd/django_projects/stlzoo/src/shipments/models.py)
> 
> I am stuck here and cannot seem to resolve this error.  I would truly
> appreciate any help on this.  Thanks.
> 
> Frank
> -- 
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/c0aff4c4-651a-43ad-9ac9-42c6452a33f4n%40googlegroups.com
> .

-- 
Adam (a...@csh.rit.edu)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a5c49b5dedd58f28ba9d7039ed8977b626b53a1e.camel%40csh.rit.edu.


circular imports error

2021-06-11 Thread frank dilorenzo
Hello,  I have 3 separate app as follow:
from django.db import models

# Create your models here.

# Supplier can have many shipments
class Supplier(models.Model):
name = models.CharField(max_length=120, null=True)
phone = models.CharField(max_length=15, null=True)
email = models.CharField(max_length=120, null=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)

def __str__(self):
return self.name
-
from django.db import models
from django.utils import timezone

# Create your models here.

# A specie can belong to many shipments
class Specie(models.Model):
name = models.CharField(max_length=120, null=True)
image = models.ImageField(upload_to="species", default="no_picture.png")
created = models.DateTimeField(default=timezone.now)

def __str__(self):
return self.name

from django.db import models
from .models import Supplier

# from .models import Supplier, Specie

# Create your models here.

# A shipment can have many species
class Shipment(models.Model):

supplier = models.ManyToManyField(Suppliers)
specie = models.ManyToManyField(Species)

name = models.CharField(
max_length=120, null=True, help_text="enter name from Supplier above..."
)
slabel = models.CharField(max_length=10)
received = models.PositiveIntegerField(default=0)
bad = models.PositiveIntegerField(default=0)
non = models.PositiveIntegerField(default=0)
doa = models.PositiveIntegerField(default=0)
para = models.PositiveIntegerField(default=0)
released = models.PositiveIntegerField(default=0)
entered = models.BooleanField(default=False)
created = models.DateTimeField(default=timezone.now)

def __str__(self):
return f"{self.slabel}"

===

when trying to migrate I get this error message:

ImportError: cannot import name 'Supplier' from partially initialized 
module 'shipments.models' (most likely due to a circular import) 
(/Users/frankd/django_projects/stlzoo/src/shipments/models.py)

I am stuck here and cannot seem to resolve this error.  I would truly 
appreciate any help on this.  Thanks.

Frank

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c0aff4c4-651a-43ad-9ac9-42c6452a33f4n%40googlegroups.com.


Re: Manually running a script that imports a Django model

2020-02-13 Thread maninder singh Kumar
Could it be an improperly configured database ?
Also check installed apps.  
It is most likely a syntax error in settings like a comma, colon or apostrophe

Sent from my iPad

> On 13-Feb-2020, at 7:34 PM, onlinejudge95  wrote:
> 
>> On Thu, Feb 13, 2020 at 6:34 PM Sourish Kundu  
>> wrote:
> 
>> So I am trying to access one of my models created in views.py in another 
>> script. This second script is the one I would like manually run. It imports 
>> the model without any errors; however, when I try to run it using PyCharm, I 
>> get this error:
> 
> Firstly, what are your models doing in views.py? I am assuming it was a typo.
>  
>> 
>> django.core.exceptions.ImproperlyConfigured: Requested setting 
>> INSTALLED_APPS, but settings are not configured. You must either define the 
>> environment variable DJANGO_SETTINGS_MODULE or call settings.configure() 
>> before accessing settings.
>> 
>> Any help on resolving this issue would be very much appreciated.
> 
> For scripts like these that are not part of your web server or that do not 
> take part in the request-response cycle, I like to implement them as custom 
> Django commands. You can read more about them here.
>  
>> 
>> 
>> Thanks,
>> Sourish
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/2c9b2d34-a9ab-4a2f-a1e4-e2694c265ea0%40googlegroups.com.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/CAD%3DM5eSBrbLvnFBzoGfOaJk3NnWG8t1N2aOQTmHR6TDsup3F%2BA%40mail.gmail.com.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/824DE3AE-7DEE-4A00-8AF2-F709F371E06A%40gmail.com.


Re: Manually running a script that imports a Django model

2020-02-13 Thread onlinejudge95
On Thu, Feb 13, 2020 at 6:34 PM Sourish Kundu 
wrote:

> So I am trying to access one of my models created in views.py in another
> script. This second script is the one I would like manually run. It imports
> the model without any errors; however, when I try to run it using PyCharm,
> I get this error:
>

Firstly, what are your models doing in views.py? I am assuming it was a
typo.


>
> *django.core.exceptions.ImproperlyConfigured: Requested setting
> INSTALLED_APPS, but settings are not configured. You must either define the
> environment variable DJANGO_SETTINGS_MODULE or call settings.configure()
> before accessing settings.*
>
> Any help on resolving this issue would be very much appreciated.
>

For scripts like these that are not part of your web server or that do not
take part in the request-response cycle, I like to implement them as custom
Django commands. You can read more about them here
<https://docs.djangoproject.com/en/3.0/howto/custom-management-commands>.


>
>
> Thanks,
> Sourish
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/2c9b2d34-a9ab-4a2f-a1e4-e2694c265ea0%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/2c9b2d34-a9ab-4a2f-a1e4-e2694c265ea0%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAD%3DM5eSBrbLvnFBzoGfOaJk3NnWG8t1N2aOQTmHR6TDsup3F%2BA%40mail.gmail.com.


Re: Manually running a script that imports a Django model

2020-02-13 Thread Adam Mičuda
Hi,
the solution is written in the error message "You must either define the
environment variable DJANGO_SETTINGS_MODULE or call
settings.configure()...". But I would recommend you to use
https://django-extensions.readthedocs.io/en/latest/runscript.html. I think
it is what you are want. ;)

Regards.

Adam

čt 13. 2. 2020 v 14:03 odesílatel Sourish Kundu 
napsal:

> So I am trying to access one of my models created in views.py in another
> script. This second script is the one I would like manually run. It imports
> the model without any errors; however, when I try to run it using PyCharm,
> I get this error:
>
> *django.core.exceptions.ImproperlyConfigured: Requested setting
> INSTALLED_APPS, but settings are not configured. You must either define the
> environment variable DJANGO_SETTINGS_MODULE or call settings.configure()
> before accessing settings.*
>
> Any help on resolving this issue would be very much appreciated.
>
>
> Thanks,
> Sourish
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/2c9b2d34-a9ab-4a2f-a1e4-e2694c265ea0%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/2c9b2d34-a9ab-4a2f-a1e4-e2694c265ea0%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAB9%3DGXasEd5NmCzc9B4dw4xZUdcPZnqNg_maQJqQ%2B%2B_gjCG1mA%40mail.gmail.com.


Manually running a script that imports a Django model

2020-02-13 Thread Sourish Kundu
So I am trying to access one of my models created in views.py in another 
script. This second script is the one I would like manually run. It imports 
the model without any errors; however, when I try to run it using PyCharm, 
I get this error:

*django.core.exceptions.ImproperlyConfigured: Requested setting 
INSTALLED_APPS, but settings are not configured. You must either define the 
environment variable DJANGO_SETTINGS_MODULE or call settings.configure() 
before accessing settings.*

Any help on resolving this issue would be very much appreciated.


Thanks,
Sourish

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2c9b2d34-a9ab-4a2f-a1e4-e2694c265ea0%40googlegroups.com.


Re: wrong imports channel ?

2018-04-12 Thread Andrew Godwin
Channels 2 has different objects and import paths - code from Channels 1
(like you have there) isn't compatible.

Andrew

On Thu, Apr 12, 2018 at 12:37 AM, Jules LE BRIS 
wrote:

> Hi,
>
> I would like to do something like this in views.py :
>
> message = {"message":msg}
> Channel('chat').send(message, immediately=True)
>
> but the import : from channels import Channel doen't work. I'm using
> channels==2.0.2.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/b7684c27-9568-4321-a70b-29e58c550b3d%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uqCuYwJYkFpnQ%3DtcVXjNNZQi-5fx9GNj%2BmZkADntv1jLQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: wrong imports channel ?

2018-04-12 Thread N. Srinivasan
Thank you Sir!

On Thu, Apr 12, 2018 at 3:37 AM, Jules LE BRIS  wrote:

> Hi,
>
> I would like to do something like this in views.py :
>
> message = {"message":msg}
> Channel('chat').send(message, immediately=True)
>
> but the import : from channels import Channel doen't work. I'm using
> channels==2.0.2.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/b7684c27-9568-4321-a70b-29e58c550b3d%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAEAcyBZpVtPgJhHdrvYR5Wg-em0nM%3DEdcqxJ9gvhHfWeP91ziA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


wrong imports channel ?

2018-04-12 Thread Jules LE BRIS
Hi,

I would like to do something like this in views.py :

message = {"message":msg}
Channel('chat').send(message, immediately=True)

but the import : from channels import Channel doen't work. I'm using 
channels==2.0.2.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b7684c27-9568-4321-a70b-29e58c550b3d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Problem Django.19 upgrade and circular imports

2016-03-08 Thread Andres Osinski
So I've tried going back to Django 1.7 but changing the app configuration
to use the AppConfig class and I've hit upon the same issue.

It would seem that part of the key is the fact that when the system hits
this line:

/home/osinski/Projects/feastly/apps/account/models.py in ()
 11
 12 from ipdb import set_trace; set_trace()
---> 13 from apps.common.models import CommonInfo
 14 from feastly.paths import get_path

ImportError: No module named common.models

It would appear as though the "common" module hasn't loaded in apps yet.
Some posts on previous versions of Django have advised that the 'apps'
subdirectory should be included in the system path; we have never done this
before and thus far things have worked well, however I don't know if this
was suppored behavior. The negative to doing that is that I'd have to
rename all the imports from 'apps.X' to 'X'.

Does Django support apps that reside in submodules or have I been depending
on broken behavior?


On Fri, Mar 4, 2016 at 5:16 PM, Andres Osinski 
wrote:

> The thing is that in my case, I have a model in the 'accounts' app which
> inherits from a model in 'common', therefore I need the import to happen in
> one's models.
>
> I've tried this in Django 1.8 and I see the same behavior; therefore the
> change occurs between the latest versions of 1.7 and 1.8. I'll try with
> older versions in 1.8 and report back.
>
> On Thu, Mar 3, 2016 at 10:11 PM, James Schneider 
> wrote:
>
>> On Thu, Mar 3, 2016 at 3:28 PM, Andres Osinski 
>> wrote:
>>
>>> Hi everyone! I'm having an issue with circular imports in Django 1.9
>>> that I have not seen in my app, which had been running 1.7 before.
>>>
>>> I have a Django app in apps.common, which has its associated
>>> apps.common.models too. No imports on __init__.py or anything of the sort.
>>> When I my app in apps.account.models attempts to import
>>> apps.common.models, it complains that said module does not exist.
>>>
>>> Putting a pdb set_trace() call at the top of apps.common.models and
>>> apps.account.models shows that the former executes *before*
>>> apps.account.models, so I fail to see how this could result in the posted
>>> error.
>>>
>>> For the record, both apps use the new AppConfig scheme in their
>>> corresponding apps.py file, and the import order in INSTALLED_APPS is:
>>>
>>> 'apps.common.CommonConfig',
>>> 'apps.account.AccountConfig',
>>> ...
>>>
>>> Has anyone else had a similar issue in this upgrade? Is there anything I
>>> can do to diagnose the source of this?
>>>
>>
>> Are you bringing in other functions in as part of your imports, or just
>> other models? If you are only bringing in models for use with FK's and
>> M2M's, then I would suggest referring to those models via their text path
>> rather than using the Python class object directly. It avoids needing the
>> import statement during the initial load (which avoids the circular import
>> issue). See the end of this section about referring to models in other
>> applications using a string rather than the real Python object (and make
>> sure to remove the related import statements):
>>
>> https://docs.djangoproject.com/en/1.9/ref/models/fields/#foreignkey
>>
>> -James
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CA%2Be%2BciUWHXr6axqA5drrsMnR3%2B9d0D1wBHSK9wQwSmqs52xFAw%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-users/CA%2Be%2BciUWHXr6axqA5drrsMnR3%2B9d0D1wBHSK9wQwSmqs52xFAw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> Andrés Osinski
>



-- 
Andrés Osinski

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2BxF-Pz%2BMoK-zZcfwMyB3YYAGvVPL9K8Zkpt8skW-oEyqD0nAA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Problem Django.19 upgrade and circular imports

2016-03-04 Thread Andres Osinski
The thing is that in my case, I have a model in the 'accounts' app which
inherits from a model in 'common', therefore I need the import to happen in
one's models.

I've tried this in Django 1.8 and I see the same behavior; therefore the
change occurs between the latest versions of 1.7 and 1.8. I'll try with
older versions in 1.8 and report back.

On Thu, Mar 3, 2016 at 10:11 PM, James Schneider 
wrote:

> On Thu, Mar 3, 2016 at 3:28 PM, Andres Osinski 
> wrote:
>
>> Hi everyone! I'm having an issue with circular imports in Django 1.9 that
>> I have not seen in my app, which had been running 1.7 before.
>>
>> I have a Django app in apps.common, which has its associated
>> apps.common.models too. No imports on __init__.py or anything of the sort.
>> When I my app in apps.account.models attempts to import
>> apps.common.models, it complains that said module does not exist.
>>
>> Putting a pdb set_trace() call at the top of apps.common.models and
>> apps.account.models shows that the former executes *before*
>> apps.account.models, so I fail to see how this could result in the posted
>> error.
>>
>> For the record, both apps use the new AppConfig scheme in their
>> corresponding apps.py file, and the import order in INSTALLED_APPS is:
>>
>> 'apps.common.CommonConfig',
>> 'apps.account.AccountConfig',
>> ...
>>
>> Has anyone else had a similar issue in this upgrade? Is there anything I
>> can do to diagnose the source of this?
>>
>
> Are you bringing in other functions in as part of your imports, or just
> other models? If you are only bringing in models for use with FK's and
> M2M's, then I would suggest referring to those models via their text path
> rather than using the Python class object directly. It avoids needing the
> import statement during the initial load (which avoids the circular import
> issue). See the end of this section about referring to models in other
> applications using a string rather than the real Python object (and make
> sure to remove the related import statements):
>
> https://docs.djangoproject.com/en/1.9/ref/models/fields/#foreignkey
>
> -James
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CA%2Be%2BciUWHXr6axqA5drrsMnR3%2B9d0D1wBHSK9wQwSmqs52xFAw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CA%2Be%2BciUWHXr6axqA5drrsMnR3%2B9d0D1wBHSK9wQwSmqs52xFAw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Andrés Osinski

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2BxF-Py5x%3DYb1okC_rwP3xV4moo9nTsHgw6c_-ZJ2geBinQ8rQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Problem Django.19 upgrade and circular imports

2016-03-03 Thread James Schneider
On Thu, Mar 3, 2016 at 3:28 PM, Andres Osinski 
wrote:

> Hi everyone! I'm having an issue with circular imports in Django 1.9 that
> I have not seen in my app, which had been running 1.7 before.
>
> I have a Django app in apps.common, which has its associated
> apps.common.models too. No imports on __init__.py or anything of the sort.
> When I my app in apps.account.models attempts to import
> apps.common.models, it complains that said module does not exist.
>
> Putting a pdb set_trace() call at the top of apps.common.models and
> apps.account.models shows that the former executes *before*
> apps.account.models, so I fail to see how this could result in the posted
> error.
>
> For the record, both apps use the new AppConfig scheme in their
> corresponding apps.py file, and the import order in INSTALLED_APPS is:
>
> 'apps.common.CommonConfig',
> 'apps.account.AccountConfig',
> ...
>
> Has anyone else had a similar issue in this upgrade? Is there anything I
> can do to diagnose the source of this?
>

Are you bringing in other functions in as part of your imports, or just
other models? If you are only bringing in models for use with FK's and
M2M's, then I would suggest referring to those models via their text path
rather than using the Python class object directly. It avoids needing the
import statement during the initial load (which avoids the circular import
issue). See the end of this section about referring to models in other
applications using a string rather than the real Python object (and make
sure to remove the related import statements):

https://docs.djangoproject.com/en/1.9/ref/models/fields/#foreignkey

-James

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciUWHXr6axqA5drrsMnR3%2B9d0D1wBHSK9wQwSmqs52xFAw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Problem Django.19 upgrade and circular imports

2016-03-03 Thread Tim Graham
No ideas off the top of my head. You could try bisecting to find the commit 
in Django where the behavior changed.

https://docs.djangoproject.com/en/dev/internals/contributing/triaging-tickets/#bisecting-a-regression

On Thursday, March 3, 2016 at 6:29:38 PM UTC-5, Andres Osinski wrote:
>
> Hi everyone! I'm having an issue with circular imports in Django 1.9 that 
> I have not seen in my app, which had been running 1.7 before.
>
> I have a Django app in apps.common, which has its associated 
> apps.common.models too. No imports on __init__.py or anything of the sort.
> When I my app in apps.account.models attempts to import 
> apps.common.models, it complains that said module does not exist.
>
> Putting a pdb set_trace() call at the top of apps.common.models and 
> apps.account.models shows that the former executes *before* 
> apps.account.models, so I fail to see how this could result in the posted 
> error.
>
> For the record, both apps use the new AppConfig scheme in their 
> corresponding apps.py file, and the import order in INSTALLED_APPS is:
>
> 'apps.common.CommonConfig',
> 'apps.account.AccountConfig',
> ...
>
> Has anyone else had a similar issue in this upgrade? Is there anything I 
> can do to diagnose the source of this?
>
> Thanks!
>
> -- 
> Andrés Osinski
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/44f5f6e6-771b-4ec2-8b29-91da0cc737cb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Problem Django.19 upgrade and circular imports

2016-03-03 Thread Andres Osinski
Hi everyone! I'm having an issue with circular imports in Django 1.9 that I
have not seen in my app, which had been running 1.7 before.

I have a Django app in apps.common, which has its associated
apps.common.models too. No imports on __init__.py or anything of the sort.
When I my app in apps.account.models attempts to import apps.common.models,
it complains that said module does not exist.

Putting a pdb set_trace() call at the top of apps.common.models and
apps.account.models shows that the former executes *before*
apps.account.models, so I fail to see how this could result in the posted
error.

For the record, both apps use the new AppConfig scheme in their
corresponding apps.py file, and the import order in INSTALLED_APPS is:

'apps.common.CommonConfig',
'apps.account.AccountConfig',
...

Has anyone else had a similar issue in this upgrade? Is there anything I
can do to diagnose the source of this?

Thanks!

-- 
Andrés Osinski

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2BxF-Pzmr2ikH5_UhC7jM50giVxftQ5%2BwihZkyR93TbbtWoyEw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django imports models for apps that aren't enabled?

2016-01-05 Thread Stodge
Carl, thanks. You were right - I was experiencing "b".

Thanks again
Mike

On Tuesday, 5 January 2016 11:02:24 UTC-5, Carl Meyer wrote:
>
> Hi Stodge, 
>
> On 01/05/2016 08:57 AM, Stodge wrote: 
> > I'm using Django 1.6.9. When the dev server (and hence Apache) starts, 
> > is Django supposed to import models for apps that exist on disk, but 
> > aren't enabled in the settings? I'm seeing this behaviour and I wasn't 
> > expecting it. Thanks 
>
> Django shouldn't do that on its own. AFAIK the only reason a models file 
> should be imported is a) because it's listed in INSTALLED_APPS, or b) 
> because something else that is imported imports it (possibly also c) an 
> installed model has an FK linking to it with a string reference?). 
>
> My first guess would be that you're seeing case (b). If you just add an 
> "assert False" at module-level in the models file in question, the 
> resulting traceback should show you the import chain leading to its 
> import. 
>
> Carl 
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a33e827f-59f6-4836-8713-d23ae6a3e6e9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django imports models for apps that aren't enabled?

2016-01-05 Thread Carl Meyer
Hi Stodge,

On 01/05/2016 08:57 AM, Stodge wrote:
> I'm using Django 1.6.9. When the dev server (and hence Apache) starts,
> is Django supposed to import models for apps that exist on disk, but
> aren't enabled in the settings? I'm seeing this behaviour and I wasn't
> expecting it. Thanks

Django shouldn't do that on its own. AFAIK the only reason a models file
should be imported is a) because it's listed in INSTALLED_APPS, or b)
because something else that is imported imports it (possibly also c) an
installed model has an FK linking to it with a string reference?).

My first guess would be that you're seeing case (b). If you just add an
"assert False" at module-level in the models file in question, the
resulting traceback should show you the import chain leading to its import.

Carl

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/568BE8AE.4040507%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.


signature.asc
Description: OpenPGP digital signature


Django imports models for apps that aren't enabled?

2016-01-05 Thread Stodge
I'm using Django 1.6.9. When the dev server (and hence Apache) starts, is 
Django supposed to import models for apps that exist on disk, but aren't 
enabled in the settings? I'm seeing this behaviour and I wasn't expecting 
it. Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/dec138b8-7851-471f-a42a-f67935768233%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: finding information on modules, imports, etc.

2015-11-09 Thread Gary Roach
Actually, I think I have been looking in the wrong place for my 
information. What I assumed was Django functions are actually Python 
functions. Most if the information I need is found in the Python web 
sites. Obviously, I'm a bit of a Nub all around. AFLE (Another xxx 
learning experience.)


Thanks for your reply

Gary R.

On 11/08/2015 11:19 AM, Gergely Polonkai wrote:


It's a bit unclear what you want to do. Could you write a use case? 
I'm afraid of an XY problem here…


On 8 Nov 2015 18:25, "Gary Roach" <mailto:gary719_li...@verizon.net>> wrote:


I can't seem to find a "dictionary" of modules? or what ever on
the use of:

import os or other possible imports

from django.contrib  import admin 

Maybe I'm using the wrong terminology but after a gazillion
searches, I still haven't found a decent reference.

Any help to this relatively new user would be sincerely appreciated.


Gary R.

-- 
You received this message because you are subscribed to the Google

Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it,
send an email to django-users+unsubscr...@googlegroups.com
<mailto:django-users%2bunsubscr...@googlegroups.com>.
To post to this group, send email to django-users@googlegroups.com
<mailto:django-users@googlegroups.com>.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit

https://groups.google.com/d/msgid/django-users/563F8575.1000208%40verizon.net.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google 
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to django-users+unsubscr...@googlegroups.com 
<mailto:django-users+unsubscr...@googlegroups.com>.
To post to this group, send email to django-users@googlegroups.com 
<mailto:django-users@googlegroups.com>.

Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CACczBULYKLmZ4bB%2BqDQAQqZjFPF2TN7ciyhhjBPb5k7TZSapDQ%40mail.gmail.com 
<https://groups.google.com/d/msgid/django-users/CACczBULYKLmZ4bB%2BqDQAQqZjFPF2TN7ciyhhjBPb5k7TZSapDQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.

For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5640B028.7080702%40verizon.net.
For more options, visit https://groups.google.com/d/optout.


Re: finding information on modules, imports, etc.

2015-11-08 Thread Gergely Polonkai
It's a bit unclear what you want to do. Could you write a use case? I'm
afraid of an XY problem here…
On 8 Nov 2015 18:25, "Gary Roach"  wrote:

> I can't seem to find a "dictionary" of modules? or what ever on the use of:
>
> import os or other possible imports
>
> from django.contrib  import admin  possibilities.>
>
> Maybe I'm using the wrong terminology but after a gazillion searches, I
> still haven't found a decent reference.
>
> Any help to this relatively new user would be sincerely appreciated.
>
>
> Gary R.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/563F8575.1000208%40verizon.net
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CACczBULYKLmZ4bB%2BqDQAQqZjFPF2TN7ciyhhjBPb5k7TZSapDQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


finding information on modules, imports, etc.

2015-11-08 Thread Gary Roach

I can't seem to find a "dictionary" of modules? or what ever on the use of:

import os or other possible imports

from django.contrib  import admin possibilities.>


Maybe I'm using the wrong terminology but after a gazillion searches, I 
still haven't found a decent reference.


Any help to this relatively new user would be sincerely appreciated.


Gary R.

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/563F8575.1000208%40verizon.net.
For more options, visit https://groups.google.com/d/optout.


Re: Does imports of related models affect cascade deletion?

2015-07-30 Thread Markus Amalthea Magnuson
I will fetch the actual piece of code and build a small test case out of
it, will reply again in this thread when I have some more info.

On Thu, Jul 30, 2015 at 6:39 PM, Marc Aymerich  wrote:

> On Thu, Jul 30, 2015 at 3:38 PM, Markus Amalthea Magnuson
>  wrote:
> > Hey,
> >
> > I stumbled upon a piece of code and a comment that says this:
> >
> > Deleting a model object that has related objects will only cascade delete
> > those objects if their models have been imported.
> >
> > Is this true? I have not found it in the documentation and would like to
> add
> > a reference to the code comment so others won't be as confused as I am.
>
> Is this models module called models.py and does it live inside a
> Django application listed in INSTALLED_APPS?
>
>
> --
> Marc
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Django users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/django-users/wLiEuzqUVeQ/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CA%2BDCN_tKE56qJPJ339uL0Zo%2BvEDSsPdTnLMODHC_3H4hd856dw%40mail.gmail.com
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAOXH8SXRVShSrLuPjZuPDPhbEEdc-Bi9v5JQm-xHf181iCzMKA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Does imports of related models affect cascade deletion?

2015-07-30 Thread Marc Aymerich
On Thu, Jul 30, 2015 at 3:38 PM, Markus Amalthea Magnuson
 wrote:
> Hey,
>
> I stumbled upon a piece of code and a comment that says this:
>
> Deleting a model object that has related objects will only cascade delete
> those objects if their models have been imported.
>
> Is this true? I have not found it in the documentation and would like to add
> a reference to the code comment so others won't be as confused as I am.

Is this models module called models.py and does it live inside a
Django application listed in INSTALLED_APPS?


-- 
Marc

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2BDCN_tKE56qJPJ339uL0Zo%2BvEDSsPdTnLMODHC_3H4hd856dw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Does imports of related models affect cascade deletion?

2015-07-30 Thread Tim Graham
No, import of related models shouldn't affect cascade deletion. Please open 
a bug report with steps to reproduce if that's actually the case. Possibly 
it could be a bug that's fixed by the app loading refactor in 1.7.

On Thursday, July 30, 2015 at 9:38:09 AM UTC-4, Markus Amalthea Magnuson 
wrote:
>
> Hey,
>
> I stumbled upon a piece of code and a comment that says this:
>
> Deleting a model object that has related objects will only cascade delete 
> those objects if their models have been imported.
>
> Is this true? I have not found it in the documentation and would like to 
> add a reference to the code comment so others won't be as confused as I am.
>
> Here's an illustration (from memory, disregard if code is not valid 
> python):
>
> mymodel.py:
>
> from django.db import models
>
> class MyModel(models.Model):
> foo = models.CharField()
> bar = models.CharField()
>
>
> my_other_model.py:
>
> from django.db import models
> from mymodel import MyModel
>
> class MyOtherModel(models.Model):
> baz = models.CharField()
> bar = models.ForeignKey(MyModel)
>
>
> some_source_file.py:
>
> from mymodel import MyModel
>
> # Without this line, deleting MyModel objects will not delete its related
> # MyOtherModel objects (?):
> from my_other_model import MyOtherModel
>
> obj = MyModel.objects.get(pk=123)
> obj.delete()
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b41ea21c-aafb-4268-8549-9337b0cf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Does imports of related models affect cascade deletion?

2015-07-30 Thread Markus Amalthea Magnuson
Hey,

I stumbled upon a piece of code and a comment that says this:

Deleting a model object that has related objects will only cascade delete 
those objects if their models have been imported.

Is this true? I have not found it in the documentation and would like to 
add a reference to the code comment so others won't be as confused as I am.

Here's an illustration (from memory, disregard if code is not valid python):

mymodel.py:

from django.db import models

class MyModel(models.Model):
foo = models.CharField()
bar = models.CharField()


my_other_model.py:

from django.db import models
from mymodel import MyModel

class MyOtherModel(models.Model):
baz = models.CharField()
bar = models.ForeignKey(MyModel)


some_source_file.py:

from mymodel import MyModel

# Without this line, deleting MyModel objects will not delete its related
# MyOtherModel objects (?):
from my_other_model import MyOtherModel

obj = MyModel.objects.get(pk=123)
obj.delete()

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/4991f097-ae07-4075-89c5-908df7b4ccf2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


broken imports in python3.3 + django 1.5

2013-04-26 Thread danny
Howdy,

In python 3.3 you no longer need __init__.py in directories to be
interpreted as package namespaces. In my source I have the following tree
src
  /apps
/index
/mapping

I removed the __init__.py files as I should be able to, but imports broke.
Specifically:

 
/opt/python3.3.1/lib/python3.3/site-packages/django/utils/translation/trans_real.py(155)_fetch()
154 app = import_module(appname)
2-> 155 apppath =
os.path.join(os.path.dirname(upath(app.__file__)), 'locale')

notice the app.__file__. In python 3.3 modules are not required to have a
__file__ attribute. 

So in particular, I was not able to execute
python3.3 manage.py shell because of the cryptic
*** AttributeError: 'module' object has no attribute '__file__'
which I traced to this line in the Django source.

I don't know if it appears anywhere else, but module.__file__ is no longer
robust in python 3.3.

The poor man's fix is to put the __init__.py's back in. 
src
  /apps
/index
  __init__.py
/mapping
  __init__.py

works just fine, but shouldn't be required.

This is on RHEL5.8, but I doubt that matters.

thanks,
Danny


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




Re: Django in Eclipse+PyDev, lot's of functions and imports are undefined/unresolved

2012-10-26 Thread Chris Pagnutti
Yeah, I'm quite sure the interpreter can see django.  I can verify it from 
the console, and the app runs fine, despite all the complaints, so I'm not 
too worried about it.  What does Project > Clean do?

On Friday, October 26, 2012 8:16:18 PM UTC-4, Andrew McHarg wrote:
>
> Have you verified that the python interpreter pydev is looking at has 
> django installed in it? That might account for it. Also try doing a Project 
> > Clean. That being said I have found that pydev's code analysis can be 
> pretty janky under a variety circumstances such as switching branches 
> outside the IDE.
>
> On Fri, Oct 26, 2012 at 5:05 PM, Chris Pagnutti 
> 
> > wrote:
>
>> It turns out that, although eclipse is complaining a lot, the site runs 
>> just fine.  I guess I'll just have to get used to ignoring eclipse 
>> complaints when working with django.  Thanks.
>>
>>
>> On Friday, October 26, 2012 12:48:35 PM UTC-4, Chris Pagnutti wrote:
>>>
>>> Hi.  I created a django project using the eric4 IDE.  I'm just trying to 
>>> transfer everything over to Eclipse+PyDev.  I basically just rebuilt the 
>>> whole project from scratch by copying and pasting all the source code.  I'm 
>>> pretty sure I set up my python interpreter fine.  I'm using django-trunk in 
>>> a virtual env.
>>>
>>> When I open the console in Eclipse, and can import django and it prints 
>>> the correct version number.  But my project, which worked fine in eric4, 
>>> makes Eclipse complain about unresolved imports and undefined variables. 
>>>  Here a list of a few that might give some clue as to what's wrong:
>>> from django.conf.urls import patterns, url
>>> from django.conf import settings
>>> from django.contrib import admin
>>>
>>> urlquote
>>> send_mail
>>> _user_has_perm
>>>
>>> among a whole bunch of others.
>>>
>>> I thought it might have something to do with the auto-reload, but I 
>>> followed this 
>>> http://pydev.org/manual_**adv_django.html<http://pydev.org/manual_adv_django.html>
>>>  (scroll 
>>> to bottom), but it still doesn't work.
>>>
>>> Any thoughts would be great.
>>>
>>>  -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msg/django-users/-/6zMdzvPEVAUJ.
>>
>> To post to this group, send email to django...@googlegroups.com
>> .
>> To unsubscribe from this group, send email to 
>> django-users...@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 view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/GVIr4qxgxFEJ.
To post to this group, send email to django-users@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.



Re: Django in Eclipse+PyDev, lot's of functions and imports are undefined/unresolved

2012-10-26 Thread Andrew McHarg
Have you verified that the python interpreter pydev is looking at has
django installed in it? That might account for it. Also try doing a Project
> Clean. That being said I have found that pydev's code analysis can be
pretty janky under a variety circumstances such as switching branches
outside the IDE.

On Fri, Oct 26, 2012 at 5:05 PM, Chris Pagnutti wrote:

> It turns out that, although eclipse is complaining a lot, the site runs
> just fine.  I guess I'll just have to get used to ignoring eclipse
> complaints when working with django.  Thanks.
>
>
> On Friday, October 26, 2012 12:48:35 PM UTC-4, Chris Pagnutti wrote:
>>
>> Hi.  I created a django project using the eric4 IDE.  I'm just trying to
>> transfer everything over to Eclipse+PyDev.  I basically just rebuilt the
>> whole project from scratch by copying and pasting all the source code.  I'm
>> pretty sure I set up my python interpreter fine.  I'm using django-trunk in
>> a virtual env.
>>
>> When I open the console in Eclipse, and can import django and it prints
>> the correct version number.  But my project, which worked fine in eric4,
>> makes Eclipse complain about unresolved imports and undefined variables.
>>  Here a list of a few that might give some clue as to what's wrong:
>> from django.conf.urls import patterns, url
>> from django.conf import settings
>> from django.contrib import admin
>>
>> urlquote
>> send_mail
>> _user_has_perm
>>
>> among a whole bunch of others.
>>
>> I thought it might have something to do with the auto-reload, but I
>> followed this 
>> http://pydev.org/manual_**adv_django.html<http://pydev.org/manual_adv_django.html>
>>  (scroll
>> to bottom), but it still doesn't work.
>>
>> Any thoughts would be great.
>>
>>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/6zMdzvPEVAUJ.
>
> To post to this group, send email to django-users@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-users@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.



Re: Django in Eclipse+PyDev, lot's of functions and imports are undefined/unresolved

2012-10-26 Thread Chris Pagnutti
It turns out that, although eclipse is complaining a lot, the site runs 
just fine.  I guess I'll just have to get used to ignoring eclipse 
complaints when working with django.  Thanks.

On Friday, October 26, 2012 12:48:35 PM UTC-4, Chris Pagnutti wrote:
>
> Hi.  I created a django project using the eric4 IDE.  I'm just trying to 
> transfer everything over to Eclipse+PyDev.  I basically just rebuilt the 
> whole project from scratch by copying and pasting all the source code.  I'm 
> pretty sure I set up my python interpreter fine.  I'm using django-trunk in 
> a virtual env.
>
> When I open the console in Eclipse, and can import django and it prints 
> the correct version number.  But my project, which worked fine in eric4, 
> makes Eclipse complain about unresolved imports and undefined variables. 
>  Here a list of a few that might give some clue as to what's wrong:
> from django.conf.urls import patterns, url
> from django.conf import settings
> from django.contrib import admin
>
> urlquote
> send_mail
> _user_has_perm
>
> among a whole bunch of others.
>
> I thought it might have something to do with the auto-reload, but I 
> followed this http://pydev.org/manual_adv_django.html (scroll to bottom), 
> but it still doesn't work.
>
> Any thoughts would be great.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/6zMdzvPEVAUJ.
To post to this group, send email to django-users@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.



Re: Django in Eclipse+PyDev, lot's of functions and imports are undefined/unresolved

2012-10-26 Thread Nick Santos
I don't have a solution, but I can indicate that the same thing has
happened to me on a standard install of Python 2.7.2 (no virtualenv) on
Windows 7, so I don't know that it's anything about your setup in
particular, if that helps.
-Nick


On Fri, Oct 26, 2012 at 9:48 AM, Chris Pagnutti wrote:

> Hi.  I created a django project using the eric4 IDE.  I'm just trying to
> transfer everything over to Eclipse+PyDev.  I basically just rebuilt the
> whole project from scratch by copying and pasting all the source code.  I'm
> pretty sure I set up my python interpreter fine.  I'm using django-trunk in
> a virtual env.
>
> When I open the console in Eclipse, and can import django and it prints
> the correct version number.  But my project, which worked fine in eric4,
> makes Eclipse complain about unresolved imports and undefined variables.
>  Here a list of a few that might give some clue as to what's wrong:
> from django.conf.urls import patterns, url
> from django.conf import settings
> from django.contrib import admin
>
> urlquote
> send_mail
> _user_has_perm
>
> among a whole bunch of others.
>
> I thought it might have something to do with the auto-reload, but I
> followed this http://pydev.org/manual_adv_django.html (scroll to bottom),
> but it still doesn't work.
>
> Any thoughts would be great.
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/kDtwvXBFp4cJ.
> To post to this group, send email to django-users@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-users@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.



Django in Eclipse+PyDev, lot's of functions and imports are undefined/unresolved

2012-10-26 Thread Chris Pagnutti
Hi.  I created a django project using the eric4 IDE.  I'm just trying to 
transfer everything over to Eclipse+PyDev.  I basically just rebuilt the 
whole project from scratch by copying and pasting all the source code.  I'm 
pretty sure I set up my python interpreter fine.  I'm using django-trunk in 
a virtual env.

When I open the console in Eclipse, and can import django and it prints the 
correct version number.  But my project, which worked fine in eric4, makes 
Eclipse complain about unresolved imports and undefined variables.  Here a 
list of a few that might give some clue as to what's wrong:
from django.conf.urls import patterns, url
from django.conf import settings
from django.contrib import admin

urlquote
send_mail
_user_has_perm

among a whole bunch of others.

I thought it might have something to do with the auto-reload, but I 
followed this http://pydev.org/manual_adv_django.html (scroll to bottom), 
but it still doesn't work.

Any thoughts would be great.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/kDtwvXBFp4cJ.
To post to this group, send email to django-users@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.



Re: A single file that contains common django imports

2012-07-06 Thread Russell Keith-Magee
On Sat, Jul 7, 2012 at 3:29 AM, Scott Woodall  wrote:
> Would there be any issues with creating a single file contains common django
> modules I use, then importing that file into my views, models etc.
>
> Example:
>
> common_imports.py
> ===
> from django.conf import settings
> from django.core.urlresolvers import reverse
> from django.shortcuts import render_to_response
> 
>
>
> Then in any other file I need to import django stuff into, I just import my
> common_imports.py
>
> views.py
> ===
> from my_project.common_imports import *
>
>
> This saves me from having to retype all the django imports repeatedly atop
> my files. That and I can never remember the paths so I'm always hitting the
> docs looking up where to import stuff from.
>
> I'm just looking for the downside of this pattern.

Will it work? Sure. Will it be a massive performance hit, or anything
like that? No. There might be some problems with module-level state,
but that's hard to predict without knowing exactly which modules
you're using.

However, regardless of any *technical* problems, I'd advise against it
as bad engineering practice.

Lets say you go down this path, and come up with whatever you consider
to be a 'common subset', and put that in your common_imports list.

Now a new engineer joins your project. You say you're having trouble
remembering the import paths for all the Django bits; you've just put
exactly the same mental load on your new team member. They need to
know what is and what isn't in the common imports module. Django's
import paths are common across every Django project. The contents of
your "common" package isn't. So while common imports might be
convenient to you, they won't necessarily be for anyone else. Django's
import paths are numerous, but they are mostly sensible, so they *can*
be memorised with time, and most experienced Django engineers would
know the common imports really well. By adding the import abstraction,
you've just made it harder (not immensely harder, but a little harder)
for a random individual with experience to join your project.

Now you realize that HttpNotAuthorized is something you're starting to
use a lot, so you put that into your common_imports list. Except that
some of your views already have HttpNotAuthorized imported. So you've
got duplicated imports, and you need to do an audit of your codebase
to find all the places where you've got older explicit imports of
HttpNotAuthorized.

Now you decide to use linting tools as part of your development stack.
Your wrapped imports will make it harder for those linters to do their
job, and they'll probably miss some easy-to-prevent errors.

For me, it comes down to the Zen of Python. TZoP advises that explicit
is better than implicit; explicitly naming each import rather than an
implicit group import is a great example of this adage in practice,
IMHO. Yes, this means there's a learning curve where you have to
memorise the import paths for all the common Django bits; but in the
long run, I don't think it will take as long to get familiar with
these as you fear.

Yours,
Russ Magee %-)

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



A single file that contains common django imports

2012-07-06 Thread Scott Woodall
Would there be any issues with creating a single file contains common 
django modules I use, then importing that file into my views, models etc.

Example:

common_imports.py
===
from django.conf import settings
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response



Then in any other file I need to import django stuff into, I just import my 
common_imports.py

views.py
===
from my_project.common_imports import *


This saves me from having to retype all the django imports repeatedly atop 
my files. That and I can never remember the paths so I'm always hitting the 
docs looking up where to import stuff from.

I'm just looking for the downside of this pattern.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/0Xw45PHZBkAJ.
To post to this group, send email to django-users@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.



Re: Django Snippet imports?

2012-02-03 Thread msbuck
Yes you need to put register = template.Library() in your module. See
https://docs.djangoproject.com/en/1.3/howto/custom-template-tags/ for
more info on where this code should reside.

On Feb 3, 9:17 am, Jesramz  wrote:
> source:http://djangosnippets.org/snippets/847/
>
> from django.contrib.auth.models import User, Group
> from django.utils.encoding import force_unicode
> from django import template
>
> Missing anything?
> Would I have to use 'register = template.Library()' ?

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Django Snippet imports?

2012-02-03 Thread Jesramz
source: http://djangosnippets.org/snippets/847/

from django.contrib.auth.models import User, Group
from django.utils.encoding import force_unicode
from django import template

Missing anything?
Would I have to use 'register = template.Library()' ?



-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Django Snippet imports?

2012-02-02 Thread Jesramz
What would I have to import to use the following snippet (source:
http://djangosnippets.org/snippets/847/)?

@register.filter
def in_group(user, groups):
"""Returns a boolean if the user is in the given group, or comma-
separated
list of groups.

Usage::

{% if user|in_group:"Friends" %}
...
{% endif %}

or::

{% if user|in_group:"Friends,Enemies" %}
...
{% endif %}

"""
group_list = force_unicode(groups).split(',')
return
bool(user.groups.filter(name__in=group_list).values('name'))

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



RE: Question about imports..Python Dejango and Models

2011-08-06 Thread Hayyan Rafiq

Thanks guys ... for clarifying that up..

> Date: Sat, 6 Aug 2011 01:50:21 -0700
> Subject: Re: Question about imports..Python Dejango and Models
> From: andrew.mark.sc...@gmail.com
> To: django-users@googlegroups.com
> 
> Hi,
> 
> models.Model is django.db.models.base.Model and is defined in django/
> db/models/base.py.
> 
> Have a look at django/db/__init__.py (in your local Django
> installation or 
> https://code.djangoproject.com/browser/django/tags/releases/1.3)
> and http://docs.python.org/tutorial/modules.html.
> 
> Regards,
> Andrew
> 
> On Aug 6, 11:38 am, Hayyan Rafiq  wrote:
> > are  you referring to the models.py which is present in every app created 
> > by python manage.py startapp books ??
> >
> > # Create your models here.
> > from django.db import models
> >
> > class Publisher(models.Model):
> > name = models.CharField(max_length=30)
> > address = models.CharField(max_length=50)
> > city = models.CharField(max_length=60)
> > state_province = models.CharField(max_length=30)
> > country = models.CharField(max_length=50)
> > website = models.URLField()
> >
> > class Author(models.Model):
> > first_name = models.CharField(max_length=30)
> > last_name = models.CharField(max_length=40)
> > email = models.EmailField()
> >
> > class Book(models.Model):
> > title = models.CharField(max_length=100)
> > authors = models.ManyToManyField(Author)
> > publisher = models.ForeignKey(Publisher)
> > publication_date = models.DateField()
> >
> >  "I meant Models is defined inside models.py. ??" .. still not clear
> >
> > Date: Fri, 5 Aug 2011 21:33:26 -0400
> > Subject: Re: RE: Question about imports..Python Dejango and Models
> > From: sh...@milochik.com
> > To: django-users@googlegroups.com
> >
> > Sorry, typo. I meant Models is defined inside models.py.
> >
> > --
> >
> > 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+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-users@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-users@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.



Re: Question about imports..Python Dejango and Models

2011-08-06 Thread Andrew
Hi,

models.Model is django.db.models.base.Model and is defined in django/
db/models/base.py.

Have a look at django/db/__init__.py (in your local Django
installation or https://code.djangoproject.com/browser/django/tags/releases/1.3)
and http://docs.python.org/tutorial/modules.html.

Regards,
Andrew

On Aug 6, 11:38 am, Hayyan Rafiq  wrote:
> are  you referring to the models.py which is present in every app created by 
> python manage.py startapp books ??
>
> # Create your models here.
> from django.db import models
>
> class Publisher(models.Model):
>     name = models.CharField(max_length=30)
>     address = models.CharField(max_length=50)
>     city = models.CharField(max_length=60)
>     state_province = models.CharField(max_length=30)
>     country = models.CharField(max_length=50)
>     website = models.URLField()
>
> class Author(models.Model):
>     first_name = models.CharField(max_length=30)
>     last_name = models.CharField(max_length=40)
>     email = models.EmailField()
>
> class Book(models.Model):
>     title = models.CharField(max_length=100)
>     authors = models.ManyToManyField(Author)
>     publisher = models.ForeignKey(Publisher)
>     publication_date = models.DateField()
>
>  "I meant Models is defined inside models.py. ??" .. still not clear
>
> Date: Fri, 5 Aug 2011 21:33:26 -0400
> Subject: Re: RE: Question about imports..Python Dejango and Models
> From: sh...@milochik.com
> To: django-users@googlegroups.com
>
> Sorry, typo. I meant Models is defined inside models.py.
>
> --
>
> 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+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-users@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.



Re: Question about imports..Python Dejango and Models

2011-08-06 Thread Shawn Milochik
2011/8/6 Rafael Durán Castañeda :
> I'm not a django expert but I think you are wrong. Checking my django
> installation:
>



Yep, you're right.

https://code.djangoproject.com/browser/django/trunk/django/db/models/base.py

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Question about imports..Python Dejango and Models

2011-08-06 Thread bruno desthuilliers
On 6 août, 03:04, Adam Zedan  wrote:
> Hi while going through the Django Book I ran into the following code
> Being fairly new to Python the code kind of confused me

Python-related questions ought to posted on comp.lang.python... But
anyway:

> from django.db import models     --->lineA
> class Publisher(models.Model):  --->line B
>
> Consider line A
>  from django.db import models
> This means that in the django directory therewill be a subdirectory of
> db from in which either you'll find a file called models.py or a
> directory called models??Rite??

This means: import the object  named 'models' (whatever it happens to
be) from the module or package named "django.db" and bound it to the
name "models" in the current namespace.

In this case, "django.db.models" happens to be a package (technically,
a directory containing an __init__.py file).

>
> Moving on to Line B
> class Publisher(models.Model):
> i am more interested in the models.Model.
> does that mean in the models folder youll find a Model.py ??

No. That means that there's something (actually a class object) bound
to the name "Model" in the django.db.models package's namespace.

> I cant
> seem to find Model.py then?? could anyone please clear this
> concept..why cant i find this file ?? I would like to take a look at
> the base class models.Model.

Read the django/db/models/__init__.py file and you'll find out that
the Model base class is defined in django/db/models/base.py.

> I have checked a couple of sites on python tutorial and they dont
> cover imports in that much detail

You obviously forgot to start with the official tutorial. This is
covered in "modules and packages" part:
http://docs.python.org/tutorial/modules.html

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Question about imports..Python Dejango and Models

2011-08-06 Thread Rafael Durán Castañeda
I'm not a django expert but I think you are wrong. Checking my django 
installation:


- models is package not a python module
- __init__.py from this file contains:
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured
from django.db import connection
from django.db.models.loading import get_apps, get_app, get_models, 
get_model, register_models

from django.db.models.query import Q
from django.db.models.expressions import F
from django.db.models.manager import Manager
_from django.db.models.base import Model_
from django.db.models.aggregates import *
from django.db.models.fields import *
from django.db.models.fields.subclassing import SubfieldBase
from django.db.models.fields.files import FileField, ImageField
from django.db.models.fields.related import ForeignKey, OneToOneField, 
ManyToManyField, ManyToOneRel, ManyToManyRel, OneToOneRel

from django.db.models import signals
- So Model is /django_path/django/db/models/base.py

On 06/08/11 03:33, Shawn Milochik wrote:


Sorry, typo. I meant Models is defined inside models.py.

--
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+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-users@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.



RE: Question about imports..Python Dejango and Models

2011-08-05 Thread Hayyan Rafiq

are  you referring to the models.py which is present in every app created by 
python manage.py startapp books ??

# Create your models here.
from django.db import models

class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()

class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()

class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()


 "I meant Models is defined inside models.py. ??" .. still not clear

Date: Fri, 5 Aug 2011 21:33:26 -0400
Subject: Re: RE: Question about imports..Python Dejango and Models
From: sh...@milochik.com
To: django-users@googlegroups.com

Sorry, typo. I meant Models is defined inside models.py. 



-- 

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+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-users@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.



Re: RE: Question about imports..Python Dejango and Models

2011-08-05 Thread Shawn Milochik
Sorry, typo. I meant Models is defined inside models.py.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



RE: Question about imports..Python Dejango and Models

2011-08-05 Thread Hayyan Rafiq

wait... every app has its own models.py...  and in models.py it says something 
like

from django.db import models
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()

I need Models.py where is that?? 

Date: Fri, 5 Aug 2011 21:15:01 -0400
Subject: Re: Question about imports..Python Dejango and Models
From: sh...@milochik.com
To: django-users@googlegroups.com

The model class is defined *inside* Models.py.



-- 

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+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-users@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.



Re: Question about imports..Python Dejango and Models

2011-08-05 Thread Shawn Milochik
The model class is defined *inside* Models.py.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Question about imports..Python Dejango and Models

2011-08-05 Thread Adam Zedan
Hi while going through the Django Book I ran into the following code
Being fairly new to Python the code kind of confused me

from django.db import models --->lineA
class Publisher(models.Model):  --->line B

Consider line A
 from django.db import models
This means that in the django directory therewill be a subdirectory of
db from in which either you'll find a file called models.py or a
directory called models??Rite?? so there is a directory called
models..great Let me know if what i said was correct.

Moving on to Line B
class Publisher(models.Model):
i am more interested in the models.Model.
does that mean in the models folder youll find a Model.py ??  I cant
seem to find Model.py then?? could anyone please clear this
concept..why cant i find this file ?? I would like to take a look at
the base class models.Model.
I have checked a couple of sites on python tutorial and they dont
cover imports in that much detail

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Question about using imports within functions/methods

2011-06-02 Thread Cal Leeming [Simplicity Media Ltd]
On Thu, Jun 2, 2011 at 4:46 PM, Shawn Milochik  wrote:

> On 06/02/2011 11:09 AM, Cal Leeming [Simplicity Media Ltd] wrote:
>
>>
>> Pretty shameful that after almost 6 years of (fairly advanced) Python
>> coding, I still finding myself not knowing some of the basic 101 stuff..
>> Slightly off topic but, does anyone else have that issue when coding? (i.e.
>> doing really complex code, but forgetting/not knowing some of the real basic
>> stuff).
>>
>>
> Yeah, I run into that from time to time. What I do to remedy it is read
> books. Especially ones like the Python Essential Reference by David Beazley.
> Just scan through it in the bath or at breakfast or whatever, and you'll
> find a handy module you never knew existed, or a wonderful new feature in
> something you use all the time.
>
> I'm anxiously anticipating Doug Hellmann's new book on the standard
> library, which should ship sometime this month.
>http://amzn.com/0321767349
> From what I've read in his PyMoTW posts and seen in his PyCon presentation,
> this book will make my Python skills even better.
>

Sounds like a good idea, this is going in my todo list :)


>
> I suspect that most working developers are self-taught, in that they didn't
> learn the technology they work with from the ground up in a classroom --
> they picked it up as they went along. That's what I blame for the gaps in
> our knowledge. I don't think these books are the best place to start with
> Python, but they're a great way to sharpen the axe.
>

Yeah I'm completely self taught too, although it does mean my knowledge is
somewhat sparse. I think your above suggestion is a good way to tackle that
problem though. I guess it's like any job, every so often, you need to have
"refreshers" to make sure you are keeping up to scratch.


>
> Shawn
>
>
> --
> 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+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-users@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.



Re: Question about using imports within functions/methods

2011-06-02 Thread Shawn Milochik

On 06/02/2011 11:09 AM, Cal Leeming [Simplicity Media Ltd] wrote:


Pretty shameful that after almost 6 years of (fairly advanced) Python 
coding, I still finding myself not knowing some of the basic 101 
stuff.. Slightly off topic but, does anyone else have that issue when 
coding? (i.e. doing really complex code, but forgetting/not knowing 
some of the real basic stuff).




Yeah, I run into that from time to time. What I do to remedy it is read 
books. Especially ones like the Python Essential Reference by David 
Beazley. Just scan through it in the bath or at breakfast or whatever, 
and you'll find a handy module you never knew existed, or a wonderful 
new feature in something you use all the time.


I'm anxiously anticipating Doug Hellmann's new book on the standard 
library, which should ship sometime this month.

http://amzn.com/0321767349
From what I've read in his PyMoTW posts and seen in his PyCon 
presentation, this book will make my Python skills even better.


I suspect that most working developers are self-taught, in that they 
didn't learn the technology they work with from the ground up in a 
classroom -- they picked it up as they went along. That's what I blame 
for the gaps in our knowledge. I don't think these books are the best 
place to start with Python, but they're a great way to sharpen the axe.


Shawn

--
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Question about using imports within functions/methods

2011-06-02 Thread Cal Leeming [Simplicity Media Ltd]
Thanks to Tom / Matias / Brian for taking the time to reply, and for
providing some good links, this was *exactly* what I needed.

In regards to circular import issues, this is pretty much the main reason
I've put imports inside function/method before, and I agree that most of the
time it can be resolved with re-organization.

Pretty shameful that after almost 6 years of (fairly advanced) Python
coding, I still finding myself not knowing some of the basic 101 stuff..
Slightly off topic but, does anyone else have that issue when coding? (i.e.
doing really complex code, but forgetting/not knowing some of the real basic
stuff).

On Thu, Jun 2, 2011 at 3:56 PM, Brian Bouterse  wrote:

> Yes relative imports are explicitly discouraged in PEP 
> 8<http://www.python.org/dev/peps/pep-0008/>.
>  Look for the imports section.
>
> I do not recommend importing at runtime which is what you are doing when
> you put imports inside function/methods.  There are performance problems
> when calling imports repeatedly.  One possible method is to use a lazy
> import paradigm described at the end of this section on import 
> overhead<http://wiki.python.org/moin/PythonSpeed/PerformanceTips#Import_Statement_Overhead>
> .
>
> The only reason I think it is ok to do imports inside function/method code
> is if you don't know what you need to import until runtime, but in those
> cases you wouldn't use the standard "import foo" syntax, since you would
> need to dynamically choose what to import.
>
> Another reason folks hide imports inside function/method code is to solve
> circular import issues.  This is a consistently unproductive solution and
> will create problems over time through hidden import dependencies that
> aren't discovered until runtime.  circular imports can almost always be
> resolved through some code and package reorganization.  I like this
> article <http://effbot.org/zone/import-confusion.htm> on how import works
> in python.
>
> Brian
>
>
>
> On Thu, Jun 2, 2011 at 10:39 AM, Cal Leeming [Simplicity Media Ltd] <
> cal.leem...@simplicitymedialtd.co.uk> wrote:
>
>> Hey guys,
>>
>> This is more of a python question, than a Django specific one, but it's
>> been bugging me for years now lol.
>>
>> I'm trying to figure out if it is better to do imports at the top of the
>> file, or within the function/methods themselves.
>>
>> I guess the questions I'm asking are:
>>
>>- Is there any (serious) performance issues by doing imports every
>>time the function/method is called
>>- Is there a preferred style / guideline when it comes to imports?
>>(maybe a PEP somewhere?)
>>- Are relative imports discouraged? (from .. import package)
>>
>> Obviously, the answers would depend on different scenarios, so I'm looking
>> for a discussion really, rather than a set in stone answer.
>>
>> Example 1:
>> import os
>> def test():
>>print os.uname()
>> def test2():
>>print os.uname()
>> test()
>> test2()
>>
>> Example 2:
>> import os
>> class test:
>>def test(self):
>>print os.uname()
>>
>>
>> Example 2:
>> class test:
>>def test(self):
>>import os
>>print os.uname()
>>def test2(self):
>>import os
>>print os.uname()
>>
>> t = test()
>> t.test()
>> t.test2()
>>
>>  --
>> 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+unsubscr...@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+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-users@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.



Re: Question about using imports within functions/methods

2011-06-02 Thread Matías Aguirre
Imports are cached by python that way things get imported only the first time.

Try this simple test, create a file a.py with this content:
print "Importing a.py"

Then create b.py with this other content:
import a

Then create c.py with this other content:
def x():
import a
pass

Open a python interpreter and do:
>>> import b

It should print "Importing a.py", do the import again, it shouldn't print
anything. Now close the interpreter and start a new one and do:
>>> import c
>>> c.x()
>>> c.x()
It should print "Importing a.py" only once too.

Anyway, I would suggest you to follow PEP8 guideline 
http://www.python.org/dev/peps/pep-0008/
and leave function level import for cases where recursive imports is an issue.

Good luck,
Matías

Excerpts from Cal Leeming [Simplicity Media Ltd]'s message of Thu Jun 02 
11:39:14 -0300 2011:
> Hey guys,
> 
> This is more of a python question, than a Django specific one, but it's been
> bugging me for years now lol.
> 
> I'm trying to figure out if it is better to do imports at the top of the
> file, or within the function/methods themselves.
> 
> I guess the questions I'm asking are:
> 
>- Is there any (serious) performance issues by doing imports every time
>the function/method is called
>- Is there a preferred style / guideline when it comes to imports? (maybe
>a PEP somewhere?)
>- Are relative imports discouraged? (from .. import package)
> 
> Obviously, the answers would depend on different scenarios, so I'm looking
> for a discussion really, rather than a set in stone answer.
> 
> Example 1:
> import os
> def test():
>print os.uname()
> def test2():
>print os.uname()
> test()
> test2()
> 
> Example 2:
> import os
> class test:
>def test(self):
>print os.uname()
> 
> 
> Example 2:
> class test:
>def test(self):
>import os
>print os.uname()
>def test2(self):
>import os
>print os.uname()
> 
> t = test()
> t.test()
> t.test2()
> 
-- 
Matías Aguirre 

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Question about using imports within functions/methods

2011-06-02 Thread Tom Evans
On Thu, Jun 2, 2011 at 3:39 PM, Cal Leeming [Simplicity Media Ltd]
 wrote:
> Hey guys,
>
> This is more of a python question, than a Django specific one, but it's been
> bugging me for years now lol.
>
> I'm trying to figure out if it is better to do imports at the top of the
> file, or within the function/methods themselves.
> I guess the questions I'm asking are:
>
> Is there any (serious) performance issues by doing imports every time the
> function/method is called
> Is there a preferred style / guideline when it comes to imports? (maybe a
> PEP somewhere?)
> Are relative imports discouraged? (from .. import package)
>

To quickly answer your Qs:

Yes, there is a slight cost in importing modules. Putting the import
in the function adds an infinitesimal cost to the function.
Imports are discussed in PEP 8 and relative imports in PEP 328.
Relative imports are strongly discouraged.

Cheers

Tom

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Question about using imports within functions/methods

2011-06-02 Thread Brian Bouterse
Yes relative imports are explicitly discouraged in PEP
8<http://www.python.org/dev/peps/pep-0008/>.
 Look for the imports section.

I do not recommend importing at runtime which is what you are doing when you
put imports inside function/methods.  There are performance problems when
calling imports repeatedly.  One possible method is to use a lazy import
paradigm described at the end of this section on import
overhead<http://wiki.python.org/moin/PythonSpeed/PerformanceTips#Import_Statement_Overhead>
.

The only reason I think it is ok to do imports inside function/method code
is if you don't know what you need to import until runtime, but in those
cases you wouldn't use the standard "import foo" syntax, since you would
need to dynamically choose what to import.

Another reason folks hide imports inside function/method code is to solve
circular import issues.  This is a consistently unproductive solution and
will create problems over time through hidden import dependencies that
aren't discovered until runtime.  circular imports can almost always be
resolved through some code and package reorganization.  I like this
article<http://effbot.org/zone/import-confusion.htm>on how import
works in python.

Brian



On Thu, Jun 2, 2011 at 10:39 AM, Cal Leeming [Simplicity Media Ltd] <
cal.leem...@simplicitymedialtd.co.uk> wrote:

> Hey guys,
>
> This is more of a python question, than a Django specific one, but it's
> been bugging me for years now lol.
>
> I'm trying to figure out if it is better to do imports at the top of the
> file, or within the function/methods themselves.
>
> I guess the questions I'm asking are:
>
>- Is there any (serious) performance issues by doing imports every time
>the function/method is called
>- Is there a preferred style / guideline when it comes to imports?
>(maybe a PEP somewhere?)
>- Are relative imports discouraged? (from .. import package)
>
> Obviously, the answers would depend on different scenarios, so I'm looking
> for a discussion really, rather than a set in stone answer.
>
> Example 1:
> import os
> def test():
>print os.uname()
> def test2():
>print os.uname()
> test()
> test2()
>
> Example 2:
> import os
> class test:
>def test(self):
>print os.uname()
>
>
> Example 2:
> class test:
>def test(self):
>import os
>print os.uname()
>def test2(self):
>import os
>print os.uname()
>
> t = test()
> t.test()
> t.test2()
>
>  --
> 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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Question about using imports within functions/methods

2011-06-02 Thread Cal Leeming [Simplicity Media Ltd]
Hey guys,

This is more of a python question, than a Django specific one, but it's been
bugging me for years now lol.

I'm trying to figure out if it is better to do imports at the top of the
file, or within the function/methods themselves.

I guess the questions I'm asking are:

   - Is there any (serious) performance issues by doing imports every time
   the function/method is called
   - Is there a preferred style / guideline when it comes to imports? (maybe
   a PEP somewhere?)
   - Are relative imports discouraged? (from .. import package)

Obviously, the answers would depend on different scenarios, so I'm looking
for a discussion really, rather than a set in stone answer.

Example 1:
import os
def test():
   print os.uname()
def test2():
   print os.uname()
test()
test2()

Example 2:
import os
class test:
   def test(self):
   print os.uname()


Example 2:
class test:
   def test(self):
   import os
   print os.uname()
   def test2(self):
   import os
   print os.uname()

t = test()
t.test()
t.test2()

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: mutual imports - how to do it?

2011-04-18 Thread Kenneth Gonsalves
On Tue, 2011-04-19 at 09:46 +0530, Kenneth Gonsalves wrote:
> > from django.db.models import get_model
> > 
> > Product = get_model('products', 'Product')
> > Something = get_model('incident', 'Something') 
> 
> that simple? will try. 

worked. Thanks.
-- 
regards
KG
http://lawgon.livejournal.com
Coimbatore LUG rox
http://ilugcbe.techstud.org/

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: mutual imports - how to do it?

2011-04-18 Thread Kenneth Gonsalves
On Tue, 2011-04-19 at 00:36 -0300, Fabian Ezequiel Gallina wrote:
> > In incident I can do: from products.models import Product
> > but in products if I do:
> > from incident.import Something - this fails and django says cannot
> > import Something.
> >
> > Is there any way around this?
> 
> from django.db.models import get_model
> 
> Product = get_model('products', 'Product')
> Something = get_model('incident', 'Something') 

that simple? will try.
-- 
regards
KG
http://lawgon.livejournal.com
Coimbatore LUG rox
http://ilugcbe.techstud.org/

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: mutual imports - how to do it?

2011-04-18 Thread Fabian Ezequiel Gallina
2011/4/18 Kenneth Gonsalves :
> hi,
>
> I have two models.py files in two applications. Say, products.models and
> incident.models.
>
> In incident I can do: from products.models import Product
> but in products if I do:
> from incident.import Something - this fails and django says cannot
> import Something.
>
> Is there any way around this?

from django.db.models import get_model

Product = get_model('products', 'Product')
Something = get_model('incident', 'Something')



Regards,
-- 
Fabián E. Gallina
http://www.anue.biz

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



mutual imports - how to do it?

2011-04-18 Thread Kenneth Gonsalves
hi,

I have two models.py files in two applications. Say, products.models and
incident.models.

In incident I can do: from products.models import Product
but in products if I do:
from incident.import Something - this fails and django says cannot
import Something.

Is there any way around this?
-- 
regards
KG
http://lawgon.livejournal.com
Coimbatore LUG rox
http://ilugcbe.techstud.org/

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: celery tasks and circular imports

2011-03-08 Thread Bill Freeman
The (major part of the work of) import is NOT done every time through.  On
subsequent passes, the import statement merely looks up the module in
sys.modules.  This is only slightly more of a performance hit than a direct
dictionary look up (depending on how much the compiler inlines when it
sees an import statement) - that is, it's not worth worrying about.

It is often possible to refactor code so that these techniques aren't necessary,
for example, breaking one module, A, into A1 and A2 modules, where A1
has no code that depends on B, but does have what B needs from A (so
both A2 and B import A1).  But this often obfuscates the code.  Readable
code is always a priority for me.

Bill

On Mon, Mar 7, 2011 at 9:55 AM, Brian Bouterse  wrote:
> I too "protect" code from being interpreted at import time by putting import
> statements directly in the tasks that need them.  I don't think there is
> anything wrong with this approach the from a Python perspective other than
> possibly these:
> * imports are parsed at runtime and thus an import occurs for each time the
> code is run    <=== performance problem
> * import errors aren't discovered until runtime
> I too am looking for a better way to do this.
> Brian
> On Mon, Mar 7, 2011 at 8:25 AM, Shawn Milochik  wrote:
>>
>> If there's a "correct" way to do this I'd love to hear it also.
>>
>> At present I do the model imports in tasks.py within the task that
>> requires the model. This is because in my models.py I import tasks
>> from tasks.py.
>>
>> Shawn
>>
>> --
>> 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+unsubscr...@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+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-users@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.



Re: celery tasks and circular imports

2011-03-07 Thread Brian Bouterse
I too "protect" code from being interpreted at import time by putting import
statements directly in the tasks that need them.  I don't think there is
anything wrong with this approach the from a Python perspective other than
possibly these:

* imports are parsed at runtime and thus an import occurs for each time the
code is run<=== performance problem
* import errors aren't discovered until runtime

I too am looking for a better way to do this.

Brian

On Mon, Mar 7, 2011 at 8:25 AM, Shawn Milochik  wrote:

> If there's a "correct" way to do this I'd love to hear it also.
>
> At present I do the model imports in tasks.py within the task that
> requires the model. This is because in my models.py I import tasks
> from tasks.py.
>
> Shawn
>
> --
> 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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: celery tasks and circular imports

2011-03-07 Thread Shawn Milochik
If there's a "correct" way to do this I'd love to hear it also.

At present I do the model imports in tasks.py within the task that
requires the model. This is because in my models.py I import tasks
from tasks.py.

Shawn

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



celery tasks and circular imports

2011-03-07 Thread cerberos
I have tasks.py that imports models to access the managers. I also
want to import a task in a custom save method of one of my models but
can't as this creates a circular import.

I'm happy to use signals instead hover I don't really know where to
register them apart from models.py (which would also create a circular
import).

Russel Keith Magee said "Django 1.3 will probably introduce a third
option -- a reliable place
to register signals" [1] however the release notes [2] don't make any
mention of this.

The best thing I can think of at the moment is adding the import in
the custom save method, this seems like it could be a common problem -
there must be a better solution?

[1]
http://groups.google.com/group/django-users/browse_thread/thread/39bd9c6c1c25aad2/6ea0ca4a21e72bde?lnk=gst&q=circular+import#6ea0ca4a21e72bde
[2] http://docs.djangoproject.com/en/dev/releases/1.3/

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



lazy imports for cyclic imports prevention

2010-02-23 Thread Itay Donenhirsch
Hi folks,
in order to prevent cyclic import problems i've written this class:


class lazy_import:

def __init__(self, module, method = None ):
if method:
self.module = module
self.method = method
else:
tokens = module.split('.')
assert len(tokens) > 1
self.module = '.'.join( tokens[:-1] )
self.method = tokens[-1]

def __call__(self,*a,**kw):
return __import__( self.module, fromlist = self.method
).__getattribute__( self.method ).__call__( *a, **kw)


you can use it, for example, by:
some_func = lazy_import ('module.func')
some_func( bla )

my question is: i know django already does this somehow (in settings and
urlconf), but i couldn't find were it defines this. somebody help
?

thanks
itay

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



Re: Imports, works from directory but not from file

2009-11-12 Thread Bill Freeman
On Thu, Nov 12, 2009 at 10:33 AM, andreas schmid  wrote:
>>
> im on a unix system and i can import it on the django shell but it makes
> troubles when running the django server which doesnt make sense


It may be that it won't be recognized as an app because the app
directory doesn't
have a models.py file in it.  The app logic decides that an app listed
in settings.py
INSTALLED_APPS has a models.py.  I don't know off hand whether the test is made
by looking for that name in the directory, in which case your
configuration would fail,
by doing "import app.models" to see if it gets an exception, in which
case your code
passes, but your models wouldn't be available unless you put stuff in
app/models/__init__.py to import your other models files.

Bill

--

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




Re: Imports, works from directory but not from file

2009-11-12 Thread rebus_
I am not sure if i got the problem right, but this is what i think you could do.

If you want to keep your views in directory:

app/
   views/
  __init__.py <- This is where you keep your views so you don't
even need to change your urls.py or anthing
  something_else.py <- something else

Also you can keep your models in models/__init__.py and don't need to
change any imports, like:

app/
   models/
  __init__.py <- this is where your keep your models

On the other hand you can have

app/
   models/
  other_models.py <- this is where the models are for example
  __init__.py <- this is where you import your models so they can
be imported

in __init__.py you say
from other_models import * (or Model1, Model2 etc)

And where every you used your models like
from app.models import Model still works as expected.

Same goes for views.

--

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




Re: Imports, works from directory but not from file

2009-11-12 Thread andreas schmid
Bill Freeman wrote:
> If that wasn't a typo, I suspect that you're going to have trouble
> with having a directory
> named "views.py".  
yes it was a typo... my dir is called 'views'
> I don't know how you import anything from it unless
> it is directly on
> your path.
>
> When you say "my app is in the pythonpath of the environment":
>1. Which directory is in the python path?
>   
my pythonpath is called 'site-packages' and this one is in the
pythonpath. every packages in there has an __init__.py in each subdir.
>2. If you mean that you've added it to the PYTHONPATH environment
> variable then
> please check whether you can find it in sys.path when running in the manage.py
> shell.  (If you don't know how to do that, please ask.)  I don't know
> if your on Windows
> or something else, but, especially on Windows, I don't take it for
> granted that environment
> settings will be properly respected/available.
>   
im on a unix system and i can import it on the django shell but it makes
troubles when running the django server which doesnt make sense
> Bill
>
> On Thu, Nov 12, 2009 at 2:39 AM, andreas schmid  wrote:
>   
>> Bill Freeman wrote:
>> 
>>> Does your project root (I'm assuming that's the directory containing
>>> the sub directory "app") have an __init__.py file.  Does the app
>>> directory?  I'm assuming that the one you mention below is in
>>> app/models/ .  All three are required, if what you have on your
>>> sys.path is just the project root.
>>>
>>>
>>>   
>> my app is in the pythonpath of the environment and every directory has
>> an __init__.py
>> everything worked fine till i changed the structure from:
>>
>> app/
>>__init__.py
>>models.py
>>views.py
>>urls.py
>>
>> to:
>>
>> app/
>>models/
>> __init__.py
>> models.py
>>views.py/
>> __init__.py
>> whatever.py
>> another.py
>>urls/
>> __init__.py
>> myurls.py
>> anotherurls.py
>> __init__.py
>>
>> so it doesnt really make sense that i cant import something in the
>> second case from my point of view.
>>
>>
>> 
>>> Do make sure that your project root is on your sys.path.  You can do
>>> that by temporarily adding:
>>>
>>> import foo
>>>
>>> to settings.py, and creating a file foo.py in the project root containing, 
>>> say:
>>>
>>> print "Foo!"
>>>
>>> and then do:
>>>
>>> python manage.py shell
>>>
>>> As part of the startup printout you should get a line containing only:
>>>
>>> Foo!
>>>
>>> By the way, I'm pretty sure that lots of django internal machinery,
>>> like syncdb, for example, depends on there being a file named exactly
>>> "models.py" in each app.  So a better choice of directory structure
>>> might be "app/myapp/models.py", with your myclass class in the
>>> models.py file.  Pinax uses such a structure, though the upper
>>> directory is called apps instead of app (there may be more than one)
>>> and has been added to the path, so that you can mention it in
>>> INSTALLED_APPS as just 'myapp', rather than 'apps/myapp'.  If you go
>>> this route, you will want to insert the apps directory at or near the
>>> beginning of sys.path, rather than appending it, so that when you
>>> mention a possibly installed app, such as django_microblogging, but
>>> which you have customized in a copy in your apps folder, you can say
>>> 'microblogging' in your INSTALLED_APPS and it will find yours rather
>>> than the system version.  (TN.B.: emplate paths are a separate issue.)
>>>
>>> Bill
>>>
>>> On Wed, Nov 11, 2009 at 10:34 AM, andreas schmid  
>>> wrote:
>>>
>>>   
 im experiencing the same problem.
 i started with a simple app and the modules.py was at the root of the
 app package, so everything was fine.
 now i extended the app and restructured the files in it by making a
 subfolder app/models and inside an __init__.py and the mymodel.py

 in the urls.py which i have an import like
 from app.models.mymodel import myclass

 and while the django shell is able to import it without any problem the
 django server isnt. im using buildout to build the whole django
 environment so the python interpreter im starting the shell and the one
 of the server is the same.

 i already tried to remove all .pyc and start everything again but i had
 no luck.

 this is weird and i cant figure out what the problem could be.



 Bill Freeman wrote:

 
> I had another couple of thoughts.
>
> In the manage.py shell, try:
>
> from openid.yadis import xri
> xri
>
> The output will include from where it got xri.  Make sure that it's
> coming from your lib directory, and not from some old system wide
> installation of openid.
>
>
> Instead of:
>
> from openid.association import Association as OIDAssociation
>
> try:
>
>

Re: Imports, works from directory but not from file

2009-11-12 Thread Bill Freeman
If that wasn't a typo, I suspect that you're going to have trouble
with having a directory
named "views.py".  I don't know how you import anything from it unless
it is directly on
your path.

When you say "my app is in the pythonpath of the environment":
   1. Which directory is in the python path?
   2. If you mean that you've added it to the PYTHONPATH environment
variable then
please check whether you can find it in sys.path when running in the manage.py
shell.  (If you don't know how to do that, please ask.)  I don't know
if your on Windows
or something else, but, especially on Windows, I don't take it for
granted that environment
settings will be properly respected/available.

Bill

On Thu, Nov 12, 2009 at 2:39 AM, andreas schmid  wrote:
> Bill Freeman wrote:
>> Does your project root (I'm assuming that's the directory containing
>> the sub directory "app") have an __init__.py file.  Does the app
>> directory?  I'm assuming that the one you mention below is in
>> app/models/ .  All three are required, if what you have on your
>> sys.path is just the project root.
>>
>>
> my app is in the pythonpath of the environment and every directory has
> an __init__.py
> everything worked fine till i changed the structure from:
>
> app/
>    __init__.py
>    models.py
>    views.py
>    urls.py
>
> to:
>
> app/
>    models/
>             __init__.py
>             models.py
>    views.py/
>             __init__.py
>             whatever.py
>             another.py
>    urls/
>             __init__.py
>             myurls.py
>             anotherurls.py
> __init__.py
>
> so it doesnt really make sense that i cant import something in the
> second case from my point of view.
>
>
>> Do make sure that your project root is on your sys.path.  You can do
>> that by temporarily adding:
>>
>>     import foo
>>
>> to settings.py, and creating a file foo.py in the project root containing, 
>> say:
>>
>>     print "Foo!"
>>
>> and then do:
>>
>>     python manage.py shell
>>
>> As part of the startup printout you should get a line containing only:
>>
>>     Foo!
>>
>> By the way, I'm pretty sure that lots of django internal machinery,
>> like syncdb, for example, depends on there being a file named exactly
>> "models.py" in each app.  So a better choice of directory structure
>> might be "app/myapp/models.py", with your myclass class in the
>> models.py file.  Pinax uses such a structure, though the upper
>> directory is called apps instead of app (there may be more than one)
>> and has been added to the path, so that you can mention it in
>> INSTALLED_APPS as just 'myapp', rather than 'apps/myapp'.  If you go
>> this route, you will want to insert the apps directory at or near the
>> beginning of sys.path, rather than appending it, so that when you
>> mention a possibly installed app, such as django_microblogging, but
>> which you have customized in a copy in your apps folder, you can say
>> 'microblogging' in your INSTALLED_APPS and it will find yours rather
>> than the system version.  (TN.B.: emplate paths are a separate issue.)
>>
>> Bill
>>
>> On Wed, Nov 11, 2009 at 10:34 AM, andreas schmid  
>> wrote:
>>
>>> im experiencing the same problem.
>>> i started with a simple app and the modules.py was at the root of the
>>> app package, so everything was fine.
>>> now i extended the app and restructured the files in it by making a
>>> subfolder app/models and inside an __init__.py and the mymodel.py
>>>
>>> in the urls.py which i have an import like
>>> from app.models.mymodel import myclass
>>>
>>> and while the django shell is able to import it without any problem the
>>> django server isnt. im using buildout to build the whole django
>>> environment so the python interpreter im starting the shell and the one
>>> of the server is the same.
>>>
>>> i already tried to remove all .pyc and start everything again but i had
>>> no luck.
>>>
>>> this is weird and i cant figure out what the problem could be.
>>>
>>>
>>>
>>> Bill Freeman wrote:
>>>
 I had another couple of thoughts.

 In the manage.py shell, try:

     from openid.yadis import xri
     xri

 The output will include from where it got xri.  Make sure that it's
 coming from your lib directory, and not from some old system wide
 installation of openid.


 Instead of:

     from openid.association import Association as OIDAssociation

 try:

     from association import Association as OIDAssociation


 Also, expecially if the previous worked, in your settings.py, instead of:

     sys.path.append(os.path.join(FILE_ROOT, 'lib', 'openid'))

 try

     sys.path.append(os.path.join(FILE_ROOT, 'lib'))

 Bill



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

Re: Imports, works from directory but not from file

2009-11-11 Thread andreas schmid
Bill Freeman wrote:
> Does your project root (I'm assuming that's the directory containing
> the sub directory "app") have an __init__.py file.  Does the app
> directory?  I'm assuming that the one you mention below is in
> app/models/ .  All three are required, if what you have on your
> sys.path is just the project root.
>
>   
my app is in the pythonpath of the environment and every directory has
an __init__.py
everything worked fine till i changed the structure from:

app/
__init__.py
models.py
views.py
urls.py

to:

app/
models/
 __init__.py
 models.py
views.py/
 __init__.py
 whatever.py
 another.py
urls/
 __init__.py
 myurls.py
 anotherurls.py
__init__.py

so it doesnt really make sense that i cant import something in the
second case from my point of view.


> Do make sure that your project root is on your sys.path.  You can do
> that by temporarily adding:
>
> import foo
>
> to settings.py, and creating a file foo.py in the project root containing, 
> say:
>
> print "Foo!"
>
> and then do:
>
> python manage.py shell
>
> As part of the startup printout you should get a line containing only:
>
> Foo!
>
> By the way, I'm pretty sure that lots of django internal machinery,
> like syncdb, for example, depends on there being a file named exactly
> "models.py" in each app.  So a better choice of directory structure
> might be "app/myapp/models.py", with your myclass class in the
> models.py file.  Pinax uses such a structure, though the upper
> directory is called apps instead of app (there may be more than one)
> and has been added to the path, so that you can mention it in
> INSTALLED_APPS as just 'myapp', rather than 'apps/myapp'.  If you go
> this route, you will want to insert the apps directory at or near the
> beginning of sys.path, rather than appending it, so that when you
> mention a possibly installed app, such as django_microblogging, but
> which you have customized in a copy in your apps folder, you can say
> 'microblogging' in your INSTALLED_APPS and it will find yours rather
> than the system version.  (TN.B.: emplate paths are a separate issue.)
>
> Bill
>
> On Wed, Nov 11, 2009 at 10:34 AM, andreas schmid  wrote:
>   
>> im experiencing the same problem.
>> i started with a simple app and the modules.py was at the root of the
>> app package, so everything was fine.
>> now i extended the app and restructured the files in it by making a
>> subfolder app/models and inside an __init__.py and the mymodel.py
>>
>> in the urls.py which i have an import like
>> from app.models.mymodel import myclass
>>
>> and while the django shell is able to import it without any problem the
>> django server isnt. im using buildout to build the whole django
>> environment so the python interpreter im starting the shell and the one
>> of the server is the same.
>>
>> i already tried to remove all .pyc and start everything again but i had
>> no luck.
>>
>> this is weird and i cant figure out what the problem could be.
>>
>>
>>
>> Bill Freeman wrote:
>> 
>>> I had another couple of thoughts.
>>>
>>> In the manage.py shell, try:
>>>
>>> from openid.yadis import xri
>>> xri
>>>
>>> The output will include from where it got xri.  Make sure that it's
>>> coming from your lib directory, and not from some old system wide
>>> installation of openid.
>>>
>>>
>>> Instead of:
>>>
>>> from openid.association import Association as OIDAssociation
>>>
>>> try:
>>>
>>> from association import Association as OIDAssociation
>>>
>>>
>>> Also, expecially if the previous worked, in your settings.py, instead of:
>>>
>>> sys.path.append(os.path.join(FILE_ROOT, 'lib', 'openid'))
>>>
>>> try
>>>
>>> sys.path.append(os.path.join(FILE_ROOT, 'lib'))
>>>
>>> Bill
>>>
>>>   
>>>   
>> 
>
> --~--~-~--~~~---~--~~
> 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+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 at 
http://groups.google.com/group/django-users?hl=.




Re: Imports, works from directory but not from file

2009-11-11 Thread Bill Freeman

Does your project root (I'm assuming that's the directory containing
the sub directory "app") have an __init__.py file.  Does the app
directory?  I'm assuming that the one you mention below is in
app/models/ .  All three are required, if what you have on your
sys.path is just the project root.

Do make sure that your project root is on your sys.path.  You can do
that by temporarily adding:

import foo

to settings.py, and creating a file foo.py in the project root containing, say:

print "Foo!"

and then do:

python manage.py shell

As part of the startup printout you should get a line containing only:

Foo!

By the way, I'm pretty sure that lots of django internal machinery,
like syncdb, for example, depends on there being a file named exactly
"models.py" in each app.  So a better choice of directory structure
might be "app/myapp/models.py", with your myclass class in the
models.py file.  Pinax uses such a structure, though the upper
directory is called apps instead of app (there may be more than one)
and has been added to the path, so that you can mention it in
INSTALLED_APPS as just 'myapp', rather than 'apps/myapp'.  If you go
this route, you will want to insert the apps directory at or near the
beginning of sys.path, rather than appending it, so that when you
mention a possibly installed app, such as django_microblogging, but
which you have customized in a copy in your apps folder, you can say
'microblogging' in your INSTALLED_APPS and it will find yours rather
than the system version.  (TN.B.: emplate paths are a separate issue.)

Bill

On Wed, Nov 11, 2009 at 10:34 AM, andreas schmid  wrote:
>
> im experiencing the same problem.
> i started with a simple app and the modules.py was at the root of the
> app package, so everything was fine.
> now i extended the app and restructured the files in it by making a
> subfolder app/models and inside an __init__.py and the mymodel.py
>
> in the urls.py which i have an import like
> from app.models.mymodel import myclass
>
> and while the django shell is able to import it without any problem the
> django server isnt. im using buildout to build the whole django
> environment so the python interpreter im starting the shell and the one
> of the server is the same.
>
> i already tried to remove all .pyc and start everything again but i had
> no luck.
>
> this is weird and i cant figure out what the problem could be.
>
>
>
> Bill Freeman wrote:
>> I had another couple of thoughts.
>>
>> In the manage.py shell, try:
>>
>>     from openid.yadis import xri
>>     xri
>>
>> The output will include from where it got xri.  Make sure that it's
>> coming from your lib directory, and not from some old system wide
>> installation of openid.
>>
>>
>> Instead of:
>>
>>     from openid.association import Association as OIDAssociation
>>
>> try:
>>
>>     from association import Association as OIDAssociation
>>
>>
>> Also, expecially if the previous worked, in your settings.py, instead of:
>>
>>     sys.path.append(os.path.join(FILE_ROOT, 'lib', 'openid'))
>>
>> try
>>
>>     sys.path.append(os.path.join(FILE_ROOT, 'lib'))
>>
>> Bill
>>
>> >
>>
>>
>
>
> >
>

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Imports, works from directory but not from file

2009-11-11 Thread andreas schmid

im experiencing the same problem.
i started with a simple app and the modules.py was at the root of the
app package, so everything was fine.
now i extended the app and restructured the files in it by making a
subfolder app/models and inside an __init__.py and the mymodel.py

in the urls.py which i have an import like
from app.models.mymodel import myclass

and while the django shell is able to import it without any problem the
django server isnt. im using buildout to build the whole django
environment so the python interpreter im starting the shell and the one
of the server is the same.

i already tried to remove all .pyc and start everything again but i had
no luck.

this is weird and i cant figure out what the problem could be.



Bill Freeman wrote:
> I had another couple of thoughts.
>
> In the manage.py shell, try:
>
> from openid.yadis import xri
> xri
>
> The output will include from where it got xri.  Make sure that it's
> coming from your lib directory, and not from some old system wide
> installation of openid.
>
>
> Instead of:
>
> from openid.association import Association as OIDAssociation
>
> try:
>
> from association import Association as OIDAssociation
>
>
> Also, expecially if the previous worked, in your settings.py, instead of:
>
> sys.path.append(os.path.join(FILE_ROOT, 'lib', 'openid'))
>
> try
>
> sys.path.append(os.path.join(FILE_ROOT, 'lib'))
>
> Bill
>
> >
>
>   


--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Imports, works from directory but not from file

2009-11-11 Thread Bill Freeman

I had another couple of thoughts.

In the manage.py shell, try:

from openid.yadis import xri
xri

The output will include from where it got xri.  Make sure that it's
coming from your lib directory, and not from some old system wide
installation of openid.


Instead of:

from openid.association import Association as OIDAssociation

try:

from association import Association as OIDAssociation


Also, expecially if the previous worked, in your settings.py, instead of:

sys.path.append(os.path.join(FILE_ROOT, 'lib', 'openid'))

try

sys.path.append(os.path.join(FILE_ROOT, 'lib'))

Bill

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Imports, works from directory but not from file

2009-11-10 Thread Bill Freeman

Did you really mean "init.py", rather than the required "__init__.py".
 If so, that's wrong.

I presume that the names of the files and directories are all lower
case and contain no spaces or accented characters.

I presume that you've checked and rechecked the spelling of the file
name against the spelling in the import statement.

It is just possible that it's a permission problem.  Can you open the
files for reading?  In the manage.py shell try (and assuming FILE_ROOT
is the current directory):

open('lib/openid/association.py').readline()

If that doesn't give an exception, but just prints the first line of
the file, good.

If there's an association.pyc remove it and try again (write
permission problem to replace file
with compiled python for the version of the interpreter in use, though
I think it would work fine
anyway).

I'd expect a different exception, but maybe a problem in
association.py is preventing it from importing.  Try adding:

import pdb;pdb.set_trace()

to the top of association.py and see if it gets there and stops, then
step along ("n" command) to see that it all completes.

Check that the file doesn't have Apple line endings ('\r' only).  I
know that both DOS and *nix line endings work on Windows ('\r\n' and
'\n' respectively).

There could be problems if the file is in a unicode variant as opposed
to simple 7 bit ASCII, though I hope not.  That would probably be
clear from the open test above.

Check that the __init__.py files are either empty, or have valid
python contents.

Beyond that, I'm out of ideas.  Good luck.

Bill

On Tue, Nov 10, 2009 at 5:24 PM, When ideas fail
 wrote:
>
> I used this in my settings.py to add the open id folder on my path
> sys.path.append(os.path.join(FILE_ROOT, 'lib', 'openid'))
>
> and on my path it say:
> 'C:\\TheAbeonaFile\\Python26\\Lib\\idlelib\\lib\\openid'
>
> And both the yadis file and openid file have an init.py
>
> On 10 Nov, 22:18, Bill Freeman  wrote:
>> Which all of these directories are on your python path, and which of
>> them have __init__.py files?
>>
>> Bill
>>
>> [Hint to answer first question:
>>
>> from pprint import pprint as pp
>> import sys
>> pp(sys.path)
>>
>> ]
>>
>> On Tue, Nov 10, 2009 at 5:12 PM, When ideas fail
>>
>>  wrote:
>>
>> > I have a folder called lib on my path and in there I have a folder
>> > called openid.
>>
>> > If i want to import things from openid i have a problem.
>>
>> > For example if I have these 2 import statements:
>>
>> > from openid.yadis import xri
>> > from openid.association import Association as OIDAssociation
>>
>> > yadis is a directory (in openid) and then there is xri.py inside. This
>> > works fine.
>> > association is a .py file not in a subdirectory, just in lib/openid
>> > this produces the following error:
>>
>> > Traceback (most recent call last):
>> >  File "C:\TheAbeonaFile\Apache2.2\MyProject\test.py", line 18, in
>> > 
>> >    from openid.association import Association as OIDAssociation
>> > ImportError: No module named association
>>
>> > I don't see why one would be a problem and not the other, does anyone
>> > have any ideas?
>>
>> > thanks
> >
>

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Imports, works from directory but not from file

2009-11-10 Thread When ideas fail

I used this in my settings.py to add the open id folder on my path
sys.path.append(os.path.join(FILE_ROOT, 'lib', 'openid'))

and on my path it say:
'C:\\TheAbeonaFile\\Python26\\Lib\\idlelib\\lib\\openid'

And both the yadis file and openid file have an init.py

On 10 Nov, 22:18, Bill Freeman  wrote:
> Which all of these directories are on your python path, and which of
> them have __init__.py files?
>
> Bill
>
> [Hint to answer first question:
>
> from pprint import pprint as pp
> import sys
> pp(sys.path)
>
> ]
>
> On Tue, Nov 10, 2009 at 5:12 PM, When ideas fail
>
>  wrote:
>
> > I have a folder called lib on my path and in there I have a folder
> > called openid.
>
> > If i want to import things from openid i have a problem.
>
> > For example if I have these 2 import statements:
>
> > from openid.yadis import xri
> > from openid.association import Association as OIDAssociation
>
> > yadis is a directory (in openid) and then there is xri.py inside. This
> > works fine.
> > association is a .py file not in a subdirectory, just in lib/openid
> > this produces the following error:
>
> > Traceback (most recent call last):
> >  File "C:\TheAbeonaFile\Apache2.2\MyProject\test.py", line 18, in
> > 
> >    from openid.association import Association as OIDAssociation
> > ImportError: No module named association
>
> > I don't see why one would be a problem and not the other, does anyone
> > have any ideas?
>
> > thanks
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Imports, works from directory but not from file

2009-11-10 Thread Bill Freeman

Which all of these directories are on your python path, and which of
them have __init__.py files?

Bill

[Hint to answer first question:

from pprint import pprint as pp
import sys
pp(sys.path)

]

On Tue, Nov 10, 2009 at 5:12 PM, When ideas fail
 wrote:
>
> I have a folder called lib on my path and in there I have a folder
> called openid.
>
> If i want to import things from openid i have a problem.
>
> For example if I have these 2 import statements:
>
> from openid.yadis import xri
> from openid.association import Association as OIDAssociation
>
> yadis is a directory (in openid) and then there is xri.py inside. This
> works fine.
> association is a .py file not in a subdirectory, just in lib/openid
> this produces the following error:
>
> Traceback (most recent call last):
>  File "C:\TheAbeonaFile\Apache2.2\MyProject\test.py", line 18, in
> 
>    from openid.association import Association as OIDAssociation
> ImportError: No module named association
>
> I don't see why one would be a problem and not the other, does anyone
> have any ideas?
>
> thanks
> >
>

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Imports, works from directory but not from file

2009-11-10 Thread When ideas fail

I have a folder called lib on my path and in there I have a folder
called openid.

If i want to import things from openid i have a problem.

For example if I have these 2 import statements:

from openid.yadis import xri
from openid.association import Association as OIDAssociation

yadis is a directory (in openid) and then there is xri.py inside. This
works fine.
association is a .py file not in a subdirectory, just in lib/openid
this produces the following error:

Traceback (most recent call last):
  File "C:\TheAbeonaFile\Apache2.2\MyProject\test.py", line 18, in

from openid.association import Association as OIDAssociation
ImportError: No module named association

I don't see why one would be a problem and not the other, does anyone
have any ideas?

thanks
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Circular imports problem

2009-07-08 Thread Ryan K

Well all of the models are part of the same application. So here is
the code:

models.py:

from django.db import models
from django.db.models import signlas
from django.dispatch import disaptacher

from asqcom.apps.staticpages import signals
from asqcom.abstract_models import CommonAbstractModel

class Link(CommonAbstractModel):
url = models.CharField(max_length=1024)
title = models.CharField(max_length=64)
text = models.CharField(max_length=64)

class Meta:
verbose_name = 'static page'
verbose_name_plural = 'static pages'
ordering = ('url',)

def __unicode__(self):
return u"%s - %s" (self.text, self.url,)

class Menu(CommonAbstractModel):
name = models.CharField(max_length=64)
title = models.CharField(max_length=64) # used in the 
links = models.ManyToManyField(Link)

class Meta:
verbose_name = 'menu'
verbose_name_plural = 'menus'
ordering = ('name',)

def __unicode__(self):
return u"%s" % (self.name)

def to_xhtml(self):
pass

class StaticPage(CommonAbstractModel):
url = models.CharField(max_length=128, db_index=True)
title = models.CharField(max_length=256)
content = models.TextField(blank=True)
menu = models.ForeignKey(Menu, null=True, blank=True)
template_name = models.CharField(max_length=70, blank=True)
registration_required = models.BooleanField()

class Meta:
verbose_name = 'static page'
verbose_name_plural = 'static pages'
ordering = ('url',)

def __unicode__(self):
return "%s - %s (menu: %s)" % (self.title, self.url, self.menu
or 'no menu',)

class CachedMenuXhtml(CommonAbstractModel):
menu = models.ForeignKey(Menu)
xhtml = models.TextField()

def __unicode__(self):
return self.xhtml

dispatcher.connect(signals.build_menu_from_link_mod,
   signal=signals.signals.post_save, sender=Link)
dispatcher.connect(signals.build_menu_from_menu_mod,
   signal=signals.signals.post_save, sender=Menu)


signals.py:

import threading

from asqcom.apps.staticpages.models import Link, Menu, StaticPage
from asqcom.apps.staticpages.models import CachedMenuXhtml

class GenerateMenuXhtmlByLink(threading.Thread):
def __init__(self, instance):
self.instance = instance
threading.Thread.__init__(self)

def run(self):
pass

class GenerateMenuXhtmlByMenu(threading.Thread):
def __init__(self, instance):
self.instance = instance
threading.Thread.__init__(self)

def run(self):
pass

def build_menu_from_link_mod(...

def build_menu_from_menu_mod(..


I hope this better specifies the issue. Should I import these in the
run method for each Thread subclass?

On Jul 8, 4:47 pm, Alex Gaynor  wrote:
> On Wed, Jul 8, 2009 at 3:45 PM, Ryan K  wrote:
>
> > I am creating an application that uses the dispatcher in Django. In
> > the apps models.py file I connect the post_save of two models to
> > functions in a signals.py file. The problem is that I need to use
> > several models in models.py and that leads to a circular import. What
> > is the best way to solve this problem?
>
> > Cheers,
> > Ryan
>
> Depending on how you're using these other models you can a) do imports
> inside of functions, or 
> b)http://docs.djangoproject.com/en/dev/ref/models/fields/#lazy-relation
>  Or both (probably this)
>
> Alex
>
> --
> "I disapprove of what you say, but I will defend to the death your right to
> say it." --Voltaire
> "The people's good is the highest law."--Cicero
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Circular imports problem

2009-07-08 Thread Alex Gaynor
On Wed, Jul 8, 2009 at 3:45 PM, Ryan K  wrote:

>
> I am creating an application that uses the dispatcher in Django. In
> the apps models.py file I connect the post_save of two models to
> functions in a signals.py file. The problem is that I need to use
> several models in models.py and that leads to a circular import. What
> is the best way to solve this problem?
>
> Cheers,
> Ryan
> >
>
Depending on how you're using these other models you can a) do imports
inside of functions, or b)
http://docs.djangoproject.com/en/dev/ref/models/fields/#lazy-relationships.
 Or both (probably this)

Alex

-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." --Voltaire
"The people's good is the highest law."--Cicero

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Circular imports problem

2009-07-08 Thread Ryan K

I am creating an application that uses the dispatcher in Django. In
the apps models.py file I connect the post_save of two models to
functions in a signals.py file. The problem is that I need to use
several models in models.py and that leads to a circular import. What
is the best way to solve this problem?

Cheers,
Ryan
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Relative imports in custom template tag collections seem to look in the wrong place.

2009-06-15 Thread greatlemer

On Jun 15, 10:47 pm, Alex Gaynor  wrote:
> On Mon, Jun 15, 2009 at 4:39 PM, greatlemer wrote:
>
>
>
>
>
> > Hi everyone,
>
> > I was creating some custom template tags and attempted to import my
> > models through the following import at the top of the file:
>
> > from ..models import *
>
> > Unfortunately this seems to be attempting to import from django.models
> > (which doesn't exist) rather than myapp.models and therefore throws up
> > an error when I try to use it.  Is there any possible way that I will
> > be able to perform this import without specifying the app name (as I
> > was trying to keep things as generic as possible) or am I just going
> > to have to bite the bullet and hard-code the app name in there?
>
> > Cheers,
>
> > G
>
> Unfortunately for the time being you're going to need to bite the bullet.
> Django does some *very* nasty things when loading templatetags that are
> almost certainly the source of your problem.  This is considered a bug,
> however it won't get fixed until the 1.2 release cycle in all likelyhood
> (only bugs that break things very badly are being considered for 1.1 at this
> point).
>
> Alex
>
> --
> "I disapprove of what you say, but I will defend to the death your right to
> say it." --Voltaire
> "The people's good is the highest law."--Cicero

Ah well, I feared that may be the case, fortunately it's not a
showstopper, just an annoying niggle.  I'll keep an eye on things to
see if it does get picked up for 1.2 though.

Thanks for the quick response.

G
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Relative imports in custom template tag collections seem to look in the wrong place.

2009-06-15 Thread Alex Gaynor
On Mon, Jun 15, 2009 at 4:39 PM, greatlemer wrote:

>
> Hi everyone,
>
> I was creating some custom template tags and attempted to import my
> models through the following import at the top of the file:
>
> from ..models import *
>
> Unfortunately this seems to be attempting to import from django.models
> (which doesn't exist) rather than myapp.models and therefore throws up
> an error when I try to use it.  Is there any possible way that I will
> be able to perform this import without specifying the app name (as I
> was trying to keep things as generic as possible) or am I just going
> to have to bite the bullet and hard-code the app name in there?
>
> Cheers,
>
> G
> >
>
Unfortunately for the time being you're going to need to bite the bullet.
Django does some *very* nasty things when loading templatetags that are
almost certainly the source of your problem.  This is considered a bug,
however it won't get fixed until the 1.2 release cycle in all likelyhood
(only bugs that break things very badly are being considered for 1.1 at this
point).

Alex

-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." --Voltaire
"The people's good is the highest law."--Cicero

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Relative imports in custom template tag collections seem to look in the wrong place.

2009-06-15 Thread greatlemer

Hi everyone,

I was creating some custom template tags and attempted to import my
models through the following import at the top of the file:

from ..models import *

Unfortunately this seems to be attempting to import from django.models
(which doesn't exist) rather than myapp.models and therefore throws up
an error when I try to use it.  Is there any possible way that I will
be able to perform this import without specifying the app name (as I
was trying to keep things as generic as possible) or am I just going
to have to bite the bullet and hard-code the app name in there?

Cheers,

G
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Model A imports Model B imports Model A - I'm completely lost as to how to get around circular imports

2009-04-05 Thread codecowboy

Doug,

Thank you so much.  I completely missed that detail while ripping
through the django docs.  I've got it all working now.  Ramiro, thanks
again for your help.

For anyone else who is trying to do this and stumbles upon this
thread, here is my full final solution.

--- Overview ---
I've got two applications within the scinet project.
scinet.conferences and scinet.scientists  There are 4 files in all
that I need for this to work.  scinet.conferences.models.py,
scinet.scientists.models.py, scinet.scientists.views.py and
scinet.templates.scientists.portal.html

--- scinet.conferences.models (DjangoProjects/scinet/conferences/
models.py) ---

I define the many-to-many relationship between conferences and
scientists here.  A conference can be attended by multiple scientists
and a scientist can attend multiple conferences.  Do note that I have
arbitrarily chosen to use the Conference model to specify the
relationship between Conferences and Scientists.  I could have done
this in my scientist model just as well.  It is important to not that
I had to import the Scientist model here since I must reference it in
order to specify the relationship in the following line: scientists =
models.ManyToManyField(Scientist, through='ConferenceAttendee').

from django.db import models
from scinet.scientists.models import Scientist

class Conference(models.Model):
title = models.CharField(max_length=200)
description = models.TextField()
date = models.DateTimeField()
scientists = models.ManyToManyField(Scientist,
through='ConferenceAttendee')
created = models.DateTimeField()
modified = models.DateTimeField()

def __unicode__(self):
return self.name

class ConferenceAttendee(models.Model):
conference = models.ForeignKey(Conference)
scientist = models.ForeignKey(Scientist)
rsvp_date = models.DateTimeField()
presenter = models.BooleanField()

--- scinet.scientists.models (DjangoProjects/scinet/scientists/
models.py) ---

Here is where I define my Scientist model.  The point to note here is
that I do NOT need to reference the conference model because in
Django, I only need to define a relationship between two models in one
of the two models.  In fact, you must ONLY define the relationship
between two models in ONE of the two models.  DO NOT SPECIFY A
RELATIONSHIP BETWEEN TWO MODELS IN BOTH MODELS.

from django.db import models

class Scientist(models.Model):
user = models.ForeignKey('auth.User')
summary = models.TextField()
undergrad = models.CharField(max_length=50)
graduate = models.CharField(max_length=50)
post_doctorate = models.CharField(max_length=50)
current_employer = models.CharField(max_length=50)
zipcode = models.CharField(max_length=5)
city = models.CharField(max_length=50)
state = models.CharField(max_length=2)
country = models.CharField(max_length=200, blank=False)
created = models.DateTimeField()
modified = models.DateTimeField()

--- scinet.scientists.views (DjangoProjects/scinet/scientists/
views.py) ---

There is nothing special to note here other than that I am simply
getting a scientist.  The following line grabs a scientist and all
related conferences.  s = get_object_or_404(Scientist,
pk=scientist_id).  In the template file which I will show next, you
will see how I access the conferences that are associated with this
particular scientist.  Do note once again that there is no need to
explicitly import the Conference model.  Django (or Python) does this
magically (which I am curious about if anyone is up for explaining
that to me).

from django.shortcuts import render_to_response, get_object_or_404
from django.template import Context, loader
from scinet.scientists.models import Scientist
from django.http import HttpResponse, HttpResponseRedirect

def portal(request):
scientist_id = 1
s = get_object_or_404(Scientist, pk=scientist_id)
return render_to_response('scientists/portal.html',
{'scientist':s})

--- scinet.templates.scientists.portal (DjangoProjects/scinet/
templates/scientists/portal.html) ---

The key here is to understand how to traverse related models in
reverse.  Check out the link that Doug posted above.  In short,
because I chose to indicate a relationship between Conferences and
Scientists in the Conference model, Scientist must access conferences
in reverse.  I used Django's default naming of a reverse model
reference in my loop below.  In case you don't know, here is the loop
for traversing scientists that belong to a conference.

{% for scientist in 
conference.scientists.all %}
{{ 
scientist.user.get_full_name }}
{% endfor %}

In order to traverse the conferences that a scientist will attend you
must use scientists.conference_set.all.  The key differnece being
"_set".  Anyways, the template code that I used is below.

{{ scientist.user.get_full_name }}
You are attendin

Re: Model A imports Model B imports Model A - I'm completely lost as to how to get around circular imports

2009-04-05 Thread Doug B

Take a look at this part of the documentation:

http://docs.djangoproject.com/en/dev/topics/db/queries/#backwards-related-objects

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Model A imports Model B imports Model A - I'm completely lost as to how to get around circular imports

2009-04-05 Thread codecowboy

Hi Ramiro,

Thank you for the response.  I read the links and modified my model
files as you instructed.  I am currently able to loop through
scientists that belong to a conference and print them out.  However, I
also need to be able to loop through conferences that a scientist is
attending.  I am not able to do that with the solution that you
provided.

-- myproject.scientist.views -
def portal
s = get_object_or_404(Scientist, pk=scientist_id)

- templates/portal.html
{% for conference in scientist.conference.all %}
{{ conference.title }},
{% endfor %}

On Apr 5, 11:19 am, Ramiro Morales  wrote:
> On Sun, Apr 5, 2009 at 11:43 AM, codecowboy  wrote:
>
> > Hi Everyone,
>
> > I'm new to the Django community and I am having trouble with circular
> > imports.  I've read every article that I can find about the issue
> > including posts on this group.  I'm going to paste my model files and
> > the stack trace.  I'm sure that this must be an issue that has come up
> > before.  Thank you in advance for any help.  If you know of an article
> > that explains the problem then point me to it.  Thanks again.
>
> > [snip]
>
> Do you need to have two, parallel, many to many relationships between 
> Scientist
> and Conference?. If the answer is no then yo don't need to define that
> relationship in both models. This alone almost solves your circular reference
> problem.
>
> The technique of using a string with the name of the model instead of tthe 
> Model
> class onject iself to indicate the target of a relationship is only needed 
> (and
> accepted) when:
>
> * The target model hasn't yet been defined (It comes after in the same
>   application models.py file).
>
> * or when you have two mutually referencing relationships between
>   two models located in diferent applications (actually, this second use case
>   isn't clearly explained in our documentation)
>
> Using these guidelines and simplifying you example to the relevant bits,
> something like this could be a start of a solution to your problem:
>
> -- scinet.scientists.models ---
> from django.db import models
>
> class Scientist(models.Model):
>     # ...
>     # no many to many field needed here
>
> -- scinet.conferences.models ---
> from django.db import models
> from scinet.scientists.models import Scientist
>
> class ConferenceAttendee(models.Model):
>     # ...
>     conference = models.ForeignKey('Conference')
>     scientist = models.ForeignKey(Scientist)
>
> class Conference(models.Model):
>     # ...
>     scientists = models.ManyToManyField(Scientist, through=ConferenceAttendee)
>
> Relevant documentation links worth reading:
>
> http://docs.djangoproject.com/en/dev/topics/db/models/#many-to-many-r...http://docs.djangoproject.com/en/dev/topics/db/models/#models-across-...http://docs.djangoproject.com/en/dev/ref/models/fields/#lazy-relation...
>
> HTH
>
> --
> Ramiro Moraleshttp://rmorales.net
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Model A imports Model B imports Model A - I'm completely lost as to how to get around circular imports

2009-04-05 Thread Ramiro Morales

On Sun, Apr 5, 2009 at 11:43 AM, codecowboy  wrote:
>
> Hi Everyone,
>
> I'm new to the Django community and I am having trouble with circular
> imports.  I've read every article that I can find about the issue
> including posts on this group.  I'm going to paste my model files and
> the stack trace.  I'm sure that this must be an issue that has come up
> before.  Thank you in advance for any help.  If you know of an article
> that explains the problem then point me to it.  Thanks again.
>
> [snip]

Do you need to have two, parallel, many to many relationships between Scientist
and Conference?. If the answer is no then yo don't need to define that
relationship in both models. This alone almost solves your circular reference
problem.

The technique of using a string with the name of the model instead of tthe Model
class onject iself to indicate the target of a relationship is only needed (and
accepted) when:

* The target model hasn't yet been defined (It comes after in the same
  application models.py file).

* or when you have two mutually referencing relationships between
  two models located in diferent applications (actually, this second use case
  isn't clearly explained in our documentation)

Using these guidelines and simplifying you example to the relevant bits,
something like this could be a start of a solution to your problem:

-- scinet.scientists.models ---
from django.db import models

class Scientist(models.Model):
# ...
# no many to many field needed here

-- scinet.conferences.models ---
from django.db import models
from scinet.scientists.models import Scientist

class ConferenceAttendee(models.Model):
# ...
conference = models.ForeignKey('Conference')
scientist = models.ForeignKey(Scientist)

class Conference(models.Model):
# ...
scientists = models.ManyToManyField(Scientist, through=ConferenceAttendee)


Relevant documentation links worth reading:

http://docs.djangoproject.com/en/dev/topics/db/models/#many-to-many-relationships
http://docs.djangoproject.com/en/dev/topics/db/models/#models-across-files
http://docs.djangoproject.com/en/dev/ref/models/fields/#lazy-relationships

HTH

--
Ramiro Morales
http://rmorales.net

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Model A imports Model B imports Model A - I'm completely lost as to how to get around circular imports

2009-04-05 Thread codecowboy

Hi Everyone,

I'm new to the Django community and I am having trouble with circular
imports.  I've read every article that I can find about the issue
including posts on this group.  I'm going to paste my model files and
the stack trace.  I'm sure that this must be an issue that has come up
before.  Thank you in advance for any help.  If you know of an article
that explains the problem then point me to it.  Thanks again.

-- Model A ---

from django.db import models
from scinet.conferences.models import ConferenceAttendee
class Scientist(models.Model):
user = models.ForeignKey('auth.User')
summary = models.TextField()
undergrad = models.CharField(max_length=50)
graduate = models.CharField(max_length=50)
post_doctorate = models.CharField(max_length=50)
current_employer = models.CharField(max_length=50)
zipcode = models.CharField(max_length=5)
city = models.CharField(max_length=50)
state = models.CharField(max_length=2)
country = models.CharField(max_length=200, blank=False)
created = models.DateTimeField()
modified = models.DateTimeField()
conferences = models.ManyToManyField(Conference,
through='ConferenceAttendee')

-- Model B ---

from django.db import models
from scinet.scientists.models import Scientist
class Conference(models.Model):
title = models.CharField(max_length=200)
description = models.TextField()
date = models.DateTimeField()
scientists = models.ManyToManyField(Scientist,
through='ConferenceAttendee')
created = models.DateTimeField()
modified = models.DateTimeField()

def __unicode__(self):
return self.name

class ConferenceAttendee(models.Model):
conference = models.ForeignKey(Conference)
scientist = models.ForeignKey(Scientist)
rsvp_date = models.DateTimeField()
presenter = models.BooleanField()

-- Stack Trace 

Validating models...
Unhandled exception in thread started by 
Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/django/core/management/
commands/runserver.py", line 48, in inner_run
self.validate(display_num_errors=True)
  File "/usr/lib/python2.5/site-packages/django/core/management/
base.py", line 246, in validate
num_errors = get_validation_errors(s, app)
  File "/usr/lib/python2.5/site-packages/django/core/management/
validation.py", line 28, in get_validation_errors
for (app_name, error) in get_app_errors().items():
  File "/usr/lib/python2.5/site-packages/django/db/models/loading.py",
line 128, in get_app_errors
self._populate()
  File "/usr/lib/python2.5/site-packages/django/db/models/loading.py",
line 57, in _populate
self.load_app(app_name, True)
  File "/usr/lib/python2.5/site-packages/django/db/models/loading.py",
line 72, in load_app
mod = __import__(app_name, {}, {}, ['models'])
  File "/home/guy/DjangoApps/scinet/../scinet/scientists/models.py",
line 2, in 
from scinet.conferences.models import ConferenceAttendee
  File "/home/guy/DjangoApps/scinet/../scinet/conferences/models.py",
line 2, in 
from scinet.scientists.models import Scientist
ImportError: cannot import name Scientist

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Circular imports between managers and models

2009-01-06 Thread Malcolm Tredinnick

On Tue, 2009-01-06 at 04:42 -0800, Polat Tuzla wrote:
> Hi,
> Suppose I have two classes in "models.py", namely A and B. And there
> is the manager for B as BManager in "managers.py". BManager makes use
> of clas A.
> 
> This situation leads to circular imports between "managers.py" and
> "models.py" for which I can't find a solution.

Since you don't tell us what you're doing currently, we'll have to break
out the crystal ball and see if the vapours let us guess as to the false
starts you might have made. It's hard to correct existing code without
knowing what it is. :-)

However, there shouldn't be any circular import problems. You just have
to remember to import only the module, not names inside the module in
one of the files. To wit...

In models.py:

from django.db import models

from managers import BManager

class A(models.Model):
...

class B(models.Model):
...
objects = BManager()

In managers.py:

from django.db import models

import models

class BManager(models.Manager):
...
# Refer to models.ModelA inside methods without problems.

When Python is importing models.py, it will see that it has to import
managers.py. So it does that, which says "import models". Python knows
that it has already imported (or is in the process of importing)
models.py, so it doesn't do it again, it just continues on. Providing it
does not need to refer to the contents of "models" (e.g. models.ModelA)
whilst parsing the managers module, there is no problem.

Circular import problems arise if you try to do "from models import
ModelA" in managers, because models isn't yet fully imported, so Python
cannot look up the ModelA inside that module. Problems would also arise
if you wrote something like this:

class BManager(models.Manager):
fred = models.ModelA.objects.all()

since the class definition is processed (executed) at parsing time,
which would require access to models.ModelA, which isn't available yet.
But that wouldn't be particularly sensible in any case (ModelA's
contents are dynamic, so reading it only at import time is inflexible).
If you have something like this:

class BManager(models.Manager):
def fred(self):
return models.ModelA.objects.all()

Then the statements inside fred() are not executed until the method is
called (they are parsed, but not executed, when the file is imported).
When you get around to calling method fred(), "models" will be fully
imported and there will be no problems.

> 
> Assuming that I need to separate models and manager into different
> files, so merging them is not an option,

If you have some particularly twisted set up, then you'll simply need to
drop that assumption. It's not really a requirement for functionality,
merely a style issue. And sometimes style has to take a backseat to
practicality.

>  are there any best practices

Best practice is "whatever works and is maintainable". People get far
too hung up on sticking a best practice label on things to the point
that it becomes devalued. Sure, some approaches look neater than others,
but I see too many people spinning their wheels wondering about the
unique "best" (in some truly debatable sense) solution, rather than just
getting on and solving their problems.

Regards,
Malcolm



--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Circular imports between managers and models

2009-01-06 Thread bruno desthuilliers

On 6 jan, 19:45, Ben Eliott  wrote:
> Not that pretty/efficient but you could use contenttype contrib
> temporarily.
> IF ClassB has a ForeignKey to Class A could you extract the Class A
> model from  a foreign key via Class B's  .meta?
> Or the Class B could have a callable method which returned the
> instance of the model you want...
> Not sure whether/how these will work but just throwing out some ideas.
>

This looks to me like dirty hacks that only add accidental complexity
instead of adressing the real problem. At least importing within a
method is a simple, well-known and idiomatic solution to circular
imports. The "correct" solution would be to turn models.py into a
package, split its content in submodules (with class A and B in
distinct submodules, each with their own manager), and use the
package's __init__.py to expose the submodule's Model classes at the
top-level, but IIRC this doesn't work well in Django (never had that
problem FWIW, since I tend to split my projects into a lot of small
apps).


--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Circular imports between managers and models

2009-01-06 Thread Ben Eliott

Not that pretty/efficient but you could use contenttype contrib  
temporarily.
IF ClassB has a ForeignKey to Class A could you extract the Class A  
model from  a foreign key via Class B's  .meta?
Or the Class B could have a callable method which returned the  
instance of the model you want...
Not sure whether/how these will work but just throwing out some ideas.



On 6 Jan 2009, at 16:08, bruno desthuilliers wrote:

>
> on 6 jan, 14:54, Polat Tuzla  wrote:
> (snip)
>> The reason why I need to separate models and managers into different
>> files is that they have simply grown to thousands of lines.
>>
> It's indeed a very compelling reason !-)
>
> But (simple suggestion - I know nothing about your project...) don't
> you see it as a sign your app (django meaning) is getting a bit to
> big ? Perhaps refactoring it into a couple or more small apps would be
> a better move ? (once again, just my 2 cents...)
>
>
> >


--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Circular imports between managers and models

2009-01-06 Thread bruno desthuilliers

on 6 jan, 14:54, Polat Tuzla  wrote:
(snip)
> The reason why I need to separate models and managers into different
> files is that they have simply grown to thousands of lines.
>
It's indeed a very compelling reason !-)

But (simple suggestion - I know nothing about your project...) don't
you see it as a sign your app (django meaning) is getting a bit to
big ? Perhaps refactoring it into a couple or more small apps would be
a better move ? (once again, just my 2 cents...)


--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Circular imports between managers and models

2009-01-06 Thread Polat Tuzla

Yes, indeed! It's the models.Manager I'm talking about.
I tried to keep my example short and easily comprehensible without
code snippets, but i think I've achieved the opposite.
Sorry for any inconvenience.

Polat Tuzla

On Jan 6, 3:37 pm, bruno desthuilliers 
wrote:
> On 6 jan, 14:08, Lee Braiden  wrote:
>
>
>
> > On Tue, 2009-01-06 at 04:42 -0800, Polat Tuzla wrote:
> > > Hi,
> > > Suppose I have two classes in "models.py", namely A and B. And there
> > > is the manager for B as BManager in "managers.py". BManager makes use
> > > of clas A.
>
> > > This situation leads to circular imports between "managers.py" and
> > > "models.py" for which I can't find a solution.
>
> > > Assuming that I need to separate models and manager into different
> > > files, so merging them is not an option, are there any best practices
> > > or do you have any other suggestions?
>
> > No expert on Django, and it's hard to tell what your GOAL is from this
> > description, but you might want I'd write something like this:
>
> > class Employee(models.Model):
> >         name = models.CharField(...)
>
> > class Manager(Employee):
> >         manager_stuff...
>
> I think the OP is talking about 
> models.Managers:http://docs.djangoproject.com/en/dev/topics/db/managers/
>
> !-)
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Circular imports between managers and models

2009-01-06 Thread Polat Tuzla

Thank you very much for both of your responses. Local imports solved
my problem.
I had previously tried this without success, apparently there was
another mistake in the code.
Upon Bruno's suggestion I gave it another try, and it worked!

The reason why I need to separate models and managers into different
files is that they have simply grown to thousands of lines.

Regards,
Polat Tuzla


On Jan 6, 3:21 pm, bruno desthuilliers 
wrote:
> On 6 jan, 13:42, Polat Tuzla  wrote:
>
> > Hi,
> > Suppose I have two classes in "models.py", namely A and B. And there
> > is the manager for B as BManager in "managers.py". BManager makes use
> > of clas A.
>
> > This situation leads to circular imports between "managers.py" and
> > "models.py" for which I can't find a solution.
>
> > Assuming that I need to separate models and manager into different
> > files, so merging them is not an option, are there any best practices
> > or do you have any other suggestions?
>
> The import statement works fine in a method too.
>
> class BManager(...):
>    def some_method(self):
>        from models import A
>        # code here
>
> But I have hard time understanding why you couldn't put models and
> managers in the models file...
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Circular imports between managers and models

2009-01-06 Thread bruno desthuilliers

On 6 jan, 14:08, Lee Braiden  wrote:
> On Tue, 2009-01-06 at 04:42 -0800, Polat Tuzla wrote:
> > Hi,
> > Suppose I have two classes in "models.py", namely A and B. And there
> > is the manager for B as BManager in "managers.py". BManager makes use
> > of clas A.
>
> > This situation leads to circular imports between "managers.py" and
> > "models.py" for which I can't find a solution.
>
> > Assuming that I need to separate models and manager into different
> > files, so merging them is not an option, are there any best practices
> > or do you have any other suggestions?
>
> No expert on Django, and it's hard to tell what your GOAL is from this
> description, but you might want I'd write something like this:
>
> class Employee(models.Model):
> name = models.CharField(...)
>
> class Manager(Employee):
> manager_stuff...


I think the OP is talking about models.Managers:
http://docs.djangoproject.com/en/dev/topics/db/managers/

!-)


--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Circular imports between managers and models

2009-01-06 Thread bruno desthuilliers

On 6 jan, 13:42, Polat Tuzla  wrote:
> Hi,
> Suppose I have two classes in "models.py", namely A and B. And there
> is the manager for B as BManager in "managers.py". BManager makes use
> of clas A.
>
> This situation leads to circular imports between "managers.py" and
> "models.py" for which I can't find a solution.
>
> Assuming that I need to separate models and manager into different
> files, so merging them is not an option, are there any best practices
> or do you have any other suggestions?

The import statement works fine in a method too.

class BManager(...):
   def some_method(self):
   from models import A
   # code here


But I have hard time understanding why you couldn't put models and
managers in the models file...
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Circular imports between managers and models

2009-01-06 Thread Lee Braiden

On Tue, 2009-01-06 at 04:42 -0800, Polat Tuzla wrote:
> Hi,
> Suppose I have two classes in "models.py", namely A and B. And there
> is the manager for B as BManager in "managers.py". BManager makes use
> of clas A.
> 
> This situation leads to circular imports between "managers.py" and
> "models.py" for which I can't find a solution.
> 
> Assuming that I need to separate models and manager into different
> files, so merging them is not an option, are there any best practices
> or do you have any other suggestions?

No expert on Django, and it's hard to tell what your GOAL is from this
description, but you might want I'd write something like this:

class Employee(models.Model):
name = models.CharField(...)

class Manager(Employee):
manager_stuff...

class Worker(Employee):
worker_stuff...
manager = models.ForeignKey(Manager,related_name="subordinates",
blank=True, null=True)

class WorkerTypeA(Worker):
workera_stuff..

class WorkerTypeB(Worker):
workerb_stuff...

class ManagerB(Manager):
a_person = ForeignKey(
more_manager_b_stuff...

Again, depending on what your goal is, you might not even need that
a_person field now, since Workers can have managers, and managers can
have subordinates.

-- 
Lee



--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Circular imports between managers and models

2009-01-06 Thread Polat Tuzla

Hi,
Suppose I have two classes in "models.py", namely A and B. And there
is the manager for B as BManager in "managers.py". BManager makes use
of clas A.

This situation leads to circular imports between "managers.py" and
"models.py" for which I can't find a solution.

Assuming that I need to separate models and manager into different
files, so merging them is not an option, are there any best practices
or do you have any other suggestions?

Thanks,
Polat Tuzla
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: When should I do my imports, in views.py?

2008-10-27 Thread Horst Gutmann

According to the Python Coding Guidelines you should always put the
imports at the top of a file simply because it makes the file more
readable. Performance-wise there are AFAIK some small
speed-ramifications (negative ones) when putting imports into a
function instead of having them at the module level. My guess would
be, that this has something to do with the compile-time vs. runtime
issue, not to mention that the import-statement isn't really free. On
the other hand memory usage should be lower if you only import
something if it's really needed (say: inside of a view-function). I
haven't really run any benchmarks, though.

You might want to take a look at
http://www.szgti.bmf.hu/harp/python/fastpython.html :-)

- Horst

On Mon, Oct 27, 2008 at 3:06 PM, 7timesTom <[EMAIL PROTECTED]> wrote:
> I have a large number of view functions which require various python,
> django and other import statements.
>
> My question is: is it ok to put ALL my import statements at the top of
> views.py and then not have to worry about which view fuction uses
> what. Or must I put my imports within the view fuctions that require
> them?
>
> Does it depend on how many functions require I certain import? And if
> so, how many imports justifies a global import at the top of the code?
>
> I'm interested on the effect this decision has on speed/memory usage
> etc.
> >
>

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: When should I do my imports, in views.py?

2008-10-27 Thread Steve Holden

7timesTom wrote:
> I have a large number of view functions which require various python,
> django and other import statements.
>
> My question is: is it ok to put ALL my import statements at the top of
> views.py and then not have to worry about which view fuction uses
> what. Or must I put my imports within the view fuctions that require
> them?
>
>   
It's OK, and indeed that's the normal way of programming.
> Does it depend on how many functions require I certain import? And if
> so, how many imports justifies a global import at the top of the code?
>
>   
One.
> I'm interested on the effect this decision has on speed/memory usage
> etc.
>   
If you want to, you can do some timings with the "timeit" Python module,
but essentially the usual justifications for putting imports inside
functions are

a. The function is very rarely called: putting the import inside the
function means that the import may never have to be executed.

b. Lowering startup overhead: with the import inside the function it
needn't be performed until the function is called, so the program gets
started quicker.

Almost no impact on speed unless you are using a heavily-layered
approach to your programming with many function calls. The import
functionality checks the sys.modules dict first thing to see whether the
module has already been imported. If so then the value of the
sys.modules entry is immediately used to satisfy the import, so there
isn't much overhead.

regards
 Steve


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



When should I do my imports, in views.py?

2008-10-27 Thread 7timesTom

I have a large number of view functions which require various python,
django and other import statements.

My question is: is it ok to put ALL my import statements at the top of
views.py and then not have to worry about which view fuction uses
what. Or must I put my imports within the view fuctions that require
them?

Does it depend on how many functions require I certain import? And if
so, how many imports justifies a global import at the top of the code?

I'm interested on the effect this decision has on speed/memory usage
etc.
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Persistent Global Imports

2008-09-14 Thread Graham Dumpleton



On Sep 15, 1:50 pm, "Chris Spencer" <[EMAIL PROTECTED]> wrote:
> One minor question. I'd like to test multiple mod_wsgi Django apps
> locally, each accessed like localhost/app*. Can I simply use the above
> directives for each app in httpd.conf without using a VirtualHost for
> each app?

Not quite. You need to move the WSGIProcessGroup directive inside of
the Directory container for the specific application.

WSGIDaemonProcess myapp processes=1 threads=25

Alias /myapp/static /usr/local/django/myapp/static/

Order deny,allow
Allow from all


WSGIScriptAlias /myapp /usr/local/django/myapp/apache/django.wsgi

WSGIProcessGroup myapp
Order deny,allow
Allow from all


Do that and you can then duplicate it for any number of applications.

Graham

> Chris
>
> On Sun, Sep 14, 2008 at 11:43 PM, Chris Spencer <[EMAIL PROTECTED]> wrote:
> > Nevermind. I just found your post on this issue at
> >http://code.google.com/p/modwsgi/wiki/ConfigurationIssues
>
> > On Sun, Sep 14, 2008 at 11:32 PM, Chris Spencer <[EMAIL PROTECTED]> wrote:
> >> Ok, so this is what I put at the bottom of my httpd.conf:
>
> >> WSGIDaemonProcess myapp processes=1 threads=25
> >> WSGIProcessGroup myapp
> >> Alias /myapp/static /usr/local/django/myapp/static/
> >> 
> >>    Order deny,allow
> >>    Allow from all
> >> 
> >> WSGIScriptAlias /myapp /usr/local/django/myapp/apache/django.wsgi
> >> 
> >>    Order deny,allow
> >>    Allow from all
> >> 
>
> >> Everything in /myapp/static/* loads fine. However, all other requests
> >> that got to django.wsgi generate this error to my Apache log:
>
> >> [Sun Sep 14 23:26:09 2008] [error] [client 127.0.0.1] (13)Permission
> >> denied: mod_wsgi (pid=344): Unable to connect to WSGI daemon process
> >> 'myapp' on '/etc/httpd/logs/wsgi.28572.2.1.sock' after multiple
> >> attempts.
>
> >> Any thoughts? The above is what the docs use as the standard recipe
> >> for running Django with mod_wsgi. Am I missing anything?
>
> >> Regards,
> >> Chris
>
> >> On Thu, Sep 11, 2008 at 9:06 PM, Graham Dumpleton
> >> <[EMAIL PROTECTED]> wrote:
>
> >>> On Sep 12, 10:35 am, "Chris Spencer" <[EMAIL PROTECTED]> wrote:
>  On Thu, Sep 11, 2008 at 6:16 AM, Graham Dumpleton
>
>  <[EMAIL PROTECTED]> wrote:
>  > Be aware that Apache/mod_wsgi in embedded mode on UNIX is going to be
>  > multiprocess. Thus where you think it is being loaded on every
>  > request, it is more likely just the result of the various processes
>  > loading the application the first time it is used. After they are all
>  > loaded, you shouldn't see loading occurring.
>
>  > Anyway this is it in simple terms, as it is actually more complicated
>  > than that as Apache can kill off processes and replace them in certain
>  > situations.
>
>  > For some details of how processes are used in Apache/mod_wsgi see:
>
>  >  http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading
>
>  > To know what is definitively what is going on, following instructions
>  > in:
>
>  >  http://code.google.com/p/modwsgi/wiki/DebuggingTechniques
>
>  > and set:
>
>  >  LogLevel info
>
>  > in Apache configuration, in place of default 'warn'. This should
>  > result in mod_wsgi outputing a lot of information into main and per
>  > virtual host, as appropriate, error logs about when processes are
>  > being started/restarted and when WSGI application scripts being
>  > loaded.
>
>  > So do that and report on what you see, including examples of Apache
>  > error logs which you believe shows the behaviour you are claiming.
>
>  I set LogLevel, and after each request, the only thing I'm seeing in
>  the log is something like:
>  [Thu Sep 11 20:17:37 2008] [info] mod_wsgi (pid=18858): Create
>  interpreter 'localhost.localdomain|/myapp'.
>  [Thu Sep 11 20:17:37 2008] [info] [client 127.0.0.1] mod_wsgi
>  (pid=18858, process='', application='localhost.localdomain|/myapp'):
>  Loading WSGI script '/usr/local/django/myapp/apache/django.wsgi'.,
>  referer:http://localhost/myapp
>
>  Is there any way to force a specific WSGI app to run on a single
>  process, without changing the number of processes used otherwise on
>  the server? I've toyed around with WSGIDaemonProcess, but all this
>  seems to do is break the debugger without fixing the problem. Would I
>  use something like:
>
>  WSGIDaemonProcess myapp processes=1 threads=1
>
> >>> Which debugger? Any specific reason you are using a single thread,
> >>> such as using older Django version or your application is not
> >>> multithread?
>
> >>> Anyway, the in web browser debuggers I know of will only work if one
> >>> process, but 'processes=1' still marks it as multiprocess in WSGI
> >>> environment. Just drop 'processes=1' and let if use default of 1
> >>> process. If you don't do that the debugger may complain it is not
> >>> running in valid environment. Thus:
>
> >>>

  1   2   >