社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

Django Tabular Inline没有外键问题

jxw • 2 年前 • 451 次点击  

在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'.
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/128307
 
451 次点击  
文章 [ 1 ]  |  最新文章 2 年前
lucutzu33
Reply   •   1 楼
lucutzu33    3 年前

你把一对夫妻的关系搞错了。形象应该是产品的外键。这样,一个产品可以有多个图像。

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)
    product = models.ForeignKey("Product", on_delete=models.CASCADE, related_name="images") 
    #if you want to get the product's images you can call product.images

并删除以下内容:

productImages = models.ForeignKey(Image, on_delete=models.CASCADE)

迁移,然后在你的管理员。py:

from django.contrib import admin
from .models import Product, Image
from .forms import ProductAdminForm

# Register your models here.
class ImageTabularInline(admin.TabularInline):
    model = Image

class ProductAdmin(admin.ModelAdmin):
    form = ProductAdminForm
    inlines = [ImageTabularInline,]
    
    class Meta:
        model = Product

admin.site.register(Image)
admin.site.register(Product, ProductAdmin)