Re: Import error

2012-06-22 Thread Adrian Bool

Hi Emily,

On 22 Jun 2012, at 15:46, Emily wrote:

> This is the class I created...
> 
> import string
> import random
> 
> class Helpers:
> 
> def random_password():

Take random_password outside of the Helpers class and you should be OK.  On the 
import statement you need to refer to a 'top level object' within the imported 
file.  The only top level object you have is the class  Helpers - which it 
doens't look like you really need.

Cheers,

aid

-- 
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: ForeignKey problem

2012-06-25 Thread Adrian Bool


On 25 Jun 2012, at 17:41, Soviet wrote:
> I'm new to this Django thing and I run into first problem :).
> 
> Let's say I have two models and in each I have field with ForeignKey
> relating to field in other model (hope it's clear). Now that I want to
> run migrate with South, I'm getting "NameError: name 'ModelName' is
> not defined". This is clearly (?) an issue with the fact that one
> model is on top of the other one and second Model hasn't been defined
> yet. Changing position of the models is, obviously, not helping. How
> do I approach this?

In the first of the two models, use a string containing the second model's name 
as the first argument to the ForeignKey method.

Regards,

aid

-- 
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: ForeignKey problem

2012-06-25 Thread Adrian Bool

On 25 Jun 2012, at 17:53, Soviet wrote:

> Thank you kind sir for your fast response, that worked brilliantly.
> Can I be cheeky and ask why does it work? :)

Magic! ;-)

Although seriously, Django obviously has some code in there to handle just the 
situation you have come across.  Sorry, I don't have a deeper answer than that!

Cheers,

aid


-- 
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: Generating random alphanumeric codes

2012-06-26 Thread Adrian Bool

On 26 Jun 2012, at 15:53, Sithembewena Lloyd Dube wrote:
> Would anyone have tips on how to generate random 4-digit alphanumeric codes 
> in python? Also, how does one calculate the number of possible combinations?

For upper case, lower case and digits we have,

26 + 26 + 10 = 62 

...possibilities for each position.  If you have four positions you have,

62 * 62 * 62 * 62 = 14776336

... possible combinations.

You can create a random number in this range by,

import Random from random

def build_population(spans):
for span in spans:
for x in range(ord(span[0]), ord(span[1]) + 1):
yield chr(x)

population = list(build_population( (('A','Z'), ('a', 'z'), ('0', '9')) 
))

r = Random()

r.sample(population, 4)
['C', 'l', 'q', 'y']

You may need to look into getting some entropy into the random number 
generator...

Cheers

aid

-- 
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: Object Oriented or Relational Database?

2011-01-26 Thread Adrian Bool

Hi,

I'm creating a DB using Django for managing our companies data - customers, 
services, orders, billing etc.  I'm using the Django admin interface to manage 
the data.

I seem to be consistently at odds with myself and would be good to hear the 
community's point of view on this.

I'm defining the DB in Python; using objects and inheritance.  As such, when 
I'm doing this I'm thinking in an object orientated (OO) manner - a service 
uses a number of resources so I create a Service object with with either 
foreign key or many-to-many (as appropriate) relationship to the Resource 
objects.  This gives me nice referential integrity and I can be sure that the 
Service has valid connections to the Resources it requires.

It seems, however, that Django expects the models not to be defined in an OO 
manner.  For example; should I wish resources to be listed as Inlines on the 
Service's page  I can only do this if the foreign key is with the Resource - 
pointing 'up' the stack to the services - as possible to being pointed to by 
the Service.  Django seems to be defining stricture using an OO language; but 
expecting that structure in a more traditional, relational database, style 
where entries point up to their owners rather than the owners pointing down to 
their children.

In order to try to fit in with Django Admin I've ended up using both styles.  
Unsurprisingly this mixture of styles is now causing me problems with circular 
dependencies.  I know I need to jump one way or the other; but would be great 
to get some comments from experienced Django coders before I decide which way...

Any comments/advice welcome...

Cheers,

aid

-- 
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: Object Oriented or Relational Database?

2011-01-26 Thread Adrian Bool

