Home > other >  Django combination multiple database, how to obtain keys associated with other tables in the databas
Django combination multiple database, how to obtain keys associated with other tables in the databas

Time:10-02

My project has a blog app, for example, set up a separate database blog_db, blog is a little praise, comments, and foreign key associated with the django's own user table,
Set up, data can be obtained from the blog_db normal blog, but foreign key associated with the user data not available, will be submitted to the following error:

[code] django. Db. Utils. ProgrammingError: (1146, "the Table 'blog_db. Auth_user" doesn' t exist ") [/code]

Errors, according to the user data is read from blog_db this database,

But I want to make is that the effect of blog_db data storage blog, only foreign key associations continue to save data into the original database, such as the database name is my original mysite_db, the user the information from mysite_db this database access, rather than my new blog_db library,

next newbie may describe not clear, I can add, bosses, please help me have a look,

Under this blog application models and view code:

Blog. Models
 
The from the django. Db import models
The from the django. Contrib. Auth. Models import User
The from the django. Contrib. Contenttypes. Fields import GenericRelation
The from the django. Urls import reverse
The from ckeditor_uploader. Fields import RichTextUploadingField
The from read_statistics. Models import ReadDetail

The class BlogType (models. Model) :
The type_name=models. CharField (max_length=20)
Def the __str__ (self) :
Return the self. The type_name

The class Meta:
Blog verbose_name=u "type"
Verbose_name_plural=verbose_name
App_label="blog"

The class Blog (models. Model) :
Title=models. CharField (max_length=30)
The content=RichTextUploadingField ()
Read_details=GenericRelation (ReadDetail)
The author=models. ForeignKey (User, on_delete=models. The CASCADE)
Blog_type=models. ForeignKey (BlogType, on_delete=models. The CASCADE)
Created_time=models. DateTimeField (auto_now_add=True)
Last_updated_time=models. DateTimeField (auto_now=True)
Is_deleted=models. BooleanField (default=False)

Def get_url (self) :
Return reverse (' blog_detail, kwargs={' blog_pk: self. Pk})

"' def get_email (self) :
Return the self. The author. Email "'

Def the __str__ (self) :
Return the self. The title

The class Meta:
Ordering=[' - created_time]
Verbose_name=u "blog"
Verbose_name_plural=verbose_name
App_label="blog"


Blog. Views
 
The from the django. Shortcuts import render, get_object_or_404
The from the django. Core. Paginator import paginator
The from the django. Db. Models import Q, Count
The from the django. Conf import Settings

From the models import Blog, BlogType
The from read_statistics. Views import read_statistics_once_read

Def blog_common_data (request, blogs_all_list) :
Paginator=paginator (blogs_all_list, Settings. EACH_PAGE_NUM)
Page_num=request. GET. GET (' page ', 1)
Page_of_blogs=paginator. The get_page (page_num)
Current_page_num=page_of_blogs. Number

Page_range=list (range (Max (current_page_num - 2, 1), current_page_num)) + \
The list (range (current_page_num, min (current_page_num + 2, paginator. Num_pages) + 1))

# and omit the page number tag
If current_page_num 1 & gt; 3:
Page_range. Insert (0, '... ')
If paginator. Num_pages - current_page_num & gt; 3:
Page_range. Append ('... ')

# add front page and back
If page_range [0]!=1:
Page_range. Insert (0, 1)
If page_range [1].=paginator. Num_pages:
Page_range. Append (paginator. Num_pages)

# get blog category corresponding to the number of blog
Blog_types=BlogType. Objects. All ()
Blog_types_list=[]
For blog_type blog_types in:
Blog_type. Blog_count=Blog. Objects. The filter (blog_type=blog_type). The count ()
Blog_types_list. Append (blog_type)

# get date classification corresponding to the number of blogs
Blog_dates=Blog. Objects. Dates (' created_time ', 'month', order='DESC')
Blog_date_dict={}
For blog_date blog_dates in:
Blog_count=Blog. Objects. The filter (created_time__year=blog_date. Year,
Created_time__month=blog_date. The month). The count ()
Blog_date_dict [blog_date]=blog_count
The context={}
The context [' page_range]=page_range
The context [' blogs']=page_of_blogs. Object_list
The context [' page_of_blogs]=page_of_blogs
The context [' types_num]=blog_types_list
The context [' blog_dates]=blog_date_dict
Return the context

Def type_list (request) :
Blogs_all_list=Blog. Objects. All ()
The context=blog_common_data (request, blogs_all_list)
Return render (request, 'type_list. HTML, context)

Def blog_detail (request, blog_pk) :
Blog=get_object_or_404 (blogs, pk=blog_pk)
Read_cookie_key=read_statistics_once_read (request, blog)

The context={}
The context [' previous_blog]=Blog. Objects. The filter (created_time__gt=Blog. Created_time). The last ()
The context [' next_blog]=Blog. Objects. The filter (created_time__lt=Blog. Created_time). The first ()
The context [' blog ']=blog
The response=render (request, 'blog_detail. HTML, context) # response
Response. Set_cookie (read_cookie_key, 'true') # read cookies tag
Return the response

CodePudding user response:

https://blog.csdn.net/songfreeman/article/details/70229839


Many database Settings, you can have a try
  • Related