Doing "OR" operation with Database API?

2005-11-01 Thread Armin

How can I do 'or' in Django's Database API. Something like this:

select * from message where from = 'armin' OR to = 'armin'

Thank you in advance for your response. You guys are great.

Armin



Re: Catching IntegrityError?

2005-11-01 Thread Clint Ecker

Thanks a bunch! I rewrote my app using the manipulators and such and
it cut out a ton of stuff I'd written!

I guess I should read through the docs a bit more often ;)

Clint

On 11/1/05, Adrian Holovaty <[EMAIL PROTECTED]> wrote:
>
> On 11/1/05, Clint Ecker <[EMAIL PROTECTED]> wrote:
> > In my view, I do something similar to the following (instantate a new
> > Person, fill in the information from a form, and try to save it).  When
> > someone enters a forumsname that's already in the database, I'd like to
> > catch that, and act appropriately.  So initially, I didn't have a try/catch
> > block so I could see what the exception was.  It told me that it would be
> > throwing an "IntegrityError" so I modified my code to look like this:
> > [...]
> > When I try the above code, I get the following error:
> > NameError: global name 'IntegrityError' is not defined
>
> Two things here:
>
> First, you're getting "global name 'IntegrityError' is not defined"
> because IntegrityError isn't in the local namespace when you're
> catching it. You'll need to import it -- probably from psycopg:
>
> from psycopg import IntegrityError
>
> Second, it's bad practice to wrap save() in try/except. The save()
> methods don't perform validation -- they expect data to be valid
> already. Use a manipulator to validate your data. (A manipulator
> encapulates validation and form display.) See the docs here:
> http://www.djangoproject.com/documentation/forms/#custom-forms-and-manipulators
>
> Adrian
>
> --
> Adrian Holovaty
> holovaty.com | djangoproject.com | chicagocrime.org
>


Re: getting id of logged user in admin site

2005-11-01 Thread Flavio Curella

thanks Hugo.

I think this is a common case: I (and peraphs many people) prefer
django to other frameworks because of its admin application: half a
work is already made. I think a feature like this will be very helpful
to many people, so I will open a ticket on django's trac.

Thanks to all,
Flavio Curella.



Re: getting id of logged user in admin site

2005-11-01 Thread hugo

>What I want to do is to save the article with the id of user that is
>saving the article, without the need to select an user/author from a
>menu.

The model code doesn't have access to the request, so you are out of
luck. It's simple to do in your own applications (as you have full
control over your views there), but I don't think it's so simple with
the admin, as that's a prebuilt application and you don't have too much
ways to hook into the view functions - only into the model behaviour.

bye, Georg



Re: How to integrate a search engine in a Django site?

2005-11-01 Thread hugo

>Thanx for the link!!! However, it seems like I cannot search across
>different models even if they have the same attributes that I want
>to search.

My abstract searching stuff (Ian gave the link) does allow searching
over different models - you just add multiple search definitions. The
models even don't need to have the same fields, as you can describe
what fields to use in every search definition.

A sample search definition file for a larger project is in the SVN
repository of my CMS project:

https://simon.bofh.ms/cgi-bin/trac-django-projects.cgi/file/cms/trunk/apps/cms/search.py

It should work with Django trunk, as it doesn't have any translations.
But it might be that some i18n-related dependency crept in, I mostly
run projects from the i18n branch. And documentatin is a bit sparse
currently, you are mostly down to reading the comments and docstrings
and the bit on the wiki (and digging through the CmsProject source
should help, too).

bye, Georg



Re: NullBooleanField & default=Unknown & Unknown not acceptable: how to do it?

2005-11-01 Thread Emanuele

Ouch! Sorry for the confusion : maybe the forces of evil are close to
my house.  ;)

Emanuele



Re: getting id of logged user in admin site

2005-11-01 Thread Flavio Curella

your snippet works, but it returns the id of the author of an articles
already saved in the database.
What I want to do is to save the article with the id of user that is
saving the article, without the need to select an user/author from a
menu.



Re: foreign key lookup problem

2005-11-01 Thread Adrian Holovaty

On 11/1/05, stava <[EMAIL PROTECTED]> wrote:
> Another alternative I tried out in the meantime was to change the
> definition of State taking out the "primary_key = True". That worked
> fine as well.

Yes, that would work. Just for your knowledge, the reason that would
also work is that removing "primary_key = True" in the State model
implicitly creates an "id" field. In that case, state__id__in would be
a valid lookup parameter.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org


Re: foreign key lookup problem

2005-11-01 Thread stava

God you're quick, I just posted this.