On 26 Jan 2011, at 11:55, Daniel Roseman wrote:
> 
> This is what's known as the object-relational impedance mismatch [1]. 
> Unfortunately, as you've noticed, OO concepts don't map completely cleanly 
> onto the relational model, and this is an issue with all systems that attempt 
> to do it. Django does what it can to smooth over the gaps, but can't always 
> do everything.

Thanks for the link; interesting reading.

> However, you should not be getting circular dependencies. Can you provide 
> some examples? One place where I often see them is when two models in 
> separate applications are related via ForeignKey, and the developer has 
> imported each model into the other's models.py. But it isn't necessary to 
> import them at all, as you can refer to models via strings: 
> 
> myfkfield = models.ForeignKey('otherapp.OtherModel')
> 
> so no circular dependency is declared. Does that solve your problem? If not, 
> some examples will be helpful.


I've used the string form for forward references of models; but I still seem to 
get problems when performing the initial migration (using South) of the 
database as the table to which the forward reference points does not exist at 
the time of creating the initial table.

I've just tried using a basic syncdb and that works fine.  I presume I'm seeing 
problems as South's migrations work one app at a time; in contrast to syncdb 
that seems to install the whole project - only worrying about dependencies at 
the end.

Still; I feel circular dependencies are bad; even work arounds are possible.

Is there a recommended path to take with Django?

Kind regards,

aid



-- 
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: Customising the Admin Save Models in DJango 1.1

2011-01-26 Thread Adrian Bool

Hu,

On 26 Jan 2011, at 07:22, Prof Falken wrote:
> I have two models, the first is a 'change request ticket' and the
> second reflects the status of that ticket.  They are linked by a
> ForeignKeyField on the first table.

You may find it best to collapse them into a single object; using the 'choice' 
option to allow the user to set the ticket's state.

> The two problems that I have are:
> 
> 1) Some of the 'states' should mark the ticket as closed, so when the
> ticket is saved with the state of 'closed', it should be marked as
> 'completed' with the date and time the action was carried out.

You can jump in and change the model's data before the model is saved by 
over-riding the 'save' method in your model.

Below is one of my models.  My frequency attribute could be quite similar to 
what you need for your state field.  Below you can see where I'm setting a 
number of my attributes (based upon the parent object when the Charge object is 
created as an Inline) before this Charge is saved,

class Charge (models.Model):
CHARGE_FREQUENCY_CHOICES = (
(u'NRC', u'Non-Recurring'),
(u'MRC', u'Monthly'),
(u'QRC', u'Quaterly'),
(u'ARC', u'Annual')
)

frequency = models.CharField(max_length=15, 
choices=CHARGE_FREQUENCY_CHOICES)
   

def save(self, *args, **kwargs):
self.description = self.service
self.customer = self.service.customer
self.legacy_charge_id = None
self.prorata = False
self.created_date = datetime.date.today()
self.started_date = self.service.completed_date
self.expiry_date = self.service.expiry_date
self.terminated_date = self.service.terminated_date
super(Charge, self).save(*args, **kwargs)

In order that the user does not set (or need to set) the above fields, set the 
fields attribute of the Admin object so only the values you want the user to 
control are visible.  Here's mine,

class ChargeInlineAdmin(admin.TabularInline):
fields = ('frequency','amount', 'currency', 'vat_chargeable')
model = Charge
extra = 0


Regards,

aid

Adrian Bool
a...@logic.org.uk



-- 
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: forbid clones

2011-01-26 Thread Adrian Bool

On 26 Jan 2011, at 12:26, Jaroslav Dobrek wrote:

>> 
>> http://docs.djangoproject.com/en/1.2/ref/models/fields/#unique
> Example:
> 
> This should be allowed:
> 
> car1: manufacturer = "foo", name = "bar"
> car2: manufacturer = "foo", name = "baz"
> 
> This should not be allowed:
> 
> car1: manufacturer = "foo", name = "bar"
> car2: manufacturer = "foo", name = "bar"

Doesn't look to be directly possible from the Django's API; but I guess you 
could,

class Car(models.Model):
manufacturer = models.CharField(max_length=127)
name = models.CharField(max_length=127)
manufacturer_name = models.CharField(max_length=256, unique=True)

def save(self, *args, **kwargs):
manufacturer_name = "%s_%s" % (self.manufacturer, self.name)
super(Charge, self).save(*args, **kwargs)

aid

