You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用django-modeltranslation时Django Admin数据库报错求助

Fixing Django ModelTranslation Admin Database Error

Hey there, let's work through that frustrating database error you're seeing when accessing your translated model in the Django Admin. That message usually means the translated fields aren't properly synced with your database, even after running migrations. Here's a step-by-step breakdown to troubleshoot and fix it:

1. Double-Check ModelTranslation Installation Order

First, make sure modeltranslation is at the top of your INSTALLED_APPS in settings.py. It needs to load before your own apps to properly patch model fields for translation:

INSTALLED_APPS = [
    'modeltranslation',  # Must come before your apps
    'django.contrib.admin',
    'django.contrib.auth',
    # ... other default apps
    'your_custom_app',  # Your app comes later
]

If it's placed after your app, Django might load the original model before modeltranslation can add the translated fields, leading to mismatches between the model and database.

2. Verify Translation Options Configuration

Ensure your model's translation setup is correct. You need a TranslationOptions class registered with your model, and the fields listed must exactly match the fields in your original model:

# In your app's translation.py or models.py
from modeltranslation.translator import register, TranslationOptions
from .models import YourModel

@register(YourModel)
class YourModelTranslationOptions(TranslationOptions):
    fields = ('title', 'description',)  # Match field names from YourModel exactly

Typos or missing fields here can cause the database to not have the expected translated columns (like title_en or description_fr).

3. Regenerate Migrations (Clean Slate)

Sometimes old or incomplete migrations can leave the database in an inconsistent state. Try resetting your migrations (safe for development environments):

  • Delete all files in your app's migrations folder except __init__.py
  • If using SQLite, you can delete the database file directly; for other databases, run a query to delete records from django_migrations where app matches your app name
  • Then run these commands to rebuild migrations and apply them:
python manage.py makemigrations
python manage.py migrate

This ensures modeltranslation's auto-generated translated fields are properly created in the database.

4. Use TranslationAdmin for Your Admin Class

If you've customized your Admin class, you must inherit from TranslationAdmin instead of the default ModelAdmin. This tells the Admin to recognize and handle the translated fields:

# In your app's admin.py
from modeltranslation.admin import TranslationAdmin
from django.contrib import admin
from .models import YourModel

@admin.register(YourModel)
class YourModelAdmin(TranslationAdmin):
    list_display = ('title',)  # You can include translated fields here too, e.g., 'title_en'

Using the standard ModelAdmin will cause the Admin to look for the original untranslated fields structure, which no longer matches the database with translated columns.

5. Check Database Permissions & Cache

  • Confirm the database user in your settings.py has full read/write access to the database. Even if you can see the model, missing permissions on the translated columns could trigger this error.
  • Clear Django's cache to ensure old model metadata isn't being cached:
python manage.py clearcache

Then restart your Django development server to load the updated model configuration.

If you work through these steps, the Admin should stop throwing that database error and let you manage your translated models properly.

内容的提问来源于stack exchange,提问作者Elchinas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:31:16