define database modelwise in django

25 views
Skip to first unread message

kartik danidhariya

unread,
Aug 2, 2017, 7:32:42 AM8/2/17
to Django users

I just tried to find a solution for define database model wise in Django. like in my settings.py there are three databases

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'db_one': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db_one.sqlite3'),
    },
    'db_two': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db_two.sqlite3'),
    },
}

And in my polls/models.py

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')


    def __str__(self):
        return self.question_text

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

@python_2_unicode_compatible
class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)


    def __str__(self):
        return self.choice_text

now I want to add question model in db_one database and Choice model in db_two database so how can I do that

I try with routers follow this multiple databases and multiple models in Django but it prefers only default database only after that i tried with put blank setting in default database and try ti migrate but it gives me an error

threes...@gmail.com

unread,
Aug 2, 2017, 7:38:02 AM8/2/17
to django...@googlegroups.com
Have a look here:


Basically:

>>> # This will run on the 'default' database.
>>> Author.objects.all()

>>> # So will this.
>>> Author.objects.using('default').all()

>>> # This will run on the 'other' database.
>>> Author.objects.using('other').all()

--
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 post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/10ce81e3-674f-4c2e-b4ad-0215f3d1aff2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

SHAILESH NEGI

unread,
Aug 2, 2017, 7:57:21 AM8/2/17
to django...@googlegroups.com
You can create it from dbshell using different database credentials.
> @python_2_unicode_compatibleclass Choice(models.Model):
> question = models.ForeignKey(Question, on_delete=models.CASCADE)
> choice_text = models.CharField(max_length=200)
> votes = models.IntegerField(default=0)
>
>
> def __str__(self):
> return self.choice_text
>
> now I want to add question model in db_one database and Choice model in
> db_two database so how can I do that
>
> I try with routers follow this multiple databases and multiple models in
> Django
> <https://stackoverflow.com/questions/18547468/multiple-databases-and-multiple-models-in-django>
> but
> it prefers only default database only after that i tried with put blank
> setting in default database and try ti migrate but it gives me an error
>
> --
> 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 post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/10ce81e3-674f-4c2e-b4ad-0215f3d1aff2%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>


--
Thanks & Regards,
SHAILESH NEGI
Reply all
Reply to author
Forward
0 new messages