Home > Enterprise >  Django Tabular Inline has no foreign key issue
Django Tabular Inline has no foreign key issue

Time:10-24

In Django I am setting up two models one for product and one for image. One product can have multiple images associated with it and therefore I am using a one to many model.

In my models.py I have

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

In admin.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)

In forms.py I have

from django import forms
from .models import Product

class ProductAdminForm(forms.ModelForm):
    class Meta:
        model = Product
        fields = "__all__"

When I run python manage.py makemigrations I receive this error that I don't know what it means

django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
<class 'products.admin.ImageTabularInline'>: (admin.E202) 'products.Product' has no ForeignKey to 'products.Product'.

CodePudding user response:

You got the OneToMany relationship wrong. Image should have a ForeignKey to Product. That way a Product can have multiple Images.

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

and remove this:

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

Do the migration, then in your admin.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)
  • Related