I have:
class MyUser(Model): today_ref_viewed_ips = ManyToManyField( UniqAddress, related_name='today_viewed_users', verbose_name="Adresses visited referal link today") ...
On some croned daily request I do:
for u in MyUser.objects.all(): u.today_ref_viewed_ips.clear()
Can it be done on DB server with update?
MyUser.objects.all().update(...)
Ok, I can't update, thanks. But only thing I need is to TRUNCATE m2m internal table, is it possible to perform from django? How to know it's name whithout mysql's console "SHOW TABLES"?
score:4
Query-1:
No, you cannot use .update()
method to update a ManyToManyField
.
Django's .update()
method does not support ManyToManyField.
As per the docs from the section on updating multiple objects at once:
You can only set non-relation fields and
ForeignKey
fields using this method. To update a non-relation field, provide the new value as a constant. To updateForeignKey
fields, set the new value to be the new model instance you want to point to.
Query-2:
If you want to delete all the objects of m2m table, you can use .delete()
queryset method.
MyModel.objects.all().delete() # deletes all the objects
Another method is to execute the raw SQL directly. This method is faster than the previous one.
from django.db import connection
cursor = connection.cursor()
cursor.execute("TRUNCATE TABLE table_name")
Query-3:
To get the table name of a model, you can use db_table
model Meta
option.
my_model_object._meta.db_table # gives the db table name
score:8
If you want to update the m2m fields only and do not want to delete the m2m objects you can use the following:
#if you have **list of pk** for new m2m objects
today_ref_pk = [1,2,3]
u = MyUser.objects.get(pk=1)
u.today_ref_viewed_ips.clear()
u.today_ref_viewed_ips.add(*today_ref_pk)
for django >=1.11 documentation:
# if you have the **list of objects** for new m2m and you dont have the
# issue of race condition, you can do the following:
today_ref_objs = [obj1, obj2, obj3]
u = MyUser.objects.get(pk=1)
u.today_ref_viewed_ips.set(today_ref_objs, clear=True)
Credit To: stackoverflow.com
Related Query
- how to check to ManyToManyField table to update m2m depend on a parent field django
- How to update model field using django signals when updating object in m2m field?
- How to make a field editable on create and read-only on update in Django REST framework
- Django Rest Framework: how to make field required / read-only only for update actions such as PUT and PATCH?
- how to override django save method to update some field dynamically?
- How to .update m2m field in django
- How to update a foreign key field in Django models.py?
- How can I update a Django models.DateTimeField when another, specific field in the same model is updated?
- How to bulk update the many-to-many field of a Django queryset
- How to set a field to null in a django update statement
- Django - How to duplicate a model instance that uses a m2m field with a through model
- Django - How to update a field inside a model save() method?
- how django 'auto_now' ignore the update of specified field
- How to automatically update the date field through Django Class model
- How to update self.request.user field in django rest framework?
- Django Beginner. How do I update all objects and set a certain field to a value that is a function of another field?
- How to annotate on a Django model's M2M field and get a list of distinct instances?
- How to update object with an image field - Django Rest Framework
- How to override M2M field names and models in Django with an existing database?
- How to update time field in django
- How to automatically update field in Django Admin (back end)
- How to update the field of django model from the existing field on the Fly
- How to save m2m field on Django with commit=False?
- How to Update a field in Django
- How to update data in Django when getting unique field error
- How can I take input from a text field in a Django page then update a SQL table with the response?
- How to update the serializer field for m2m relationship
- How can I add an object to a M2M field on my users profile from django rest frameworks class based create view?
- How to update File field django
- How to update exisiting date field in django using existing days field?
More Query from same tag
- Django - Show BooleanField in a formset as one group of radio buttons
- How do I query a complex JSONB field in Django 1.9
- Logic behind Form(request.POST or None)
- Http response at 400 or 500 level
- Django : loaddata to update data
- Django request.user returning None
- what should i do to be able to post multiple contents in my code in django rest framework?
- How to use serializer to create a new object with a foreignkey
- Tastypie: Getting error during create_response()
- Send an e-mail notification when a Django CharField is modified via the admin site
- Google TTS in Django: Create Audio File in Javascript from base64 String
- NoReverseMatch at / Reverse for 'register_request' not found. 'register_request' is not a valid view function or pattern name
- Select between two dates with Django
- django app is working while not included in settings.py
- Adding app to Django