Just tested it and it works fine now.

Another alternative I tried out in the meantime was to change the
definition of State taking out the "primary_key = True". That worked
fine as well.

Thanks a bunch!
/LarS



Re: foreign key lookup problem

2005-11-01 Thread Adrian Holovaty

On 11/1/05, stava <[EMAIL PROTECTED]> wrote:
> class State(meta.Model):
>   state = meta.CharField(maxlength = 12, primary_key = True)
>
> class Item(meta.Model):
>   wdate = meta.DateField()
>   state = meta.ForeignKey(State)
>
> % python
> >>> from django.models.ttime import *
> >>> items.get_list(state__id__in = ['Open'])
> [snip]
> TypeError: got unexpected keyword argument 'state__id__in'
>
> I've read the "Field lookups" section of the "Database API reference",
> and my understanding is that the field key should be available. I've
> also tried "state__in", "state_id__in".

"state__id" isn't working because none of your fields are called "id".
The database API uses the field names. In your case, the field name is
"state", so here's what you'd want:

items.get_list(state__state__in=['Open'])

The first "state" refers to the "state" field in Item. The second
refers to the "state" field in State.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org


Re: Catching IntegrityError?

2005-11-01 Thread Adrian Holovaty

On 11/1/05, Clint Ecker <[EMAIL PROTECTED]> wrote:
> In my view, I do something similar to the following (instantate a new
> Person, fill in the information from a form, and try to save it).  When
> someone enters a forumsname that's already in the database, I'd like to
> catch that, and act appropriately.  So initially, I didn't have a try/catch
> block so I could see what the exception was.  It told me that it would be
> throwing an "IntegrityError" so I modified my code to look like this:
> [...]
> When I try the above code, I get the following error:
> NameError: global name 'IntegrityError' is not defined

Two things here:

First, you're getting "global name 'IntegrityError' is not defined"
because IntegrityError isn't in the local namespace when you're
catching it. You'll need to import it -- probably from psycopg:

from psycopg import IntegrityError

Second, it's bad practice to wrap save() in try/except. The save()
methods don't perform validation -- they expect data to be valid
already. Use a manipulator to validate your data. (A manipulator
encapulates validation and form display.) See the docs here:
http://www.djangoproject.com/documentation/forms/#custom-forms-and-manipulators

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org


foreign key lookup problem

2005-11-01 Thread stava

I have difficulties using field lookups on foreign keys. I'm trying to
retrieve a list of items with a specific state using the following
model:

class State(meta.Model):
  state = meta.CharField(maxlength = 12, primary_key = True)

class Item(meta.Model):
  wdate = meta.DateField()
  state = meta.ForeignKey(State)

% python
>>> from django.models.ttime import *
>>> items.get_list(state__id__in = ['Open'])
[snip]
TypeError: got unexpected keyword argument 'state__id__in'

I've read the "Field lookups" section of the "Database API reference",
and my understanding is that the field key should be available. I've
also tried "state__in", "state_id__in".

Anyone have a clue?
/LarS



Catching IntegrityError?

2005-11-01 Thread Clint Ecker
I've got a model that looks like the following and describes a "Person", notice that I require the forumsname to be "unique"---begin code---class Person(meta.Model):    fullname    = 
meta.CharField(maxlength=100,verbose_name="Full Name",blank=False)    forumsname  = meta.CharField(maxlength=100, blank=False, unique=True)    address1    = meta.CharField(maxlength=255, blank=False)
    address2    = meta.CharField(maxlength=255)    city    = meta.CharField(maxlength=255, blank=False)    stateprovince   = meta.CharField(maxlength=100, blank=False)    postalcode  = meta.CharField
(maxlength=32, blank=False)    country = meta.CharField(maxlength=100, blank=False)     emailaddress    = meta.EmailField(blank=False)    notes   = meta.TextField()    beenemailed = 
meta.BooleanField(default=False)    randomkey   = meta.CharField(maxlength=32)    ...--end code--In my view, I do something similar to the following (instantate a new Person, fill in the information from a form, and try to save it).  When someone enters a forumsname that's already in the database, I'd like to catch that, and act appropriately.  So initially, I didn't have a try/catch block so I could see what the exception was.  It told me that it would be throwing an "IntegrityError" so I modified my code to look like this:
---begin code---p = persons.Person( fullname = input['fullname'],    forumsname = input['forumsname'].lower(),    address1 = input['address1'],    address2 = input['address2'],
    city = input['city'],    stateprovince = input['stateprovince'],    country = input['country'],    notes = input['notes'],    postalcode = input['postalcode'],
    emailaddress = input['emailaddress'],    randomkey = GenPasswd2(32) )    if errors == 0:    try:    p.save()    except IntegrityError, e:   # this doesn't work!
    errors = errors + 1    t['forumsnameerror'] = e    ---end code---When I try the above code, I get the following error:NameError: global name 'IntegrityError' is not defined
