开发者

Copy all fields of a django model instance

ok i think this is very basic, but since I am new to Django开发者_Go百科 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.


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


You can just do the following:

m = MyModel.objects.get(pk=1)
m.id = None
m.save()

That way new instance will be created with new id of course in case of any unique properties it will trigger errors during validation.

NOTE: As for the function you've mentioned - it is not yet added to the trunk, the status is design decision needed, but if you know what you're doing you can manually apply the diff to your django instance - it's called patching btw. Here are some details about how to do it: http://ariejan.net/2007/07/03/how-to-create-and-apply-a-patch-with-subversion/.


I'll try to answer your actual problem, because what you are asking for in the question is a problem with your proposed solution, which as many have pointed out is not really ideal.

In the comments you mention this:

i'll try to explain.. So we have 2 models: User and Book. A User has a book called "Titanic" with some content. Now, another user wants a relation to that book too. But, the second user wants exactly the same book, but it should be called "Ship is going under".. I would copy the book, and rename it. - I know, i could also put the content of the book in another model - but my model is a little bit more complex.

Looks like you have three things to track:

  1. Users
  2. Their Books
  3. Some custom notes that users have about their "owned" book.

For each Book, store its common information, for example:

class Author(models.Model):
    name = models.CharField(max_length=50)
    email = models.EmailField()

    def __unicode__(self):
        return unicode(self.name)

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ManyToMany(Author)
    isbn = models.CharField(max_length=13)
    # and so on

    def __unicode__(self):
        return unicode(self.title)

Now, you need to store Book -> User relation, such as one user can have many books, two users may own the same book, with their own meta information attached.

class BookClub(models.Model):
     username = models.ForeignKey(User)
     book = models.ForeignKey(Book)
     comments = models.TextField()

If you structure your models like that, you'll be able to do queries like:

  • "How many books does each member have?"
  • "What is the most popular book?"
  • "What is the least popular book?"
  • "What is the most popular author?"


It's worth noting that as of django 1.8 you may have UUIDFields. If you copy a model instance and then save it the unique constraint will fail on this column, in which case you have to do something like:

import uuid
m = MyModel.objects.get(pk=1)
m.pk = None
m.uuid = uuid.uuid4() //generate new uuid
m.save()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