ManyToManyField that can be set even if it relies on a intermediary model.
Version of ManyToManyField that can be set even if it relies on a intermediary model.
- field
- related
- manytomany
Version of ManyToManyField that can be set even if it relies on a intermediary model.
Put this in your templates/admin/change_form.html template and you will be able to double click a manytomany in a multi select to be able to edit it.
Limit ManyToMany fields in forms. Hide the field, if only one item can be selected. e.g. For limit sites choices only to accessible sites. Also available via django-tools: http://code.google.com/p/django-tools/
unique_together doesn't work with ManyToMany, yet. See: http://code.djangoproject.com/ticket/702 Here a simple test in save() that would raise a IntegrityError.
**Sumary** M2M relation without creating table. Normally you should specify m2m only in *one* model, thus widgets for many-to-many relations will be displayed inline on whichever model contains the actual reference to the ManyToManyField. But if you want to be able to have widgets displayed on both form you need some tricks, for example using intermediary-models can help, but you will not get multiselect widget (and in case of inlining extra = multi). Also you can write your own form which takes care about adding widget (just 1 line) and setting default values and saving it (more than few lines of code). If you try ManyToManyField with same db_table specified, the only problem will be in syncdb (it will try to create two identical tables) and the only thing our class does is preventing creation of table for M2M, so in one model you should use ManyToManyField and in another ManyToManyField_NoSyncdb with the same db_table argument. **Example** So to have M2M widgets in both forms you can write: class User(models.Model): #... groups = ManyToManyField('Group', related_name='groups', db_table=u'USERS_TO_GROUPS') class Group(models.Model): #... users = ManyToManyField_NoSyncdb(User, related_name='users', db_table=u'USERS_TO_GROUPS')
In editing a ManyToMany field in a form, it is necessary to clear the entries in the bridge table before adding new entries. So first save the new entries, remove the old entries in the multiple choice field and then add the new entries. It is also necessary to add the commit_on_success decorator to make sure that the whole process is in one transaction.
6 snippets posted so far.