-- 
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: forbid clones

2011-01-26 Thread Adrian Bool

Please ignore this dumn-ass p
On 26 Jan 2011, at 12:34, Adrian Bool wrote:

> 
> On 26 Jan 2011, at 12:26, Jaroslav Dobrek wrote:
> 
>>> 
>>> http://docs.djangoproject.com/en/1.2/ref/models/fields/#unique
>> Example:
>> 
>> This should be allowed:
>> 
>> car1: manufacturer = "foo", name = "bar"
>> car2: manufacturer = "foo", name = "baz"
>> 
>> This should not be allowed:
>> 
>> car1: manufacturer = "foo", name = "bar"
>> car2: manufacturer = "foo", name = "bar"
> 
> Doesn't look to be directly possible from the Django's API; but I guess you 
> could,
> 
> class Car(models.Model):
>   manufacturer = models.CharField(max_length=127)
>   name = models.CharField(max_length=127)
>   manufacturer_name = models.CharField(max_length=256, unique=True)
> 
>def save(self, *args, **kwargs):
>manufacturer_name = "%s_%s" % (self.manufacturer, self.name)
>super(Charge, self).save(*args, **kwargs)
> 


Urgh.  Please ignore this dis-information.  Hat off to Chris for knowing what 
he types.

aid


-- 
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: Object Oriented or Relational Database?

2011-01-26 Thread Adrian Bool


Hi Bruno,

On 26 Jan 2011, at 12:55, bruno desthuilliers wrote:
> Some will talk about "OO/relational impedance mismatch" (ok, I cheated
> - Daniel just did;)), but I don't see it that way as far as I'm
> concerned - I'm using a relational model wrapped into an OO
> representation, and that's just what I want for most apps.

Many thanks for your comments; although it wasn't really what I wanted to hear 
- as I prefer the OO model over the relational.  I now feel that if I decide to 
use Django - not following a relational style will only cause me difficulties.

Kind regards,

aid

-- 
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: problem with compressed egg in mysqldb module

2011-01-26 Thread Adrian Bool


On 26 Jan 2011, at 23:24, Cindy wrote:

> 
> On Jan 26, 2:13 pm, Cindy  wrote:
>> Hi, all.  I'm getting this error, when trying to export my django site
>> to a new place:

Do you mean to a new server?

>> 
>> TemplateSyntaxError: Caught ImproperlyConfigured while rendering:
>> Error loading MySQLdb module: bad local file header in /usr/local/lib/
>> python2.6/site-packages/MySQL_python-1.2.3-py2.6-freebsd-8.1-RELEASE-
>> amd64.egg

If so; is the destination server running a 64bit OS? 

Regards,

aid

-- 
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: Customising the Admin Save Models in DJango 1.1

2011-01-27 Thread Adrian Bool

On 27 Jan 2011, at 13:28, Matthew Macdonald-Wallace wrote:
>def save(self,*args,**kwargs):
>if not self.id:
>self.created = datetime.date.today()
>if self.Status.ClosesChangeRequest == True:
>self.completed = True
>super(ChangeHeader, self).save(*args,**kwargs)
> 
> but I assume the issue I'm running in to is that the Save handler
> doesn't know the current state of the Status field.
> 
> Is there a better approach which avoids hard-coding?

If the user had selected a valid ChangeStatus for the Status attribute on the 
change form; then I would expect you to be able to query the Status in the way 
you do above (although my situation was slightly different; so just because I 
would't expect something doesn't mean that is the way it is ;-)). 

If you run with the above code, do you get an Exception?

aid


-- 
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: pyodbc: Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnectW)')

2011-01-30 Thread Adrian Bool

On 30 Jan 2011, at 09:31, Orgil wrote:

