在Django,我正在设置两个模型,一个用于产品,一个用于图像。一个产品可以有多个图像与之关联,因此我使用的是一对多模型。
在我的模型中。我有
from django.db import models
from django.db.models.fields import CharField
# Create your models here.
class Image(models.Model):
image = models.ImageField()
imageMetaKeyWords = models.CharField("Meta keywords for SEO", max_length = 255,
help_text = "Comma delimited words for SEO")
imageMetaDescription = models.CharField("Meta description", max_length = 255,
help_text = "Content for image meta tag description")
defaultImage = models.BooleanField(default= False)
class Product(models.Model):
productName = models.CharField(max_length=200)
productDescription = models.TextField(blank=True)
productPrice = models.DecimalField(max_digits=9, decimal_places=0, default=0)
productAvailable = models.BooleanField(default = True)
productCreated_at = models.DateTimeField(auto_now_add = True)
productUpdated_at = models.DateTimeField(auto_now = True)
productSlug = models.SlugField(max_length = 255, unique = True,
help_text = "Unique text for url created from product name")
productMetaKeywords = models.CharField("Meta keywords for SEO", max_length = 255,
help_text ="Comma delimited words for SEO")
productMetaDescription = models.CharField("Meta description", max_length = 255,
help_text="Content for meta tag description")
productImages = models.ForeignKey(Image, on_delete=models.CASCADE)
class Meta:
db_table = 'products'
ordering = ['-productName']
def __str__(self):
return self.productName
在管理方面。py
from django.contrib import admin
from .models import Product, Image
from .forms import ProductAdminForm
# Register your models here.
class ImageTabularInline(admin.TabularInline):
model = Product
class ProductAdmin(admin.ModelAdmin):
form = ProductAdminForm
inlines = [ImageTabularInline,]
class Meta:
model = Product
admin.site.register(Product, ProductAdmin)
admin.site.register(Image)
在形式上。我有
from django import forms
from .models import Product
class ProductAdminForm(forms.ModelForm):
class Meta:
model = Product
fields = "__all__"
当我跑的时候
python manage.py makemigrations
我收到这个错误,我不知道它是什么意思
德扬戈。果心经营基础SystemCheckError:SystemCheckError:System check发现了一些问题:
ERRORS:
<class 'products.admin.ImageTabularInline'>: (admin.E202) 'products.Product' has no ForeignKey to 'products.Product'.