On Wed, 18 Feb 2015 06:45:15 -0800 (PST)
Luan Nguyen <
lih...@gmail.com> wrote:
> 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'
> <
http://django.contrib.gis.db.backends.postgis%27/>,
> 'NAME' : 'issue1',
> 'USER' : 'user',
> 'PASSWORD' : 'password',
> 'HOST' : '127.0.0.1',
> 'OPTIONS' : {
> 'autocommit' : True,
> }
> },
> 'another': {
> 'ENGINE' : 'django.contrib.gis.db.backends.postgis'
> <
http://django.contrib.gis.db.backends.postgis%27/>,
> '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)
You don't have GeoManager on Room model? It's required to be default (first) manager if you do queries that even relate to geo-enabled model.
https://docs.djangoproject.com/en/1.7/ref/contrib/gis/model-api/#geomanager
And there reads: "It should also be noted that GeoManager is required even if the model does not have a geographic field itself, e.g., in the case of a ForeignKey relation to a model with a geographic field."
> 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 <
http://h.room_set.all%28%29[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 "<console>", line 1, in <module>
> File "/
> Applications/MAMP/htdocs/Rainmaker/vir341/lib/python3.4/site-packages/django/db/models/fields/related.py
> <
http://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
> <
http://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?
>
> To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/daa291e8-bf87-422a-a8fc-b23038e94268%40googlegroups.com.
Jani Tiainen