简体   繁体   English

Django模型实例的副本以及相关的一对一字段

[英]copy of a Django model instance along with a related One-to-One field

How do I create a copy of a Django model instance along with a related One-to-One field? 如何创建Django模型实例的副本以及相关的一对一字段?

Copying the model instance works fine, but when I try to create a copy of the one-to-one model, all the fields of that model become blank. 复制模型实例可以正常工作,但是当我尝试创建一对一模型的副本时,该模型的所有字段都变为空白。 Here's what I did: 这是我所做的:

            new_address = self.object.designer.address # type Address
            new_address.pk = None
            new_address.save()

            new_contact = self.object.designer # type Contact
            new_contact.pk = None
            new_contact.address = new_address

            new_contact.save()

            self.object.shippinginfo.contact = new_contact
            self.object.shippinginfo.save()

The Contact model has a one-to-one relationship with the Address model. 联系人模型与地址模型具有一对一的关系。 I tried printing out the values, after creating the new address, the values were correct when I printed them out, but then when I save the address to the address field of the new contact, all of the fields of the address are blank except the pk... 在创建新地址后,我尝试打印出这些值,当我将它们打印出时,这些值是正确的,但是当我将地址保存到新联系人的地址字段中时,该地址的所有字段均为空白PK ...

I think you're not clear about how to define relationship. 我认为您不清楚如何定义关系。 If Contact model has one to one relationship with Address model, then one object of Contact class can be related to one object of Address model. 如果Contact模型与Address模型具有一对一关系,则Contact类的一个对象可以与Address模型的一个对象相关。 It'll be defined as: 它将定义为:

class Contact(models.Model):
    # field_one = ...
    # field_two = ...
    # and so on...

class Address(models.Model):
    contact = OneToOneField(Contact)
    # and more model fields...

This way you can associate one Contact object to one Address object. 这样,您可以将一个Contact对象与一个Address对象关联。 If you want to have more than one address for one contact then you should use ForeignKey . 如果您希望一个联系人拥有多个地址,则应使用ForeignKey

For one Contact object having related to many Address instances, you can define relationship as: 对于一个与许多Address实例相关的Contact对象,可以将关系定义为:

class Contact(models.Model):
   # field_one = ...
   # field_two = ...
   # and so on...

class Address(models.Model):
    contact = ForeignKey(Contact)
    # and more fields...

But here an Address object can be associated to a particular Contact object only. 但是,这里的地址对象只能与特定的联系人对象关联。

You can read about Many To Many realtionship here. 您可以在此处阅读有关多对多房地产的信息。

And you don't have to initialize pk field as it is automatically updated/added. 而且您不必初始化pk字段,因为它会自动更新/添加。

To answer your direct question, you may want to use the save(force_insert=True) function and see if that fixes it. 要回答您的直接问题,您可能需要使用save(force_insert=True)函数,看看是否可以解决该问题。 I would also check what you get if you call Contact.objects.all().count() and the same for Address, so you can ensure you are adding new records. 我还将检查如果您调用Contact.objects.all().count()会得到什么,并且对Address也是如此,那么您可以确保添加新记录。

That said, I personally will recommend against what you are trying to do, which in my book, is a hack. 就是说,我个人会建议您不要尝试做的事,在我的书中,这是hack。 Instead, just write the few extra lines of code and properly call the Adress.objects.create() and Contact.objects.create with the fields set from the other records. 相反,只需编写一些额外的代码行,并使用其他记录中设置的字段正确调用Adress.objects.create()Contact.objects.create eg 例如

old_address = self.object.designer.address
new_address = Address.objects.create(line1=old_adress.line1, line2=old_address.line2, etc)

Or even better, use an AddressManager : 甚至更好,使用AddressManager

class AddressManager(models.Manager):
    def create_copy(self, obj):
        address = self.create(line1=obj.line1, etc.)
        return address

class ContactManager(models.Manager):
    def create_copy(self, obj):
        new_address = Address.objects.create_copy(obj.address)
        contact = self.create(name=obj.name, address=new_address, etc.)
        return contact

new_contact = Contact.objects.create_copy(old_contact)

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM