如何解决Django:AttributeError:<Model>对象没有属性'_meta'
注意:- 我已经提到了SO链接和其他教程,但我无法克服此错误。
Django版本:django-3.1
上下文:- 我有一个产品,可以针对p_id(PK)附加多个图像。
待办事项:- 使用django-admin根据产品代码上传多张图片
Error:AttributeError:“ PhotoInline”对象没有属性“ _meta”
models.py
from django.db import models
from django.urls import reverse
from django.utils import timezone
# Create your models here.
class Product(models.Model):
"""
1. **product**
| Column-Name | Type |
| ------------ | ---------- |
| p_id | NUMERIC-AI |
| product_name | VARCHAR |
| COST | NUMERIC |
"""
p_id = models.AutoField(primary_key=True)
product_name = models.CharField(max_length=250)
cost = models.FloatField(default=0)
created_on = models.DateField(default=timezone.now)
def get_absolute_url(self):
return reverse('product:index')
def __str__(self):
return self.product_name
class Meta:
db_table = 'product'
verbose_name = 'Product List'
def img_upload_location(p_id,filename):
p_id = f"{p_id.p_id_id}".replace(" ","-")
file_name = filename.lower().replace(" ","-")
return f"pimages/{p_id}/{file_name}"
class Pimage(models.Model):
"""
| Column-Name | Type |
| ----------- | ------- |
| p_id(FK) | NUMERIC |
| img | VARCHAR |
"""
p_id = models.ForeignKey(Product,on_delete=models.CASCADE)
# img = models.ImageField(upload_to='images/')
img = models.ImageField(upload_to=img_upload_location)
def get_absolute_url(self):
return reverse('pimage:index')
# def __str__(self):
# # return Product.objects.get(p_id=f'{self.p_id}').product_name
# return f'{self.p_id}'
def __unicode__(self):
return self.p_id
class Meta:
db_table = 'pimage'
# Add verbose name
verbose_name = 'Product Image'
admin.py
from django.contrib import admin
from .models import Product,Pimage
from django.contrib.auth.models import Group
# Register your models here.
class ProdAdmin(admin.ModelAdmin):
list_display = ('product_name','cost','created_on')
class PhotoInline(admin.StackedInline):
model = Pimage
extra = 1
fk_name = Product.p_id
# fields = ["p_id","img"]
# fields = ('p_id','img')
class PimgAdmin(admin.ModelAdmin):
list_display = ('p_id','img')
inlines = [PhotoInline]
admin.site.register(Product,ProdAdmin)
admin.site.register(Pimage,PimgAdmin)
admin.site.unregister(Group)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。