Adding data in FK'd models through admin
See original GitHub issueNote: Detailed explanation below, this is somewhat similar to #138, explained in ‘footnote’ section.
I’m having an issue restructuring my models and have a feeling that I’m bending wagtail to do something its not meant to, at the same time this is somewhat standard while working in Django .
I want:
- to add categories of a product via the admin panel in a table ‘Category’/
- ProductPage(Page) to FK’s to a Category
- In the Product Page admin, I would like to pick (and preferably add) a category for the product.
This is straightforward with the Django stock admin, but I can’t seem to figure out how to get it to work here.
A quick and dirty example of e-commerce models (with problems mentioned):
An implementation I would have thought would work:
class Category(models.Model):
'''
Each Product will be associated with one Category.
'''
name = models.CharField(max_length = 100)
description = models.TextField()
def __unicode__(self):
return u"%s" % (self.name)
class ProductPage(Page):
product_name = RichTextField()
product_description = RichTextField()
category = models.ForeignKey('CategoryPage', to_field='category_name', related_name='+')
price = models.IntegerField(default=0)
qty_in_stock = models.IntegerField(default=0)
ProductPage.content_panels = [
FieldPanel('title', classname="Page Title"),
FieldPanel('product_name', classname="Product Name"),
FieldPanel('product_description', classname="Product Description"),
FieldPanel('category', classname="Category Name"),
FieldPanel('price'),
FieldPanel('qty_in_stock'),
]
While I can pick a category, there is no way to add one (through the admin).
What I’m finally doing (still some problems mentioned at the end).
class CategoryPage(Page):
category_name = RichTextField("Category Name", unique=True)
category_description = RichTextField("Category Description")
CategoryPage.content_panels = [
FieldPanel('category_name', classname="Category Name"),
FieldPanel('category_description', classname="Category Description"),
]
class ProductPage(CategoryPage): # since I'm inheriting CategoryPage, I'm inheriting the content_panels as well
product_name = RichTextField()
product_description = RichTextField()
category = models.ForeignKey('CategoryPage', to_field='category_name', related_name='+') # if I remove this I lose the dropdown
price = models.IntegerField(default=0)
qty_in_stock = models.IntegerField(default=0)
ProductPage.content_panels = [
FieldPanel('title', classname="Page Title"),
FieldPanel('product_name', classname="Product Name"),
FieldPanel('product_description', classname="Product Description"),
FieldPanel('category', classname="Category Name"), # if I remove this I lose the dropdown
FieldPanel('price'),
FieldPanel('qty_in_stock'),
]
I have tried a couple of things of other (maybe silly) things, one of which is adding:
python CategoryPage.content_panels = []
above python ProductPage.content_panels =[ foo bar ]
in the ProductPage model.
Can anyone shed any light on this?
As for #138: The solution suggested by @gasman was to add categories of a menu as subpages of MenuPage. Translating this to my scenario, this means that each ProductPage will be linked to one (or more) categories, however, I could not figure out how to pick categories that already existed in the admin. A guess - would this entail adding an admin action? Or am I off the mark completely here?
Issue Analytics
- State:
- Created 10 years ago
- Comments:10 (2 by maintainers)
Top GitHub Comments
@tutuca The original issue here was not about adding new items from a popup, and was already closed before people started talking about that…
It looks like this feature request is covered by #396 - I suggest that any further discussion towards implementing this feature should happen there.
I’d just like to clarify that this isn’t an issue with wagtailmodeladmin, AFAIK, which seems to use the same foreignkey field and widget as wagtail. I’m just curious if there’s a reason why the functionality isn’t present in wagtail. The “expected” functionality (coming from django stock admin or mezzanine) is a little + button next to the select element which then pops open a modal in which you can create the object you’d like to add via a form. Once submitting the form the modal vanishes and the newly created id is selected in the foreignkey select element. I hope this makes sense 😃
I’ll take a deeper look this weekend, I might be able to get it working on a fork.