GenericForeignKey not working across multiple databases
|Reported by:||bartek||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Description (last modified by kmtracey)
It seems that you can't use a GenericForeignKey across multiple databases. My model is as follows:
class Item(Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey() # Other model fields, irrelevant to this example.
From my tests, the following two scenarios happen:
Scenario 1: I use the content_object approach, which uses the actual GenericForeignKey field to handle the data.
# Accessory is from a secondary database. I use routers to select it from the second database.
>>> a = Accessory.objects.get(pk=3) >>> a <Accessory: 1019 - Blue> >>> i = Item(content_object=a, title="Test item") >>> i.save() >>> item = Item.objects.get(pk=1) >>> item.content_object <ATotallyDifferentModel: some_other_value> >>> item.object_id 3L # This is correct
So it looks like it doesn't pick the right table to work with. It picks another model from the primary database, not going for the second database.
Scenario 2: I don't use content_object and set the id/type manually.
>>> ctype = ContentType.objects.get_for_model(Accessory) >>> fi = Item.objects.create(object_id=a.pk, content_type=ctype, title="Some real item.") Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/bartek/.virtualenvs/internal/lib/python2.6/site-packages/django/db/models/manager.py", line 138, in create return self.get_query_set().create(**kwargs) File "/home/bartek/.virtualenvs/internal/lib/python2.6/site-packages/django/db/models/query.py", line 352, in create obj.save(force_insert=True, using=self.db) TypeError: save() got an unexpected keyword argument 'using'
So at this point I am at a loss. I asked around and no one knows if this is a bug or if I'm doing something wrong, so I am assuming it's a bug.
Change History (3)
comment:1 follow-up: ↓ 2 Changed 5 years ago by kmtracey
- Description modified (diff)
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset