Thursday, July 27, 2023

Re: CreateView for non default database

Database Router is the trick for this. Django calls this function to determine which database the transaction will use based on the type of operation requested, so you can separate which database is selected for reads, writes and migrations.

Regards,
David

On Wed, Jul 26, 2023 at 1:11 PM Muhammad Juwaini Abdul Rahman <juwaini@gmail.com> wrote:
Hi all,

I have two different databases as below:

DATABASES = {
'tvet': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
},
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'jpk',
'USER': 'jpkuser',
'PASSWORD': 'jpkpassword',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}

When I want to create a CreateView, I am unable to save it.

This is my CreateView:

class BiodataCreateView(CreateView):
template_name = 'bio_create.html'

# model = Biodata.objects.using('tvet').all()
def get_queryset(self):
return Biodata.objects.using('tvet').all()

fields = 'name', 'hobby'

def get_success_url(self):
return reverse('biodata-list')

Any idea on how to do it?

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4985efd7-437c-432e-86c8-9a902430513fn%40googlegroups.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAE5VhgUAewdcBZWdB-5Typ-UR9NKgXhWLhq64oTd3YDSJNxEDA%40mail.gmail.com.

No comments:

Post a Comment