Django Admin - SubModels on Parent Form -
i have django model called product
. want add 1:n relation in product
choose images of product, , on product
admin form want user add images.
i tried manytomany
relation it's not want because don't want user visit 2 forms product
create products, images
create images , products
again select images of product
have.
anyone has ideia? sorry if not clear, , let me know if need more informations.
models.py:
class image(models.model): """ generic image model """ name = models.charfield(max_length=100, blank=false, verbose_name=_("name")) image_file = models.imagefield(blank=false, upload_to=image_upload, verbose_name=_("file")) def __unicode__(self): return u'{}'.format(self.name) def post_url(self): return os.path.join(settings.media_root, 'images', self.__class__.__name__) class meta: verbose_name=_("image") verbose_name_plural=_("images") class product(models.model): name = models.charfield(max_length=100, blank=false, unique=true, verbose_name=_("name")) creation_date = models.datefield(auto_now_add=true, blank=false, db_index=true, verbose_name=_("date")) cover_image = models.imagefield(blank=false, upload_to=product_cover_upload,verbose_name=_("image")) category = models.foreignkey(category, blank=true,verbose_name=_("category")) value = models.decimalfield(blank=false, max_digits=15, decimal_places=2,verbose_name=_("value")) featured = models.booleanfield(blank=false, default=false,verbose_name=_("featured")) def __unicode__(self): return u'{}'.format(self.name) class meta: ordering = ['name'] verbose_name=_("product") verbose_name_plural=_("products") class productimage(image): """ product images """ product = models.foreignkey(product,verbose_name=_("product")) def post_url(self): return os.path.join(settings.media_root, 'images', 'products', str(self.product)) class meta: proxy = false verbose_name=_("product image") verbose_name_plural=_("products images")
admin.py:
class productadmin(admin.modeladmin): list_display = ('name', 'creation_date', 'category', 'value', 'featured',) read_only_fields = ('creation_date',) admin.site.register(product, productadmin)
admin.py:
class productimageinline(admin.stackedinline): model = productimage class galleryadmin(admin.modeladmin): inlines = [productimageinline]
Comments
Post a Comment