Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy all fields of a django model instance

ok i think this is very basic, but since I am new to Django I don't know how to handle this.

I need to copy an instance of a django-model. As explained here, there is a problem with copying ManyToMany relations. But the attachment "django-model-copying.diff" has that function I guess. So I don't know - does my Django already have that function? I don't know how to call it. Help would be appreciated.

like image 353
Peter Avatar asked Sep 30 '10 19:09

Peter


People also ask

How do I copy a model in Django?

There is no built-in method for copying model instances, it is possible to create new instance with all fields values copied. If an instance is saved with instance's pk set to None , the instance is used to create a new record in the DB. That means every field other than the PK is copied.

What does of Django field class types do?

Django uses field class types to determine a few things: The column type, which tells the database what kind of data to store (e.g. INTEGER, VARCHAR, TEXT). The default HTML widget to use when rendering a form field (e.g. <input type=”text”>, <select>).


1 Answers

The docs include directions on how to do this - including how to handle many to many relationships.

like image 189
Aidan Ewen Avatar answered Sep 22 '22 01:09

Aidan Ewen