Django 删除数据库(Python 管理迁移)

问题描述 投票:0回答:1

我正在使用 Django。

运行 python manage migrate 后,数据库已正常创建。然而,一段时间后,数据库突然消失了。我的代码中不存在 drop 命令。

我很好奇为什么数据库被删除了。所有数据均已删除。 (数据库、集合、字段)

有谁知道原因吗?

这是我的代码

下面的代码是应用程序/模型

型号

from djongo import models

class RealTime(models.Model):
    _id = models.CharField(max_length=255, primary_key=True)
    site = models.CharField(max_length=125)
    title = models.CharField(max_length=255)
    url = models.URLField()
    create_time = models.DateTimeField()
    GPTAnswer = models.TextField()
    
    class Meta:
        db_table = 'realtimebest'

class Daily(models.Model):
    rank = models.IntegerField()
    title = models.CharField(max_length=255)
    url = models.URLField()
    create_time = models.DateTimeField()

下面的代码是架构:

架构

import graphene
from graphene_django.types import DjangoObjectType
from graphene import Mutation

from .views import board_summary
from .communityWebsite.models import RealTime, Daily

class RealTimeType(DjangoObjectType):
    class Meta:
        model = RealTime

class DailyType(DjangoObjectType):
    class Meta:
        model = Daily

class Query(graphene.ObjectType):
    all_realtime = graphene.List(RealTimeType)
    all_daily = graphene.List(DailyType)

    def resolve_all_realtime(self, info, **kwargs):
        return RealTime.objects.all()

    def resolve_all_daily(self, info, **kwargs):
        return Daily.objects.all()

class SummaryBoardMutation(Mutation):
    class Arguments:
        board_id = graphene.String(required=True)

    response = graphene.String()

    def mutate(self, info, board_id):
        response = board_summary(board_id)
        return SummaryBoardMutation(response=response)

class Mutation(graphene.ObjectType):
    summary_board = SummaryBoardMutation.Field()

schema = graphene.Schema(query=Query, mutation=Mutation)

设置.py

# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
     "corsheaders",

    # Graph QL
    'graphene_django',
    'graphene_mongo',
    
    'webCrwaling',
    'kingwangjjang',
    'chatGPT'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'kingwangjjang.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        # 'DIRS': [],
        "DIRS": [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'kingwangjjang.wsgi.application'

# Database
# MongoDB settings
DB_URI = 'mongodb://'+ DB_HOST + '/' + DB_USER + ':' + DB_PASSWORD
DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': DB_NAME,
        'ENFORCE_SCHEMA': True,
        'CLIENT': {
            'host': DB_URI
        }  
    }
}

# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/

LANGUAGE_CODE = 'ko-kr'

TIME_ZONE = 'Asia/Seoul'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# GRAPHENE = {
#     "SCHEMA": "webCrwaling.schema.schema"
# }

插入数据

def get_real_time_best(self):
    req = requests.get('https://www.dcinside.com/', headers=self.g_headers[0])
    html_content = req.text
    soup = BeautifulSoup(html_content, 'html.parser')
    li_elements = soup.select('#dcbest_list_date li')
    already_exists_post = []

    for li in li_elements:
        p_element = li.select_one('.box.besttxt p')
        a_element = li.select_one('.main_log')
        time_element = li.select_one('.box.best_info .time')

        if p_element and a_element and time_element:
            p_text = p_element.get_text(strip=True)
            a_href = a_element['href']
            no_value = a_href.split('no=')[-1]
            time_text = time_element.get_text(strip=True)

            if(time_text.find('-') > 0): 
                break  # 오늘 것만 추가 (이전 글은 제외 (DB에서 확인))

            # 시간 13:40 -> 2024.01.29 13:40 로 수정
            now = datetime.now()
            hour, minute = map(int, time_text.split(':'))
            # 시간 설정 및 datetime 객체 생성
            target_datetime = datetime(now.year, now.month, now.day, hour, minute)

            try:
                existing_instance = RealTime.objects.filter(_id=no_value).first()
                if existing_instance:
                    already_exists_post.append(no_value)
                    continue
                else:
                    RealTime.objects.get_or_create(
                        _id=no_value,
                        defaults={
                            'site' : 'dcinside',
                            'title': p_text,
                            'url': a_href,
                            'create_time': target_datetime,
                            'GPTAnswer': DEFAILT_GPT_ANSWER
                        }
                    )
            except IntegrityError:
                continue

    print("already exists post", already_exists_post)

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 这会是一个问题吗?

需要修改哪部分才能解决数据完全消失的问题?

  1. 时区=假

但是

Python manage migrate
是正常的

python django pymongo
1个回答
0
投票

感谢那些回答我问题的人。

我给mongodb添加了一个认证过程,解决了db消失的问题。您在评论期间要求我检查日志,这对我很有帮助。谢谢你。

© www.soinside.com 2019 - 2024. All rights reserved.