question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding data in FK'd models through admin

See original GitHub issue

Note: 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:closed
  • Created 10 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
gasmancommented, Jun 19, 2018

@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.

1reaction
codydjangocommented, Feb 11, 2016

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set initial data for Django admin model add instance ...
I am using Django 1.3.1. My model is the following: class Foo(models.Model): title = models ...
Read more >
How to Save Data Input through Form Using a Django Model ...
Enjoyed my video? Leave a like!GitHub Link: https://github.com/maxg203Personal Website: http://maxgoodridge.com.
Read more >
Django Admin CSV file upload - YouTube
In this django tutorial we build an admin feature to allow admin users to upload csv files so that database tables can be...
Read more >
The Django admin site
One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick,...
Read more >
Perform CRUD Operations in the Django Admin
Create - where you insert new data to a database. · Read - where you retrieve data from the database (often so that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found