10👍
✅
Of course it’s possible! You just have to create an explicit intermediate table and
then use this model’s bulk_create
method.
43👍
If you want to add queryset to bulk add or remove method of many to many relation models :
qs = Article.objects.all()
publications = Publications.objects.get(id=1)
publications.article_set.add(*qs)
publications.save()
publications.article_set.remove(*qs)
publications.save()
- [Django]-How about having a SingletonModel in Django?
- [Django]-Phpmyadmin logs out after 1440 secs
- [Django]-Update django database to reflect changes in existing models
1👍
An example that adds a group and assign its permissions
group = Group.objects.create(name='user_manager')
codenames = ['delete_user', 'add_user', 'view_user', 'change_user']
permissions = Permission.objects.filter(codename__in=codenames)
group.permissions.add(*permissions)
- [Django]-Django admin: make field editable in add but not edit
- [Django]-How do I subtract two dates in Django/Python?
- [Django]-Django Forms: if not valid, show form with error message
0👍
You can just use RelatedManager.set
for this:
a2 = Article(headline='NASA uses Python')
a2.save()
a2.publications.set([p1, p2, p3])
The documentation also tells us that there’s no need to call .save()
at the end.
- [Django]-Django rest framework override page_size in ViewSet
- [Django]-Django app works fine, but getting a TEMPLATE_* warning message
- [Django]-How to use curl with Django, csrf tokens and POST requests
Source:stackexchange.com