Sni*_*eep 4 django django-admin
我有两个模型产品和产品图像。但我必须提交与管理部分不同的那些。是否有任何选项可以将两个模型的所有字段组合在一个表单中 admin. 我在产品和图像之间有一对多的关系。
class Products(models.Model):
product_name = models.CharField(max_length = 50)
sku = models.CharField(max_length = 50)
details = models.TextField(null = True)
price = models.FloatField(default='0.00')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __unicode__(self):
return '%s %s' % (self.product_name, self.sku)
class ProductImages(models.Model):
product = models.ForeignKey('Products', on_delete=models.CASCADE)
images = models.ImageField(upload_to = 'images/product_images/', default = '')
created_date = models.DateTimeField(auto_now_add=True)`enter code here`
updated_date = models.DateTimeField(auto_now=True)
Run Code Online (Sandbox Code Playgroud)
您可以使用InlineModelAdmin,请参阅https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin。
admin.py:
from django.contrib import admin
class ProductImagesInline(admin.TabularInline):
model = ProductImages
class ProductsAdmin(admin.ModelAdmin):
inlines = [
ProductImagesInline,
]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4634 次 |
| 最近记录: |