Bulk add foreign keys in admin interface

87 views
Skip to first unread message

Kristofer Pettijohn

unread,
Oct 25, 2013, 10:17:01 PM10/25/13
to django...@googlegroups.com
I am using the following type of model setup:

class Product(models.Model):
    name = models.CharField(max_length=40)
    description = models.TextField(blank=True)
    active = models.BooleanField(default=True)
    priority = models.IntegerField(default=100)
    size_chart = models.ForeignKey(SizeChart)
    designer = models.ForeignKey(Designer)
    collections = models.ManyToManyField(Collection)
    categories = models.ManyToManyField(Category)
    price = models.DecimalField(max_digits=6, decimal_places=2)
    def __unicode__(self):
        return self.name

class Variation(models.Model):
    color = models.ForeignKey(Color)
    size = models.ForeignKey(Size)
    product = models.ForeignKey(Product)
    price_override = models.DecimalField(max_digits=6, decimal_places=2, blank=True)

I have Variations setup as TabularInline in Django admin.  However, there are > 200 different colors, and about 20 sizes.  Adding 10 different colors with 15 sizes is a lot of clicking for someone.  What I would like to do is add some type of dropdown where multiple colors, along with multiple Sizes, can be selected, and then all of the Variations will be created for the cross product of those two selections.

What I envision is having an area somewhere in the Django Admin for Product that is titled "Bulk Add" with drop down boxes for both Color and Size where multiple values for each can be selected.  When the user is happy with their selection, they will click a button and all of the Variations will be created and the user will be back to the product page with all of the options now visible in the TabularInline section.

Is this type of action possible?

Timothy W. Cook

unread,
Oct 26, 2013, 6:17:47 AM10/26/13
to django...@googlegroups.com
Yes it is possible and quite easy to do.  Take a look at https://docs.djangoproject.com/en/1.6/ref/contrib/admin/actions/

Cheers,
Tim


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1899431826.178951.1382753821780.JavaMail.zimbra%40cybernetik.net.
For more options, visit https://groups.google.com/groups/opt_out.



--
MLHIM VIP Signup: http://goo.gl/22B0U
============================================
Timothy Cook, MSc           +55 21 94711995
MLHIM http://www.mlhim.org
Like Us on FB: https://www.facebook.com/mlhim2
Circle us on G+: http://goo.gl/44EV5
Google Scholar: http://goo.gl/MMZ1o
LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook

Kristofer Pettijohn

unread,
Oct 27, 2013, 2:47:03 AM10/27/13
to django...@googlegroups.com
I found that previously, but it doesn't seem to be quite what I need.

Let me try to rephrase.  I want to add two multi-selection boxes within the admin view page for the Product model.  One of the multi-selections will be for Colors, and one will be for Sizes.  I then want a button that the user can click after the select options from both multi-selection boxes.  The button will redirect to a method that will loop through the product of the two selections and add a Variation for each combination to the single Product (not multiple products).

For example, if the user wants to add Green, Yellow, Blue, Pink, Orange, and White colors with sizes 2, 4, 6, 8, 10, and 12, they can do it all with one click, versus manually adding 32 (Green-2, Green-4, Green-6, Blue-2, Blue-4, Blue-6, etc.) items into the TabularInline section
Reply all
Reply to author
Forward
0 new messages