get_or_create and foreign keys

2008-12-06 Thread felix

this is not the issue previously posted by somebody else.

I think I found a bug with Manager get_or_create and foreign key
fields when specifying with foreigntable__id

class ObjectPermission(models.Model):

permission = models.CharField(blank=False, max_length=20)

user = models.ForeignKey(User,blank=True,null=True)

objects = ObjectPermissionManager()

note that user may be null

class ObjectPermissionManager(models.Manager):

def set_perm(self,permission,user_id):
import pdb; pdb.set_trace()

(perm,created) = self.get_or_create
(permission=permission,user__id=user_id)


(Pdb) user_id
17L

# user_id is incorrect when using get_or_create
(Pdb) self.get_or_create(permission=permission,user_id=user_id)
*** FieldError: Cannot resolve keyword 'user_id' into field. Choices
are: id, permission, user

# using user__id
(Pdb) (op,created) = self.get_or_create
(permission=permission,user__id=user_id)
(Pdb) op


# but user_id was set to null
(Pdb) op.user_id
(Pdb) op.user

here is the crux of the issue :

# if using create, then user_id is correct
(Pdb) self.create(permission=permission,user_id=user_id)

# if using get, then user__id is correct
(Pdb) self.get(permission=permission,user__id=user_id)
*** DoesNotExist: ObjectPermission matching query does not exist.

If someone else can confirm or deny that its a bug then I'll
investigate further and maybe cure it.

thanks

-flix

--~--~-~--~~~---~--~~
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: get_or_create and foreign keys

2008-04-28 Thread davenaff

This might be a syntax problem. This is the syntax I use:

object, created = Entity.objects.get_or_create(id=12)

On Apr 28, 12:12 pm, Thierry <[EMAIL PROTECTED]> wrote:
> The get or create syntax does not appear to support the following
> syntax:
>
> object, created = get_or_create(entity_id = 12)
>
> it fails on the _id part.
> is there anyway to give it numbers instead of 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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



get_or_create and foreign keys

2008-04-28 Thread Thierry

The get or create syntax does not appear to support the following
syntax:

object, created = get_or_create(entity_id = 12)

it fails on the _id part.
is there anyway to give it numbers instead of 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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---