I can't find it anywhere, so your help will be nice for me :) Here is that field:
categories = models.ManyToManyField(fragmentCategory)
FragmentCategory:
class fragmentCategory(models.Model): CATEGORY_CHOICES = ( ('val1', 'value1'), ('val2', 'value2'), ('val3', 'value3'), ) name = models.CharField(max_length=20, choices=CATEGORY_CHOICES)
Here is the form to send:
<input type="checkbox" name="val1" /> <input type="checkbox" name="val2" /> <input type="checkbox" name="val3" />
I tried something like this:
categories = fragmentCategory.objects.get(id=1),
Or:
categories = [1,2]
When you add a many to many field to a model a separate table is created in the database that stores the links between two models. If you don't need to store any extra information in this third table then you don't have to define a model for it.
A good example of a many-to-many relationship is the relationship between a sandwich and a sauce. I like a chicken teriyaki sandwich but only if it contains barbeque sauce as well as mayonnaise sauce. So the same sandwich can have multiple sauces.
To define a many-to-many relationship, use ManyToManyField . What follows are examples of operations that can be performed using the Python API facilities. You can't associate it with a Publication until it's been saved: >>> a1.
There's a whole page of the Django documentation devoted to this, well indexed from the contents page.
As that page states, you need to do:
my_obj.categories.add(fragmentCategory.objects.get(id=1))
or
my_obj.categories.create(name='val1')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With