CreateView for non default database

51 views
Skip to first unread message

Muhammad Juwaini Abdul Rahman

unread,
Jul 25, 2023, 11:11:19 PM7/25/23
to Django users
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?

Sebastian Jung

unread,
Jul 26, 2023, 3:22:48 PM7/26/23
to django...@googlegroups.com
Hello ,

I found this tutorial:


But i self don't use several databases

Regards

--
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...@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.

David Nugent

unread,
Jul 27, 2023, 6:31:33 AM7/27/23
to django...@googlegroups.com
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

--

ivan harold

unread,
Aug 9, 2023, 10:05:44 AM8/9/23
to Django users
Did you try what  David Nugent suggested? Did it worked?
Reply all
Reply to author
Forward
0 new messages