如何从 django admin 的同一页面插入多个 tables/models 的数据?
How to insert data for multiple tables/models from the same page of django admin?
我是 Django 新手,正在处理我项目的管理部分。下面是我的 models.py.
代码
class Shops(models.Model):
name = models.CharField(max_length=200)
description = models.CharField(max_length=1500)
address = models.CharField(max_length=1000)
location = models.CharField(max_length=100)
contact_number = models.IntegerField()
other_details = models.CharField(max_length=100,null='true')
def __str__(self): # __unicode__ on Python 2
return (self.name)
class Shop_Type(models.Model):
category = models.CharField(max_length=500)
def __str__(self): # __unicode__ on Python 2
return (self.category)
class Shop_Category(models.Model):
shop_id = models.ForeignKey(Shops)
category_id = models.ForeignKey(Shop_Type)
现在我想在管理模块的单个页面中显示用于在 "Shops" 和 "Shop_Category" 表中插入数据的选项,因为它们都已连接。我提到了 this 问题,但未能实现我想要的。下面是我使用的 admin.py 的代码:
class ShopCatAdmin(admin.ModelAdmin):
model = Shop_category
class ShopsAdmin(admin.ModelAdmin):
inlines = [ShopCatAdmin]
admin.site.register(Shops, ShopsAdmin)
它抛出一些属性错误说 - “'ShopCatAdmin' 对象没有属性 'get_formset'”
如果有人能帮我解决这个问题就太好了。
提前致谢:)
您需要将 ShopCatAdmin
定义为继承自内联管理员 class,而不是基本管理员。
class ShopCatAdmin(admin.TabularInline):
model = Shop_Category
(注意,Python 风格不鼓励在 class 名称中使用下划线;您的模型应称为 ShopType 和 ShopCategory。)
我是 Django 新手,正在处理我项目的管理部分。下面是我的 models.py.
代码class Shops(models.Model):
name = models.CharField(max_length=200)
description = models.CharField(max_length=1500)
address = models.CharField(max_length=1000)
location = models.CharField(max_length=100)
contact_number = models.IntegerField()
other_details = models.CharField(max_length=100,null='true')
def __str__(self): # __unicode__ on Python 2
return (self.name)
class Shop_Type(models.Model):
category = models.CharField(max_length=500)
def __str__(self): # __unicode__ on Python 2
return (self.category)
class Shop_Category(models.Model):
shop_id = models.ForeignKey(Shops)
category_id = models.ForeignKey(Shop_Type)
现在我想在管理模块的单个页面中显示用于在 "Shops" 和 "Shop_Category" 表中插入数据的选项,因为它们都已连接。我提到了 this 问题,但未能实现我想要的。下面是我使用的 admin.py 的代码:
class ShopCatAdmin(admin.ModelAdmin):
model = Shop_category
class ShopsAdmin(admin.ModelAdmin):
inlines = [ShopCatAdmin]
admin.site.register(Shops, ShopsAdmin)
它抛出一些属性错误说 - “'ShopCatAdmin' 对象没有属性 'get_formset'”
如果有人能帮我解决这个问题就太好了。
提前致谢:)
您需要将 ShopCatAdmin
定义为继承自内联管理员 class,而不是基本管理员。
class ShopCatAdmin(admin.TabularInline):
model = Shop_Category
(注意,Python 风格不鼓励在 class 名称中使用下划线;您的模型应称为 ShopType 和 ShopCategory。)