[Answered ]-Grouping many to many choices by type in form

1👍

I would create dynamic form, creating one choice field for every AttributeType.

Then, you can easily replace widget with radio buttons.

This article could be helpful:

http://www.b-list.org/weblog/2008/nov/09/dynamic-forms/

1👍

Can one sandbox have many items, or one item have many sandboxes? Can an item belong to multiple sandboxes at once?

I think you want one sandbox to contain many items:

class Sandbox(models.Model):
    name = models.CharField()

class Item(models.Model):
    name = models.CharField()
    sandbox= models.ForeignKey(Sandbox)
    attributes = models.ManyToManyField('Attribute')

The same analysis holds true here:

Does one attribute have many attribute types, or one attribute type have many attributes?

Here you have the relationship right, I just switched the order of the models

class AttributeType(models.Model):
    name = models.CharField()

class Attribute(models.Model):
    name = models.CharField()
    type = models.ForeignKey(AttributeType)

So each item has an attribute and they always are endowed with these attributes, color and shape.

While you could have a table that had data that looked like:

pk type
1 green
2 circular
etc

I personally wouldn’t do that, because I think data that is logically the same should be grouped together. Shapes have different attributes than colors. For example and to illustrate my point, what if you wanted the RGB of a color? Then you would have extra columns for shapes when they aren’t needed and this is confusing. Same is true in the converse, colors don’t have dimension.

Instead, I might do something like:

class Color(models.Mode):
     #info about colors

class Shape(models.Mode):
     #info about shapes

class Item(models.Model):
    name = models.CharField()
    sandbox= models.ForeignKey(Sandbox)
    color= models.ForeignKey(Color)
    shape= models.ForeignKey(Shape)

This also guarantees you only have one choice per, because ForeignKey in django.Forms defaults you to a ChioceField (iirc).

As for overriding that and making it a radiobutton, just review the docs here:

https://docs.djangoproject.com/en/dev/ref/forms/widgets/

Leave a comment