> My code is below:
> ---
> import pyodbc
> conn = pyodbc.connect('DRIVER={SQL Server};SERVER=testserver
> \mssql2008;DATABASE=eoffice;UID=erp;PWD=123')
> ---

I'd try replacing 'testserver\mssql2008' with a domain name that your ubuntu 
box can ping - or the IP address of the Windows 7 box.

Regards,

aid

-- 
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: XML parsing

2011-01-31 Thread Adrian Bool

Hi,

Remove any carriage returns and extra spaces from your binary.xml file, like so,

http://schemas.xmlsoap.org/soap/envelope/";>http://flypp.infy.com/sms/v2010r1_0";>12345343661t1p1p2DICT
 
test


Seems to parse ok then.

aid

On 31 Jan 2011, at 09:18, sami nathan wrote:

> HI 
>I am trying to parse my response  by the following method
>   elif request.method == 'POST':
>reqp=request.raw_post_data
>response = HttpResponse(mimetype='text/xml')
>response.write(reqp)
>print response
>xmldoc = minidom.parse('binary.xml')
>reflist = xmldoc.getElementsByTagName('message')
>print reflist
>return response 
>  But its giving me error of "TO MANY VALUE TO UNPACK"
> --
> my request looks like this
> 
>  xmlns:soapenv="http://sc
> hemas.xmlsoap.org/soap/envelope/"> xmlns:ns1="http://flypp.infy.com/sms/v2010r1_0";>1234534366<
> applicationID>1t1p
> 1p2DICT 
> test ns1:receiveShortMessageRequest>
> 
> 
> Now my ultimate aim is to send error  response to client sending me the above 
> request without DICT in message 
> 
> -- 
> 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.

Adrian Bool
a...@logic.org.uk



-- 
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: working django with existing database

2011-01-31 Thread Adrian Bool

Arief,

Just do a search and replace in your text file along the lines of,

search for,

max_digits=0, decimal_places=-127

replace,

max_digits=10, decimal_places=2

And Django should be happy.

aid

On 31 Jan 2011, at 10:29, arief nur andono wrote:

> oh please...
> 
> i need only to query the table, have no access to modified the table
> 
> why there is no way query the database and then make the output as object??
> 
> 2011/1/31 Ian 
> On Jan 31, 12:20 am, arief nur andono 
> wrote:
> > class TempJmlGangguanPyl(models.Model):
> > singkatpyl = models.CharField(max_length=4, blank=True)
> > singkatgrd = models.CharField(max_length=4, blank=True)
> > jumlah_gangguan = models.DecimalField(null=True, max_digits=0,
> > decimal_places=-127, blank=True)
> > lama_gangguan = models.DecimalField(null=True, max_digits=0,
> > decimal_places=-127, blank=True)
> > class Meta:
> > db_table = u'temp_jml_gangguan_pyl'
> 
> It appears that your numeric columns are all decimal-precision
> floating-point (i.e. "FLOAT" or "NUMBER" with no precision or scale
> specified), which is a bit of a corner case.  The basic problem is
> that Django core has no field for this.  DecimalField can only handle
> fixed-point, and FloatField uses binary precision.  If these columns
> are actually used to store fixed-point data, you could manually update
> the generated models (or preferably the original schema) with the
> actual precision and scale of the data.  If they are truly floating-
> point, then you would probably need to write a custom DecimalField
> class.  I wouldn't recommend changing them to FloatField in any case,
> because that could result in a loss of precision.
> 
> A similar problem can be encountered with "NUMBER(p, s)" columns where
> s < 0 or s > p.  Both these scenarios are perfectly valid in Oracle,
> but are (somewhat arbitrarily) disallowed by the DecimalField
> validation.
> 
> I'll make a note of this.  At the very least, there should be a more
> descriptive error message for when this comes up.
> 
> Ian
> 
> --
> 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.

Adrian Bool
a...@logic.org.uk



-- 
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: DRY and static attribute for multiple classes.

2011-02-01 Thread Adrian Bool

On 1 Feb 2011, at 21:05, Marc Aymerich wrote:

> Hi all,
> I want to provide an encapsulated static attribute called _registry
> for several classes.
> 
> I try to use inheritance in order to make it DRY: all classes inherit
> from a BaseClass that implements the _registry encapsulation. But with
> inheritance it doesn't work how I want, because a single instance of
> the _registry is shared between all of the inherited classes, and I
> want to have an independent _registry for every class.
> 
> How can I do that without coping all the code in every class?


Create your base class with the _registry attribute like so,

class MyBaseClass(models.Model)
_registry = models.IntegerField()
class Meta:
abstract = True

With 'abstract = True'; when you inherit from MyBaseClass the child classes 
will have their own _registry columns in the DB (rather than sharing a common 
table).

Cheers,

aid


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