Create model instance using data from related object

dease

I have two following models (with OneToOne relation):

class Company(BaseModel):
    name = models.CharField(max_length=64)
    address_street = models.CharField(max_length=64)
    address_city = models.CharField(max_length=64)
    address_zipcode = models.CharField(max_length=6)
    # ...many other fields here...

class InvoicingData(BaseModel):
    company = models.OneToOneField(Company, null=True, blank=True)
    company_name = models.CharField(max_length=64)
    address_street = models.CharField(max_length=64)
    address_city = models.CharField(max_length=64)
    address_zipcode = models.CharField(max_length=6)

I want to auto-create InvoicingData model instance, when new Company is being added. I wanted to use signal to do so:

@receiver(post_save, sender=Company)
def create_invoicing_data(sender, instance, created, **kwargs):
    if created:
        # WHAT HERE?

Is there any simple, automated way to copy shared fields values from one model to another?

Mark R.
@receiver(post_save, sender=Company)
def create_invoicing_data(sender, instance, created, **kwargs):
    if created:
        invdata = InvoicingData(company=instance)
        # set any other fields on invdata if desired
        invdata.save()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create new object on ModelForm save, with values from related model

From Dev

Django REST Framework: create related model from POSTed data

From Dev

Django: how to create an instance of the model based on the data from the json file?

From Dev

Create a field in form for each model instance related to another instance in django

From Dev

Create instance of model from method

From Dev

Waterline: Populate model related object on create (Sails)

From Dev

Django create new instance of model with '__init__' function from table, instance data is error?

From Dev

How to create an Ext.data.Model from a generic object?

From Dev

Create an Object instance from a Map

From Dev

Retrieving related data from Result object

From Dev

Rails - Create instance of a model from within another model

From Dev

How to create an instance of an object in session using django?

From Java

How to create a new object instance from a Type

From Dev

How to create instance of object from prototype method

From Dev

Create new object from instance of another

From Dev

How to create an object of an instance from string in Python

From Dev

NodeJS - Javascript create object instance from String

From Dev

Create class object instance named from string?

From Dev

django-need to create model object when related forigenkey model object is created

From Dev

Object reference not set to an instance of an object when using custom model binding

From Dev

"Cannot assign / must be an instance" error when creating a model instance from a CSV (ForeignKey related)

From Dev

Django. How to annotate a object count from a related model

From Dev

Better way to duplicate model instance and related model

From Dev

How to create an object instance of a typescript class from within plain js using AMD modules

From Dev

How to create an object instance of a typescript class from within plain js using AMD modules

From Dev

How to combine related data from two related models using EF?

From Dev

Extracting related data from XML using SQL

From Dev

SLC Loopback: Using model instance from within a model hook

From Dev

create a model instance with associations

Related Related

  1. 1

    Create new object on ModelForm save, with values from related model

  2. 2

    Django REST Framework: create related model from POSTed data

  3. 3

    Django: how to create an instance of the model based on the data from the json file?

  4. 4

    Create a field in form for each model instance related to another instance in django

  5. 5

    Create instance of model from method

  6. 6

    Waterline: Populate model related object on create (Sails)

  7. 7

    Django create new instance of model with '__init__' function from table, instance data is error?

  8. 8

    How to create an Ext.data.Model from a generic object?

  9. 9

    Create an Object instance from a Map

  10. 10

    Retrieving related data from Result object

  11. 11

    Rails - Create instance of a model from within another model

  12. 12

    How to create an instance of an object in session using django?

  13. 13

    How to create a new object instance from a Type

  14. 14

    How to create instance of object from prototype method

  15. 15

    Create new object from instance of another

  16. 16

    How to create an object of an instance from string in Python

  17. 17

    NodeJS - Javascript create object instance from String

  18. 18

    Create class object instance named from string?

  19. 19

    django-need to create model object when related forigenkey model object is created

  20. 20

    Object reference not set to an instance of an object when using custom model binding

  21. 21

    "Cannot assign / must be an instance" error when creating a model instance from a CSV (ForeignKey related)

  22. 22

    Django. How to annotate a object count from a related model

  23. 23

    Better way to duplicate model instance and related model

  24. 24

    How to create an object instance of a typescript class from within plain js using AMD modules

  25. 25

    How to create an object instance of a typescript class from within plain js using AMD modules

  26. 26

    How to combine related data from two related models using EF?

  27. 27

    Extracting related data from XML using SQL

  28. 28

    SLC Loopback: Using model instance from within a model hook

  29. 29

    create a model instance with associations

HotTag

Archive