Answer the question
In order to leave comments, you need to log in
How to add many objects to database in Django TabularInLine?
I currently need to add many objects to a pass-through table using python manage.py shell.
I will describe the models here in a nutshell, I will not litter with extra fields.
So I have a Product model:
class Product(models.Model):
name = models.CharField(max_length=200, db_index=True, unique=True)
slug = models.SlugField(max_length=200, db_index=True, unique=True)
class Shop(models.Model):
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True)
class ShopQuantity(models.Model):
shop = models.ForeignKey(Shop, blank=True, on_delete=models.CASCADE, null=True, related_name='product_list')
product = models.ForeignKey(Product, blank=True, on_delete=models.CASCADE, null=True, related_name='shop_list')
price = models.DecimalField(max_digits=10, decimal_places=2, default='0')
quantity = models.IntegerField(blank=True, null=True, default='0')
class ShopQuantityInline(admin.TabularInline):
model = ShopQuantity
extra = 0
@admin.register(Shop)
class ShopAdmin(admin.ModelAdmin):
fields = ['name', 'slug']
list_display = ['name']
inlines = [ShopQuantityInline]
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
fields = ['name', 'slug']
list_display = ['name']
inlines = [ShopQuantityInline]
for i in range(5):
p = ShopQuantity.objects.create()
p.save()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question