Seems like a geodjango bug with multiple databases

2015-02-18 Thread Luan Nguyen
I'm using geodjango and multiple databases, and experiencing a quite weird 
situation, I guess it's a kind of bug but not absolutely sure since I'm 
pretty new to Django.

Here is how to reproduce the problem:
- Set up another database besides default:
DATABASES = {
'default': {
'ENGINE' : 'django.contrib.gis.db.backends.postgis' 
,
'NAME' : 'issue1',
'USER' : 'user',
'PASSWORD' : 'password',
'HOST' : '127.0.0.1',
'OPTIONS' : {
'autocommit' : True,
}
},
'another': {
'ENGINE' : 'django.contrib.gis.db.backends.postgis' 
,
'NAME' : 'issue2',
'USER' : 'user',
'PASSWORD' : 'password',
'HOST' : '127.0.0.1',
'OPTIONS' : {
'autocommit' : True,
}
},
}

And two models:
from django.db import models as default_models
from django.contrib.gis.db import models
# Create your models here.
class Hotel(models.Model):
hotel_name = models.CharField(max_length=255)
objects = models.GeoManager()

class Room(models.Model):
room_num = models.IntegerField()
hotel = models.ForeignKey(Hotel, null=True, blank=True)

Add data into issue2 database (leave issue1 blank), then go into shell:
>>>h = Hotel.objects.using('another').all()[0]
>>> h.id
9
>>>h.room_set.all()[0].id  #=> room id 
10 links to hotel id 9
10
>>>r = Room.objects.using('another').get(pk=10)
>>>r.hotel
Traceback (most recent call last):
File "", line 1, in 
File "/
Applications/MAMP/htdocs/Rainmaker/vir341/lib/python3.4/site-packages/django/db/models/fields/related.py
 
",
 
line 572, in __get__
rel_obj = qs.get()
File "/
Applications/MAMP/htdocs/Rainmaker/vir341/lib/python3.4/site-packages/django/db/models/query.py
 
",
 
line 357, in get
self.model._meta.object_name)
multi.models.DoesNotExist: Hotel matching query does not exist.

The thing is, if I create a hotel record on database issue1 with id of 9 
then the last command works, so I guess it tried to look up in default 
database. This doesn't have any problems if I use default manager for 
Hotel, so I guess it's a bug?

-- 
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/daa291e8-bf87-422a-a8fc-b23038e94268%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Seems like a geodjango bug with multiple databases

2015-02-21 Thread Luan Nguyen
Hi, sorry for my late response.

Thanks for replying.

I'm using django 1.7.4.

Going with this 
Room.objects.using('another').select_related('hotel').get(pk=10) is fine, I 
can then get room.hotel without any problem.

I also followed Jani's advice and changed default manager of Room to 
GeoManager, but still got that error.

@George: I actually had this issue in one of my app (running django 1.7), 
but not sure whether it's due to my code or django, so I set up a 
completely new project as described above (with django 1.7.4), and still 
get the error. Postgis is enabled on both databases (will get error if it 
doesn't anyway).

I was trying to trace back the code but don't have enough experience to 
fully understand the problem. I'll ask geodjango list as Collin suggested 
to see if someone experienced the same thing.

Luan

-- 
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/bebe1636-c8a8-4026-bb11-578f29220149%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.