So which is it?  does it exist or not?  I'm really confused here :)Clint


Re: post_save problem

2005-11-01 Thread stava

OK, thanks, that cleared things up.



Re: pre_save problem

2005-11-01 Thread stava

OK, thanks.



Re: post_save problem

2005-11-01 Thread Adrian Holovaty

On 11/1/05, stava <[EMAIL PROTECTED]> wrote:
> In a foreign key relationship, the main objects pre_save() and
> post_save() both get performed prior to the subordinate object's pre
> and post save, e.g.:
>
> poll._pre_save()
> poll._post_save()
> choice._pre_save()
> choice._post_save()
> choice._pre_save()
> choice._post_save()
>
> Is this intentional?

Yes, this is intentional. Each pre_save() and post_save() is atomic
within its own object's save process. pre_save() and post_save() have
no knowledge of where they are among any larger transaction, and no
knowledge of related objects.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org


Re: pre_save problem

2005-11-01 Thread Adrian Holovaty

On 11/1/05, stava <[EMAIL PROTECTED]> wrote:
> In a foreign key relationsship (like Polls and Choices in the
> tutorial), the _pre_save() definition tries to retrieve the subordinate
> ("choices") items from the database. I really would like to get hold of
> the subordinate items prior to being saved to database (well, it's
> called pre_save, right?) in order to do some simple calculations before
> the items get saved.
>
> Does anyone know how this could be done?

A model's _pre_save() method doesn't have access to any related objects.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org


Re: Problems with Admin interface and one-to-one rels

2005-11-01 Thread Dado


Adrian Holovaty wrote:
> On 10/30/05, Dado <[EMAIL PROTECTED]> wrote:
> > When I added the admin interface via "class META: admin = meta.Admin()"
> > to the models as described in
> >  I
> > cannot get the Admin interface to work properly with these models. I
> > either get errors or strange behavior.
>
> It'd be a great help if you gave details about which errors and
> strange behavior you got. Without that, we can't help much. :)
>
> Adrian
>
> --
> Adrian Holovaty
> holovaty.com | djangoproject.com | chicagocrime.org

Does this issue relate to this ticket?
http://code.djangoproject.com/ticket/527

I can add this related object just fine from a Python interactive
session and play around with them... they just don't work in the Admin.
The other issue I have is that unders restaurants all places are
listed... I would expect only places that are related to restaurants to
appear.

Just my 2c,

Dado



Re: getting id of logged user in admin site

2005-11-01 Thread Joey Coleman

Try the following:

On 10/27/05, Flavio Curella <[EMAIL PROTECTED]> wrote:
>
> hi all, I've this model:
>
> from django.core import meta
> from django.models.auth import User
>
> class Article(meta.Model):
> title = meta.CharField(maxlength=255)
> content = meta.TextField()
> author = meta.ForeignKey(User) // problem is here, I think
> categories = meta.ManyToManyField(Category)
> creation_date = meta.DateTimeField('creation date', auto_now_add=True)
> mod_date = meta.DateTimeField('modification date', auto_now=True)
> file = meta.FileField(upload_to='/Users/go/Sites/django/files/')
>
> class META:
> admin = meta.Admin(
> fields = (
> (None, {'fields': ('title', 'content', 
> 'categories')
> }),
> ),
> )
>
> How can I save the field 'author' with the id of current user? I think
> to use 'default' attribute, something like this:
> author = meta.ForeignKey(User, default=???)
> but how can I get current uesre's id? (user.id doesn't work)
>

  That code above should work fine (I'm using almost precisely that in
something I'm working on).

  Try something like this to get the id:

>>> a = articles.get_list()[0]
>>> auth = a.get_author()
>>> auth.id

cheers,
--joey


post_save problem

2005-11-01 Thread stava

In a foreign key relationship, the main objects pre_save() and
post_save() both get performed prior to the subordinate object's pre
and post save, e.g.:

poll._pre_save()
poll._post_save()
choice._pre_save()
choice._post_save()
choice._pre_save()
choice._post_save()

Is this intentional?

I would have expected the main object pre_save to be performed first,
followed by the subordinate object's pre and post save, followed by the
main objects post_save last, i.e.:

poll._pre_save()
choice._pre_save()
choice._post_save()
choice._pre_save()
choice._post_save()
poll._post_save()

Any hints on how to achieve this order of things?



pre_save problem

2005-11-01 Thread stava

In a foreign key relationsship (like Polls and Choices in the
tutorial), the _pre_save() definition tries to retrieve the subordinate
("choices") items from the database. I really would like to get hold of
the subordinate items prior to being saved to database (well, it's
called pre_save, right?) in order to do some simple calculations before
the items get saved.

Does anyone know how this could be done?



Re: NullBooleanField & default=Unknown & Unknown not acceptable: how to do it?

2005-11-01 Thread Adrian Holovaty

On 11/1/05, James Bennett <[EMAIL PROTECTED]> wrote:
> On 11/1/05, Emanuele <[EMAIL PROTECTED]> wrote:
> > Now I see. So "" means blank value. I missed it in the docs. Thank
> > you again!!
>
> No, the example Adrian provided had the tuple ('', '---') for that
> choice; '---' is what will show up in the admin, but '' is the actual
> value it will try to insert. That's two single quotes with nothing in
> between, which means the value it attempts to save would be an empty
> string. So they'd get thrown back an error and told to select
> something else, because the field doesn't allow blank values.

Yes, what James said. :)

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org


