迁移后,Django新属性未显示在管理控制台中

时间:2018-07-13 03:27:20

标签: python mysql django

仅需了解一下内容:最近,我从开发人员手中接过了我的实时网站,虽然我有一些基本的编码知识,但是我并不熟练。

我尝试向Product模型添加'purchase_type'属性,但是即使进行了迁移,新属性也不会显示在管理控制台中。但是,当我查询数据库时,该属性在那里并返回正确的值。

1。在模型中添加了“ purchase_type”。py:

class Product(models.Model):

index_number = models.IntegerField(default=None, blank=True, null=True)
name = models.CharField(max_length=500, blank=True, null=True)
coming_soon = models.BooleanField(default=False)
retail_price = models.DecimalField(max_digits=100, decimal_places=2, default=None, null=True)

calories = models.IntegerField(default=None, blank=True, null=True)
protein_class = models.CharField(max_length=500, blank=True, null=True)
carb_class = models.CharField(max_length=500, blank=True, null=True)
fat_class = models.CharField(max_length=500, blank=True, null=True)
purchase_type = models.CharField(max_length=500, default="Subscription")

2。在admin.py中的字段集中添加了“ purchase_type”:

class ProductAdmin(admin.ModelAdmin):
list_display = ("name", "index_number", "coming_soon", "retail_price", "calories", "protein_class", "carb_class", "fat_class")
search_fields = ("name",)
list_filter = ("protein_class", "carb_class", "fat_class")
ordering = ("index_number",)
fieldsets = (
    ("Main Info", {
        "fields": ("index_number", "name", "coming_soon", "retail_price", "calories", "protein_class", "carb_class", "fat_class", "purchase_type"),
    }),

3。按运行顺序运行以下命令:

./manage.py migrate
./manage.py makemigrations webapp
./manage.py migrate

4。当我查询数据库中的purchase_type时,返回正确的值:

./manage.py shell
from webapp.models import Product
>>> print(Product.objects.values('purchase_type'))
[{'purchase_type': u'Subscription'}, {'purchase_type': u'Subscription'}, {'purchase_type': u'Subscription'}, {'purchase_type': u'Subscription'}]

5。但是,管理控制台没有属性或值:

purchase_type not showing admin dashboard

6。以下是settings.py中的数据库设置:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'lpdb',                                     # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': '[redacted]',
        'PASSWORD': '[redacted]',
        'HOST': 'localhost',                                # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',                                         # Set to empty string for default.
    }
}

非常感谢您的帮助,祝您生活愉快!

编辑:这是完整的admin.py文件

from webapp.models import Subscription, Product, MailingListSignup
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

from import_export import resources
from import_export.admin import ExportMixin

class ProductAdmin(admin.ModelAdmin):
    list_display = ("name", "index_number", "coming_soon", "retail_price", "calories", "protein_class", "carb_class", "fat_class")
    search_fields = ("name",)
    list_filter = ("protein_class", "carb_class", "fat_class")
    ordering = ("index_number",)
    fieldsets = (
        ("Main Info", {
            "fields": ("index_number", "name", "coming_soon", "retail_price", "calories", "protein_class", "carb_class", "fat_class", "purchase_type"),
        }),
        ("Macronutrients", {
            "fields": ("protein_amount", "protein_units", "net_carbohydrates_amount", "net_carbohydrates_units", "sugars_amount", "sugars_units", "dietary_fiber_amount", "dietary_fiber_units", "fats_amount", "fats_units", "saturated_fats_amount", "saturated_fats_units", "monounsaturated_fats_amount", "monounsaturated_fats_units", "polyunsaturated_fats_amount", "polyunsaturated_fats_units"),
        }),
        ("MVitamins & Minerals", {
            "fields": ("sodium_amount", "sodium_units", "sodium_percentage_dv", "potassium_amount", "potassium_units", "potassium_percentage_dv", "calcium_amount", "calcium_units", "calcium_percentage_dv", "iron_amount", "iron_units", "iron_percentage_dv", "iodine_amount", "iodine_units", "iodine_percentage_dv", "zinc_amount", "zinc_units", "zinc_percentage_dv", "selenium_amount", "selenium_units", "selenium_percentage_dv", "magnesium_amount", "magnesium_units", "magnesium_percentage_dv", "vitamin_a_amount", "vitamin_a_units", "vitamin_a_percentage_dv", "vitamin_c_amount", "vitamin_c_units", "vitamin_c_percentage_dv", "vitamin_d_amount", "vitamin_d_units", "vitamin_d_percentage_dv", "vitamin_e_amount", "vitamin_e_units", "vitamin_e_percentage_dv", "vitamin_k_amount", "vitamin_k_units", "vitamin_k_percentage_dv", "b2_riboflavin_amount", "b2_riboflavin_units", "b2_riboflavin_percentage_dv", "niacin_amount", "niacin_units", "niacin_percentage_dv", "b1_thiamine_amount", "b1_thiamine_units", "b1_thiamine_percentage_dv", "vitamin_b6_amount", "vitamin_b6_units", "vitamin_b6_percentage_dv", "vitamin_b12_amount", "vitamin_b12_units", "vitamin_b12_percentage_dv", "folates_amount", "folates_units", "folates_percentage_dv", "pantothenic_acid_amount", "pantothenic_acid_units", "pantothenic_acid_percentage_dv", "biotin_amount", "biotin_units", "biotin_percentage_dv"),
        }),
    )
    save_as = True

class SubscriptionAdmin(admin.ModelAdmin):
    list_display = ("user", "user_phone_number", "stripe_customer_id", "stripe_subscription_id", "stripe_successful", "paid_amount", "shipped", "datetime_ordered", "stripe_email", "shipping_name", "shipping_address_country", "shipping_address_zip", "shipping_address_state", "shipping_address_line1", "shipping_address_city", "shipping_address_country_code", "monthly_qty", "calories", "protein", "carb", "fats")
    search_fields = ("user", "datetime_ordered")
    list_filter = ("user", "datetime_ordered")
    ordering = ("datetime_ordered",)

class UserAdmin(UserAdmin):
    list_display = ('username', 'email', 'is_staff', 'date_joined', 'last_login')
    ordering = ('-date_joined',)
    list_filter = ('date_joined', 'last_login')

class MailingListSignupAdmin(ExportMixin, admin.ModelAdmin):
    list_display = ('first_name', 'last_name', 'email')
    search_fields = ('first_name', 'last_name', 'email')

admin.site.register(Subscription, SubscriptionAdmin)
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
admin.site.register(Product, ProductAdmin)
admin.site.register(MailingListSignup, MailingListSignupAdmin)

1 个答案:

答案 0 :(得分:0)

您尝试一些尝试:

首先,您必须在“迁移”文件夹中查找以检查Purchase_type是否已包含在迁移文件中。

如果,请运行此命令,这将撤消所有迁移:

python manage.py migrate webapp zero

要重新执行迁移,请运行下面的2个命令进行迁移,然后进行迁移并检查是否可以解决您的问题。


如果,则从** App **删除所有迁移文件,然后重新运行:

删除所有带有数字的文件 App>迁移> 0000_files.py

python manage.py makemigrations webapp
python manage.py migrate

如果这不能解决您的问题,请删除您的dbsqlite3文件,然后再次进行迁移。这总是有帮助的,但是您将运行所有迁移,超级用户创建等。


更新

运行此命令:

python manage.py syncdb

此命令将在您的模型文件中实现对PostgresSql数据库的更改。