문제

I'm new to Django (using version 1.5.1) and have been struggling with this issue.

I have two models: Question and Image. Each Question can have many Images, and each Image can be related to only one Question. I'm trying to make an admin interface, where one can add Questions and add multiple Images to each Question.

The problem is that I have two databases with separate reading and writing schemes. When an anonymous user reads data, it should use the default database and this works. Admin on the other hand should both read and write data to a secondary database called 'remote' through the admin interface. The reason for this is too complicated to explain.

Anyways, I need to save both Questions and Images to the 'remote' database. Questions are saved fine there, but Images are saved to the default database for some reason. I thought that formfield_for_foreignkeys functions should work. It reads from the 'remote' database as it should (because of the inline query-function?), but of course no images are found. A query to the default database finds the images, but no Questions.

Here's my code

from django.contrib import admin
from questionservice.models import Question, Image

class ImageTabularInline(admin.TabularInline):
    model = Image
    extra = 1
    using = 'remote'

    def queryset(self, request):
        return super(ImageTabularInline, self).queryset(request).using(self.using)

    def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
        return super(ImageTabularInline, self).formfield_for_foreignkey(db_field, request=request, using=self.using, **kwargs)

    def formfield_for_manytomany(self, db_field, request=None, **kwargs):
        return super(ImageTabularInline, self).formfield_for_manytomany(db_field, request=request, using=self.using, **kwargs)

class QuestionModelAdmin(admin.ModelAdmin):
    inlines = [ImageTabularInline]
    using = 'remote'

    def save_model(self, request, obj, form, change):
        obj.save(using=self.using)

    def delete_model(self, request, obj):
        obj.delete(using=self.using)

    def queryset(self, request):
        return super(QuestionModelAdmin, self).queryset(request).using(self.using)

    def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
        return super(QuestionModelAdmin, self).formfield_for_foreignkey(db_field, request=request, using=self.using, **kwargs)

    def formfield_for_manytomany(self, db_field, request=None, **kwargs):
        return super(QuestionModelAdmin, self).formfield_for_manytomany(db_field, request=request, using=self.using, **kwargs)

admin.site.register(Question, QuestionModelAdmin)

I also tried using routers, but I didn't find a way to route admin interface to read from other database.

Any ideas? Thank you!

도움이 되었습니까?

해결책

OK, so if you can only make writes to the networked db, then you can only make changes when you are on the network anyway. So instead of dicking around with which db to write to, split your config into 2 parts.

  • A server that can only access the admin, which has it's default database pointed at the network sqlitedb.
  • A "Client" that runs on the laptops, which can't access the admin, which has it's default database pointed at the local sqlite db.

Your file structure could look like this:

|-- project
| |-- static
| |-- templates
| |-- settings
| | |-- __init__.py
| | |-- base.py    # All your common settings
| | |-- server.py  # DATABASES = {'default': 'NAME':'\\server\netshare\remote.sq3'}}
| | `-- client.py  # DATABASES = {'default': 'NAME':'local.sq3'}}
| |-- urls_server.py # anchors admin to root url
| |-- urls_client.py # does not embed admin
| `-- wsgi.py
`-- manage.py

and you can run them with

./manage.py runserver --settings=project.settings.server
  OR
./manage.py runserver --settings=project.settings.client
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top