[ANN] Django Page Templates & Exception Formatter

2005-11-01 Thread Stefan H. Holek


Some news for those of us using Zope3 Page Templates with Django:

* DjangoPageTemplates 1.0.2 is available. This is a bugfix release to  
tackle a problem with TALES path expressions. If you have seen  
unexpected NameErrors, this release is for you.




* Page Templates produce a wealth of debug information which is  
however ignored by Django's default exception formatter. The  
django.contrib.exceptionformatter package provides an alternative  
exception formatter implementing the ITracebackSupplement interface  
of Zope3. This is a recommended add-on for all developers working  
with Page Templates.




Stefan

--
Anything that happens, happens.  --Douglas Adams





Re: getting id of logged user in admin site

2005-11-01 Thread Grigory Fateyev

Hello Flavio Curella!
On Tue, 01 Nov 2005 03:33:01 -0800 you wrote:

> 
> 
> Grigory Fateyev wrote:
> > Hello Flavio Curella!
> > On Thu, 27 Oct 2005 20:20:23 - you wrote:
> >
> > >
> > > hi all, I've this model:
> > >
> > > from django.core import meta
> > > from django.models.auth import User

from django.models import auth, core

> > >
> > > class Article(meta.Model):
> > >   title = meta.CharField(maxlength=255)
> > >   content = meta.TextField()
> > >   author = meta.ForeignKey(User) // problem is here, I think
> > author = meta.ForeignKey(auth.User) # May be?
> >
> 
> no, :( --> NameError: name 'auth' is not defined
> 
> and with:
> 
> from django.models import auth
> 
> --> NameError: name 'User' is not defined
> 
> Other ideas?
> 


-- 
Всего наилучшего!
greg [at] anastasia [dot] ru Григорий.


Re: getting id of logged user in admin site

2005-11-01 Thread Flavio Curella


Grigory Fateyev wrote:
> Hello Flavio Curella!
> On Thu, 27 Oct 2005 20:20:23 - you wrote:
>
> >
> > hi all, I've this model:
> >
> > from django.core import meta
> > from django.models.auth import User
> >
> > class Article(meta.Model):
> > title = meta.CharField(maxlength=255)
> > content = meta.TextField()
> > author = meta.ForeignKey(User) // problem is here, I think
>   author = meta.ForeignKey(auth.User) # May be?
>

no, :( --> NameError: name 'auth' is not defined

and with:

from django.models import auth

--> NameError: name 'User' is not defined

Other ideas?



Re: NullBooleanField & default=Unknown & Unknown not acceptable: how to do it?

2005-11-01 Thread James Bennett

On 11/1/05, Emanuele <[EMAIL PROTECTED]> wrote:
> Now I see. So "" means blank value. I missed it in the docs. Thank
> you again!!

No, the example Adrian provided had the tuple ('', '---') for that
choice; '---' is what will show up in the admin, but '' is the actual
value it will try to insert. That's two single quotes with nothing in
between, which means the value it attempts to save would be an empty
string. So they'd get thrown back an error and told to select
something else, because the field doesn't allow blank values.


--
"May the forces of evil become confused on the way to your house."
  -- George Carlin


Re: NullBooleanField & default=Unknown & Unknown not acceptable: how to do it?

2005-11-01 Thread Emanuele

Now I see. So "" means blank value. I missed it in the docs. Thank
you again!!

Emanuele